alexjercan/bugnet huggingface dataset:
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;

int hyoka(string);
int isNum(int);

int main() {
  int n, i;
  string s;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s;
    cout << hyoka(s) << endl;
  }

  return 0;
}

int hyoka(string s) {
  long int total = 0, work = 0;
  long int tmp1, tmp2;
  int oi = 0;
  char oc;
  int n;
  string cs;

  if (s[oi] == '(' || isNum(s[oi]) != -1) {
    s = "+" + s;
  }
  // cout<<s<<endl;

  while (1) {
    oc = s[oi];
    if (oc == '=') {
      total += work;
      break;
    }

    if (s[oi + 1] != '(') { //??°???????????????
      tmp1 = 0;
      while (1) {
        oi++;
        tmp2 = isNum(s[oi]);
        if (tmp2 != -1) {
          tmp1 = tmp1 * 10 + tmp2;
        } else {
          break;
        }
      }
    } else { //??????????????°
      cs = "";
      n = 1;
      oi++;
      while (1) {
        oi++;
        switch (s[oi]) {
        case '(':
          n++;
          break;
        case ')':
          n--;
          break;
        }
        if (n != 0) {
          cs += s[oi];
        } else {
          break;
        }
      }
      cs += '=';
      // cout<<cs<<endl;
      tmp1 = hyoka(cs);
    }

    switch (oc) {
    case '+':
      total += work;
      work = tmp1;
      break;
    case '-':
      total += work;
      work = -tmp1;
      break;
    case '*':
      work *= tmp1;
      break;
    case '/':
      work /= tmp1;
      break;
    }
  }

  return total;
}

int isNum(int c) {
  if (48 <= c && c <= 57) {
    return c - 48;
  }
  return -1;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int hyoka(string);
int isNum(int);

int main() {
  int n, i;
  string s;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s;
    cout << hyoka(s) << endl;
  }

  return 0;
}

int hyoka(string s) {
  long int total = 0, work = 0;
  long int tmp1, tmp2;
  int oi = 0;
  char oc;
  int n;
  string cs;

  if (isNum(s[oi]) != -1) {
    s = "+" + s;
  }

  while (1) {
    oc = s[oi];
    if (oc == '=') {
      total += work;
      break;
    }

    if (s[oi + 1] != '(') { //??°???????????????
      tmp1 = 0;
      while (1) {
        oi++;
        tmp2 = isNum(s[oi]);
        if (tmp2 != -1) {
          tmp1 = tmp1 * 10 + tmp2;
        } else {
          break;
        }
      }
    } else { //??????????????°
      cs = "";
      n = 1;
      oi++;
      while (1) {
        oi++;
        switch (s[oi]) {
        case '(':
          n++;
          break;
        case ')':
          n--;
          break;
        }
        if (n != 0) {
          cs += s[oi];
        } else {
          break;
        }
      }
      cs += '=';
      tmp1 = hyoka(cs);
    }

    switch (oc) {
    case '+':
      total += work;
      work = tmp1;
      break;
    case '-':
      total += work;
      work = -tmp1;
      break;
    case '*':
      work *= tmp1;
      break;
    case '/':
      work /= tmp1;
      break;
    }
  }

  return total;
}

int isNum(int c) {
  if (49 <= c && c <= 57) {
    return c - 49;
  }
  return -1;
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <iostream>
#include <string>

using namespace std;

string str;
int cur = 0;

int expression();
int term();
int factor();

int expression();

int digit() {
  int a = str[cur] - '0';
  cur++;
  return a;
}

int number() {
  int a = digit();
  while (cur < str.size() && isdigit(str[cur])) {
    a = a * 10 + digit();
  }
  return a;
}

int expression() {
  int a = term();
  while (cur < str.size() && (str[cur] == '+' || str[cur] == '-')) {
    if (str[cur] == '+') {
      cur++;
      int b = term();
      a += b;
    } else if (str[cur] == '-') {
      cur++;
      int b = term();
      a -= b;
    }
  }
  return a;
}

int term() {
  int a = factor();
  while (cur < str.size() && (str[cur] == '*' || str[cur] == '/')) {
    if (str[cur] == '*') {
      cur++;
      int b = factor();
      a *= b;
    } else if (str[cur] == '/') {
      cur++;
      int b = factor();
      a /= b;
    }
  }
  return a;
}

int factor() {
  if (str[cur] != '(')
    return number();
  cur++;
  int a = expression();
  cur++;
  return a;
}

int main() {
  int N = 0;
  cin >> N;
  for (int n = 0; n < N; n++) {
    cin >> str;
    cur = 0;
    cout << expression() << endl;
  }
}

Error: 0

Fixed submission:

#include <cassert>
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

string str;
int cur = 0;

int expression();
int term();
int factor();

int digit() {
  assert(isdigit(str[cur]));
  int a = str[cur] - '0';
  cur++;
  return a;
}

int number() {
  int a = digit();
  while (cur < str.size() && isdigit(str[cur])) {
    a = a * 10 + digit();
  }
  return a;
}

int expression() {
  int a = factor();
  while (cur < str.size() && (str[cur] == '+' || str[cur] == '-')) {
    if (str[cur] == '+') {
      cur++;
      int b = factor();
      a += b;
    } else if (str[cur] == '-') {
      cur++;
      int b = factor();
      a -= b;
    }
  }
  return a;
}

int term() {
  int a = number();
  while (cur < str.size() && (str[cur] == '*' || str[cur] == '/')) {
    if (str[cur] == '*') {
      cur++;
      int b = factor();
      a *= b;
    } else if (str[cur] == '/') {
      cur++;
      int b = factor();
      a /= b;
    }
    cur++;
  }
  return a;
}

int factor() {
  if (str[cur] != '(')
    return term();

  cur++;
  int a = expression();
  assert(str[cur] == ')');
  cur++;
  return a;
}

int main() {
  int N = 0;
  cin >> N;
  for (int n = 0; n < N; n++) {
    cin >> str;
    cur = 0;
    cout << expression() << endl;
  }
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int cur;
string s;

int digit();
int number();
int factor();
int term();
int expression();

int digit() { return (s[cur++] - '0'); }

int number() {
  int n = digit();
  while (isdigit(s[cur]))
    n = n * 10 + digit();
  return n;
}

int factor() {
  int n;
  if (s[cur] == '(') {
    cur++;
    n = expression();
    cur++;
  } else
    n = number();
  return n;
}

int term() {
  int n = factor();
  while (s[cur] == '*' || s[cur] == '/') {
    cur++;
    if (s[cur - 1] == '*')
      n *= factor();
    else
      n /= factor();
  }
  return n;
}

int expression() {
  int n = term();
  while (s[cur] == '+' || s[cur] == '-') {
    cur++;
    if (s[cur - 1] == '+')
      n += term();
    else
      n -= term();
  }
  return n;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cur = 0;
    cin >> s;
    int m = expression();
    cout << m << endl;
  }
}

Error: -11

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <stack>
#include <string>
using namespace std;

int main() {
  /*const*/ string s;
  stack<int> num;
  stack<int> mar;
  int n1, n2;
  cin >> s;
  for (int i = 0; i < 100; i++) {
    if (s.substr(i, 1) == "+") {
      if (mar.top() == 0) {
        n1 = num.top();
        num.pop();
        num.push(n1 + n2);
        mar.pop();
      } else if (mar.top() == 1) {
        n1 = num.top();
        num.pop();
        num.push(n1 - n2);
        mar.pop();
      }
      mar.push(0);
    } else if (s.substr(i, 1) == "-") {
      mar.push(1);
      if (mar.top() == 0) {
        n1 = num.top();
        num.pop();
        num.push(n1 + n2);
        mar.pop();
      } else if (mar.top() == 1) {
        n1 = num.top();
        num.pop();
        num.push(n1 - n2);
        mar.pop();
      }
    } else if (s.substr(i, 1) == "*")
      mar.push(2);
    else if (s.substr(i, 1) == "/")
      mar.push(3);
    else if (s.substr(i, 1) == "(")
      mar.push(4);
    else if (s.substr(i, 1) == ")") {
      if (mar.top() == 0) {
        n1 = num.top();
        num.pop();
        num.push(n1 + n2);
        mar.pop();
      } else if (mar.top() == 1) {
        n1 = num.top();
        num.pop();
        num.push(n1 - n2);
        mar.pop();
      }
      mar.pop();
    } else if (s.substr(i, 1) == "=") {
      if (mar.top() == 0) {
        n1 = num.top();
        num.pop();
        num.push(n1 + n2);
        mar.pop();
      } else if (mar.top() == 1) {
        n1 = num.top();
        num.pop();
        num.push(n1 - n2);
        mar.pop();
      }
      cout << num.top();
      break;
    } else {
      n2 = atoi(s.c_str());
      if (mar.top() == 2) {
        n1 = num.top();
        num.pop();
        num.push(n1 * n2);
        mar.pop();
      } else if (mar.top() == 3) {
        n1 = num.top();
        num.pop();
        num.push(n1 / n2);
        mar.pop();
      } else
        num.push(n2);
    }
  }
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define INF (1 << 25)
#define P(a, b) make_pair(a, b)

int id = 0;
string s;

int exp();
int term();
int fact();

int exp() {
  int ans = term();
  while (true) {
    char c = s[id++];
    if (c == '+') {
      ans += term();
    } else if (c == '-') {
      ans -= term();
    } else {
      return ans;
    }
  }
}
int term() {
  int ans = fact();
  while (true) {
    char c = s[id++];
    if (c == '*') {
      ans *= fact();
    } else if (c == '/') {
      ans /= fact();
    } else {
      id--;
      return ans;
    }
  }
}
int fact() {
  char c = s[id++];
  if (c == '(')
    return exp();
  if (c == '-')
    return -exp();
  if (c == '+')
    return exp();
  int x = c - '0';
  while (true) {
    c = s[id++];
    if ('0' <= c && c <= '9') {
      x = x * 10 + c - '0';
    } else {
      id--;
      return x;
    }
  }
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    cin >> s;
    id = 0;
    cout << exp() << endl;
  }
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

string str, t;

string calc(string s) {

  vector<char> otr;
  otr.reserve(1000);
  vector<int> ond;
  ond.reserve(1000);
  string tmp;
  size_t now = 0;

  while (~s.find_first_of("*/-+=", now)) {
    tmp = s.substr(now, s.find_first_of("*/-+=", now) - now);
    ond.push_back(atoi(tmp.c_str()));
    otr.push_back(s[s.find_first_of("*/-+=", now)]);
    now = s.find_first_of("*/-+=", now) + 1;
  }

  for (int i = 0; i < otr.size(); i++) {
    if (otr[i] == '*') {
      ond[i] = ond[i] * ond[i + 1];
      ond.erase(ond.begin() + i + 1);
      otr.erase(otr.begin() + i);
      i--;
    } else if (otr[i] == '/') {
      // ond[i] = ond[i] / ond[i+1] ;
      ond.erase(ond.begin() + i + 1);
      otr.erase(otr.begin() + i);
      i--;
    }
  }

  /*cout << ond[0];
  for(int j=0;j<otr.size();j++){
          if(otr[j]=='=')break;
          cout << " " << otr[j] << " ";
          cout << ond[j+1];
  }
  cout << endl;*/

  for (int i = 0; i < otr.size(); i++) {
    if (otr[i] == '+') {
      ond[i] = ond[i] + ond[i + 1];
      ond.erase(ond.begin() + i + 1);
      otr.erase(otr.begin() + i);
      i--;
    } else if (otr[i] == '-') {
      ond[i] = ond[i] - ond[i + 1];
      ond.erase(ond.begin() + i + 1);
      otr.erase(otr.begin() + i);
      i--;
    }
  }
  stringstream ret;
  ret << ond[0];
  return ret.str();
}

string parent(string s) {
  int st, en;
  while (~s.find("(")) {
    st = s.find("(");
    en = s.find(")");
    t = s.substr(st + 1, en - st - 1);
    s.replace(st, en - st + 1, parent(t + string("=")));
  }
  return calc(s);
}

int main() {
  int n;
  cin >> n;
  while (cin >> str) {
    /*
    if(~s.find("(")){
            st = s.find("(");
            en = s.rfind(")");
            t = s.substr(st+1,en-st-1);
    }*/
    cout << parent(str) << endl;
  }
}
Problem p00109 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cassert>
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

string S;
size_t cur = 0;
int N;
int parse();

char readchar() {
  assert(cur < S.size());
  return S[cur++];
}

char peek() {
  // assert(cur < S.size());
  return S[cur];
}

int digit() {
  assert(isdigit(peek()));
  int n = readchar() - '0';
  return n;
}

int number() {
  int n = digit();
  while (cur < S.size() && isdigit(peek())) {
    n = n * 10 + digit();
  }
  return n;
}

int expression();

int factor() {
  if (peek() != '(')
    return number();
  cur += 1;
  int n = expression();
  assert(peek() == ')');
  cur += 1;
  return n;
}

int term() {
  int a = factor();
  while (cur < S.size() && (peek() == '*' || peek() == '/')) {
    char op = readchar();
    int b = factor();
    if (op == '*')
      a *= b;
    else
      a /= b;
  }
  return a;
}

int expression() {
  int a = term();
  while (cur < S.size() && (peek() == '+' || peek() == '-')) {
    char op = readchar();
    int b = term();
    if (op == '+')
      a += b;
    else
      a -= b;
  }
  return a;
}

int parse() { return expression(); }

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S;
    cur = 0;
    int a = parse();
    cout << a << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int t;
int N;
int d[110], f[110];
vector<vector<int>> adj(110);
void dfs(int cur) {
  d[cur] = t;
  t += 1;
  // cerr << "visiting " << cur << " " << t << endl;
  for (int i = 0; i < adj[cur].size(); ++i) {
    int dst = adj[cur][i];
    if (d[dst] == 0) {
      dfs(dst);
    }
  }
  f[cur] = t;
  t++;
}

int main() {
  cin >> N;
  t = 1;
  int u, k;
  for (int i = 0; i < N; ++i) {
    d[i] = 0;
    f[i] = 0;
  }
  for (int i = 0; i < N; ++i) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      adj[u].push_back(v);
    }
    sort(adj[u].begin(), adj[u].end());
  }
  for (int i = 1; i < N + 1; ++i) {
    if (d[i] == 0)
      dfs(i);
  }

  for (int i = 1; i < N + 1; ++i) {
    cout << 1 << " " << d[i] << " " << f[i] << endl;
  }
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

// Smart Calculator
#include <cassert>
#include <cstdio>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
using namespace std;
string S;
size_t cur = 0;
bool isdigit(char);
int digit();
int number();
int term();
int expression();
int factor();
int parse() { return expression(); }

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    cur = 0;
    cin >> S;
    S.resize(S.size() - 1);
    cout << parse() << endl;
  }
}

bool isdigit(char c) { return c >= '0' && c <= '9'; }
int digit() {
  assert(isdigit(S[cur]));
  int n = S[cur] - '0';
  cur = cur + 1;
  return n;
}
int number() {
  int n = digit();
  while (cur < S.size() && isdigit(S[cur])) {
    n = n * 10 + digit();
  }
  return n;
}

int expression() {
  int mult = 1;
  if (S[cur] == '-') {
    mult = -1;
    cur++;
  }
  int sum = term() * mult;
  while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) {
    char op = S[cur];
    cur++;
    if (op == '+') {
      sum += term();
    } else {
      sum -= term();
    }
  }
  return sum;
}
int term() {
  int ret = factor();
  while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
    char op = S[cur];
    cur++;
    if (op == '*') {
      ret *= factor();
    } else {
      ret /= factor();
    }
  }
  return ret;
}
int factor() {
  if (S[cur] != '(')
    return number();
  cur += 1;
  int n = expression();
  assert(S[cur] == ')');
  cur++;
  return n;
}

Error: WA

Fixed submission:

// A Reward for a Carpenter
#include <cassert>
#include <cstdio>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
using namespace std;
string S;
size_t cur = 0;
bool isdigit(char);
int digit();
int number();
int term();
int expression();
int factor();
int parse() { return expression(); }

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    cin >> S;
    cout << parse() << endl;
  }
}

bool isdigit(char c) { return c >= '0' && c <= '9'; }
int digit() {
  assert(isdigit(S[cur]));
  int n = S[cur] - '0';
  cur = cur + 1;
  return n;
}
int number() {
  int n = digit();
  while (cur < S.size() && isdigit(S[cur])) {
    n = n * 10 + digit();
  }
  return n;
}

int expression() {
  int mult = 1;
  if (S[cur] == '-') {
    mult = -1;
    cur++;
  }
  int sum = term() * mult;
  while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) {
    char op = S[cur];
    cur++;
    if (op == '+') {
      sum += term();
    } else {
      sum -= term();
    }
  }
  return sum;
}
int term() {
  int ret = factor();
  while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
    char op = S[cur];
    cur++;
    if (op == '*') {
      ret *= factor();
    } else {
      ret /= factor();
    }
  }
  return ret;
}
int factor() {
  if (S[cur] != '(')
    return number();
  cur += 1;
  int n = expression();
  assert(S[cur] == ')');
  cur++;
  return n;
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};

int roc = 0;
string s;
int insu();
int kou();
int shiki();

int insu() {
  if (s[roc] >= '0' && s[roc] <= '9') {
    int j = roc;
    while (roc < s.size() && s[roc] >= '0' && s[roc] <= '9')
      ++roc;
    return stoi(s.substr(j, roc - j));
  } else {
    ++roc;
    int res = shiki();
    ++roc;
    return res;
  }
}

int kou() {
  int res = insu();
  while (s[roc] == '*' || s[roc] == '/') {
    if (s[roc] == '*') {
      ++roc;
      res *= insu();
    } else {
      ++roc;
      res /= insu();
    }
  }
  return res;
}

int shiki() {
  int res = kou();
  while (s[roc] == '+' || s[roc] == '-') {
    if (s[roc] == '+') {
      ++roc;
      res += kou();
    } else {
      ++roc;
      res -= kou();
    }
  }
  return res;
}

int main(void) {
  int n;
  cin >> n;
  REP(roop, n) {
    roc = 0;
    cin >> s;
    s.pop_back();
    cout << shiki() << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};

int roc = 0;
string s;
int insu();
int kou();
int shiki();

int insu() {
  if (s[roc] >= '0' && s[roc] <= '9') {
    int j = roc;
    while (roc < s.size() && s[roc] >= '0' && s[roc] <= '9')
      ++roc;
    return stoi(s.substr(j, roc - j));
  } else {
    ++roc;
    int res = shiki();
    ++roc;
    return res;
  }
}

int kou() {
  int res = insu();
  while (s[roc] == '*' || s[roc] == '/') {
    if (s[roc] == '*')
      res *= insu();
    else
      res /= insu();
  }
  return res;
}

int shiki() {
  int res = kou();
  while (s[roc] == '+' || s[roc] == '-') {
    if (s[roc] == '+')
      res += insu();
    else
      res -= insu();
  }
  return res;
}

int main(void) {
  int n;
  cin >> n;
  REP(roop, n) {
    roc = 0;
    cin >> s;
    s.pop_back();
    cout << shiki() << endl;
  }
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <iostream>
#include <string>
using namespace std;
typedef string::const_iterator State;

int number(State &begin);
int term(State &begin);
int expression(State &begin);
int factor(State &begin);

int number(State &begin) {
  int ret = 0;

  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    begin++;
  }
  // cout << "number :: " << ret << endl;
  return ret;
}
int term(State &begin) {
  int ret = factor(begin);

  while (1) {
    if (*begin == '*') {
      begin++;
      // cout << "go factor // *" << endl;
      ret *= factor(begin);
    } else if (*begin == '/') {
      begin++;
      // cout << "go factor // /" << endl;
      ret /= factor(begin);
    } else
      break;
  }
  // // cout << "term :: " << ret << endl;
  return ret;
}
int expression(State &begin) {
  int ret = term(begin);

  while (1) {
    if (*begin == '+') {
      begin++;
      // // cout << "go term // +" << endl;
      ret += term(begin);
    } else if (*begin == '-') {
      begin++;
      // // cout << "go term // -" << endl;
      ret -= term(begin);
    } else
      break;
  }
  // // cout << "expression :: " << ret << endl;
  return ret;
}
int factor(State &begin) {
  if (*begin == '(') {
    begin++;
    // // cout << "go expression" << endl;
    int ret = expression(begin);
    // // cout << "factor :: " << ret << endl;
    begin++;
    return ret;
  } else
    return number(begin);
}

int main(void) {
  int n;
  cin >> n;
  cin.ignore();
  for (int i = 0; i < n; i++) {
    string s;
    getline(cin, s);

    State begin = s.begin();
    // cout << "Start !!!" << endl;
    cout << expression(begin) << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cctype>
#include <iostream>
#include <string>
using namespace std;

typedef string::const_iterator State;
int number(State &begin);
int factor(State &begin);
int expression(State &begin);
int term(State &begin);

int number(State &begin) { // 数字
  int ret = 0;
  while (isdigit(*begin)) {
    ret *= 10;
    ret += (*begin - '0');
    begin++;
  }
  return ret;
}
int factor(State &begin) { // 括弧
  if (*begin == '(')
    return expression(begin);
  if (*begin == '-')
    return -1 * factor(begin);
  if (*begin == '+')
    return factor(begin);

  int ret = number(begin);
  begin--;
  return ret;
}
int term(State &begin) { // 乗算除算
  int ret = factor(begin);
  while (1) {
    if (*begin == '*')
      ret *= factor(++begin);
    else if (*begin == '/')
      ret /= factor(++begin);
    else
      break;
  }
  begin--;
  return ret;
}
int expression(State &begin) { // 四則演算
  int ret = term(begin);
  while (1) {
    if (*begin == '+')
      ret += term(++begin);
    else if (*begin == '-')
      ret -= term(++begin);
    else
      break;
  }
  return ret;
}

int main(void) {
  int n;
  cin >> n;
  cin.ignore();
  for (int i = 0; i < n; i++) {
    string str;
    getline(cin, str);

    State begin = str.begin();
    cout << expression(begin) << endl;
  }

  return 0;
}