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

#include <ctype.h>
#include <iostream>
#include <string>

std::string s;
std::string::iterator it;

int term();
int fact();

// ?¨????
int expr() {
  int p = term();

  // + or -
  while (*it == '+' || *it == '-') {
    if (*it == '+') {
      it++;
      int q = term();
      p += q;
    } else {
      it++;
      int q = term();
      p -= q;
    }
  }

  return p;
}

// ???????¨????
int term() {
  int p = fact();

  while (*it == '*' || *it == '/') {
    if (*it == '*') {
      it++;
      int q = fact();
      p *= q;
    } else {
      it++;
      int q = fact();
      p /= q;
    }
  }

  return p;
}

// ??¬??§??? or ??°???
int fact() {
  if (*it == '(') {
    it++;
    int p = expr();
    it++;

    return p;
  } else {
    int p = 0;

    while (isdigit(*it)) {
      p *= 10;
      p += *it - '0';
      it++;
    }

    return p;
  }
}

int main() {
  int n;
  std::cin >> n;

  while (n--) {
    std::cin >> s;
    s[s.size() - 1] = '\\0';
    it = s.begin();
    std::cout << expr() << std::endl;
  }

  return 0;
}

Error: -6
Expected '=' but got '

Fixed submission:

// https://gist.github.com/draftcode/1357281
#include <cctype>
#include <iostream>
#include <string>
typedef std::string::iterator State;
class ParseError {};

int number(State &begin);
int factor(State &begin);
int term(State &begin);
int expression(State &begin);
void consume(State &begin, char expected) {
  if (*begin == expected) {
    begin++;
  } else {
    std::cerr << "Expected '" << expected << "' but got '" << *begin << "'"
              << std::endl;
    std::cerr << "Rest string is '";
    while (*begin) {
      std::cerr << *begin++;
    }
    std::cerr << "'" << std::endl;
    throw ParseError();
  }
}

int main(void) {
  int N;
  std::cin >> N;
  // std::cin.ignore();
  while (N--) {
    std::string s;
    std::getline(std::cin, s);
    State begin = s.begin();
    int ans = expression(begin);
    consume(begin, '=');
    std::cout << ans << std::endl;
  }
  return 0;
}

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

  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    begin++;
  }

  return ret;
}

int factor(State &begin) {
  if (*begin == '(') {
    consume(begin, '(');
    int ret = expression(begin);
    consume(begin, ')');
  } else {
    return number(begin);
  }
}

int term(State &begin) {
  int ret = factor(begin);

  for (;;) {
    if (*begin == '*') {
      begin++;
      ret *= factor(begin);
    } else if (*begin == '/') {
      begin++;
      ret /= factor(begin);
    } else {
      break;
    }
  }
  return ret;
}

int expression(State &begin) {
  int ret = term(begin);

  for (;;) {
    if (*begin == '+') {
      begin++;
      ret += term(begin);
    } else if (*begin == '-') {
      begin++;
      ret -= term(begin);
    } else {
      break;
    }
  }
  return ret;
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
#include <cctype>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
// #include<math.h>

using namespace std;

string S;
size_t cur = 0;

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

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 a = term();
  while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) {
    char op = S[cur];
    cur = cur + 1;
    int b = term();
    if (op == '+')
      a += b;
    else
      a -= b;
  }
  return a;
}

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

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

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cur = 0;
    cin >> S;
    S.resize(S.size() - 1);
    cout << expression() << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_M_replace_aux


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>

using namespace std;

string S;
size_t cur = 0;

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 factor() {
  if (S[cur] != '(')
    return number();
  cur += 1;
  int n = expression();
  assert(S[cur] == ')');
  cur += 1;
  return n;
}

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

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

int parse() { return expression(); }

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    cur = 0;
    scanf("%s", &S);
    S.resize(S.size() - 1);
    cout << parse() << endl;
  }
  return 0;
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int compute(string s) {
  int begin;
  int nest = 0;
  int n = 0;
  vector<int> children, children2;
  vector<char> operators, operators2;

  for (int i = 0; i < (int)s.length(); i++) {
    if (s[i] == '(') {
      nest++;
      if (nest == 1) {
        begin = i + 1;
      }
    } else if (s[i] == ')') {
      if (nest == 1) {
        string substr = s.substr(begin, i - begin);
        n = compute(substr);
      }
      nest--;
    } else if (nest == 0) {
      if ('0' <= s[i] && s[i] <= '9') {
        n *= 10;
        n += s[i] - '0';
      } else {
        children.push_back(n);
        operators.push_back(s[i]);
        n = 0;
      }
    }
  }
  children.push_back(n);
  children2.push_back(children[0]);
  for (int i = 0; i < (int)operators.size(); i++) {
    if (operators[i] == '*') {
      children2[children2.size() - 1] *= children[i + 1];
    } else if (operators[i] == '/') {
      children2[children2.size() - 1] /= children[i + 1];
    } else {
      children2.push_back(children[i + 1]);
      operators2.push_back(operators[i]);
    }
  }
  n = children2[0];
  for (int i = 0; i < (int)operators2.size(); i++) {
    if (operators2[i] == '+') {
      n += children2[i + 1];
    } else {
      n -= children2[i + 1];
    }
  }
  return n;
}

int main() {
  int n;
  string s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    cout << compute(s.substr(0, s.length() - 1)) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int compute(string s) {
  int begin;
  int nest = 0;
  int n = 0;
  vector<int> children, children2;
  vector<char> operators, operators2;

  for (int i = 0; i < (int)s.length(); i++) {
    if (s[i] == '(') {
      nest++;
      if (nest == 1) {
        begin = i + 1;
      }
    } else if (s[i] == ')') {
      if (nest == 1) {
        string substr = s.substr(begin, i - begin);
        n = compute(substr);
      }
      nest--;
    } else if (nest == 0) {
      if ('0' <= s[i] && s[i] <= '9') {
        n *= 10;
        n += s[i] - '0';
      } else {
        children.push_back(n);
        operators.push_back(s[i]);
        n = 0;
      }
    }
  }
  children.push_back(n);
  if (operators.size() == 0) {
    return children[0];
  }
  children2.push_back(children[0]);
  for (int i = 0; i < (int)operators.size(); i++) {
    if (operators[i] == '*') {
      children2[children2.size() - 1] *= children[i + 1];
    } else if (operators[i] == '/') {
      children2[children2.size() - 1] /= children2[i + 1];
    } else {
      children2.push_back(children[i + 1]);
      operators2.push_back(operators[i]);
    }
  }
  n = children2[0];
  for (int i = 0; i < (int)operators2.size(); i++) {
    if (operators2[i] == '+') {
      n += children2[i + 1];
    } else {
      n -= children2[i + 1];
    }
  }
  return n;
}

int main() {
  int n;
  string s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    cout << compute(s.substr(0, s.length() - 1)) << endl;
  }
  return 0;
}
Problem p00109 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
string s;
int a;
int func(); //+-
int num();
int func2(); //*/
int func3(); //()
int num() {
  int out = 0;
  while (isdigit(s[a])) {
    out *= 10;
    out += s[a] - '0';
    a++;
  }
  return out;
}
int func3() {
  int out;
  if (s[a] == '(') {
    a++;
    out = func();
    a++;
  } else
    out = num();
  return out;
}
int func2() {
  int out = func3();
  while (1) {
    if (s[a] == '*') {
      a++;
      out *= func3();
    } else if (s[a] == '/') {
      a++;
      out /= func3();
    } else
      break;
  }
  return out;
}
int func() {
  int out = func2();
  while (1) {
    if (s[a] == '+') {
      a++;
      out += func2();
    } else if (s[a] == '-') {
      a++;
      out -= func2();
    } else
      break;
  }
  return out;
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    a = 0;
    cin >> s;
    // s="0+"+s;
    cout << func() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
string s;
int a;
int func();
int num();
int func2();
int num() {
  int out = 0;
  while (isdigit(s[a])) {
    out *= 10;
    out += s[a] - '0';
    a++;
  }
  return out;
}
int func2() {
  int out;
  if (isdigit(s[a]))
    out = num();
  else {
    a++;
    out = func();
  }
  while (a != s.size()) {
    if (s[a] == '*') {
      a++;
      if (s[a] == '(') {
        a++;
        out *= func();
      } else
        out *= num();
    } else if (s[a] == '/') {
      a++;
      if (s[a] == '(') {
        a++;
        out /= func();
      } else
        out /= num();
    } else
      return out;
  }
  return out;
}
int func() {
  int out;
  if (isdigit(s[a]))
    out = num();
  else {
    a++;
    out = func();
  }
  while (a != s.size()) {
    // cout<<a<<" "<<out<<endl;
    if (s[a] == '=')
      return out;
    if (s[a] == ')') {
      a++;
      return out;
    }
    if (s[a] == '+') {
      a++;
      out += func2();
    } else if (s[a] == '-') {
      a++;
      out -= func2();
    }
  }
  return out;
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    a = 0;
    cin >> s;
    s = "0+" + s;
    cout << func() << endl;
  }
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error



#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int n;
string S;
int cur;

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

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

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

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

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

int main(void) {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cur = 0;
    cin >> S;
    S.resize(S.size() - 1);
    cout << expression() << endl;
  }

  return 0;
}

Error: -6
ba659a11-8258-4a0c-a323-9eda06e01966.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00109/C++/s056365126.cpp:24: int digit(): Assertion `isdigit(S[cur])' failed.


Fixed submission:


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int n;
string S;
int cur;

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

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

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

int term() {
  int sum = number();
  while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
    char op = S[cur];
    cur++;
    int b = number();
    if (op == '*')
      sum *= b;
    else
      sum /= b;
  }
  return sum;
}

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

int main(void) {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cur = 0;
    cin >> S;
    S.resize(S.size() - 1);
    cout << expression() << endl;
  }

  return 0;
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
int cur = 0;
string S;
int expression();
int term();
int factor();
int main() {
  int times;
  cin >> times;
  for (int i = 0; i < times; i++) {
    cin >> S;
    cur = 0;
    S.resize(S.size() - 1);
    cout << expression() << endl;
  }
}
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 sum = term();
  while (S[cur] == '+' || S[cur] == '-') {
    char op = S[cur];
    cur += 1;
    int b = term();
    if (op == '+')
      sum += b;
    else
      sum -= b;
  }
  return sum;
}
int term() {
  int a = factor();
  while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
    char op = S[cur];
    cur += 1;
    int b = factor();
    if (op == '*')
      a *= b;
    else
      a /= b;
  }
  return a;
}
int factor() {
  if (S[cur] != '(')
    return number();
  cur += 1;
  int n = expression();
  assert(S[cur] == ')');
  cur += 1;
  return n;
}

Error: -6
4d1c261b-55f5-4beb-81bf-4e698ec61b48.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00109/C++/s806401179.cpp:23: int digit(): Assertion `isdigit(S[cur])' failed.


Fixed submission:

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

using namespace std;
int cur = 0;
string S;
int expression();
int term();
int main() {
  int times;
  cin >> times;
  for (int i = 0; i < times; i++) {
    cin >> S;
    cur = 0;
    S.resize(S.size() - 1);
    cout << expression() << endl;
  }
}
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 sum = term();
  while (S[cur] == '+' || S[cur] == '-') {
    char op = S[cur];
    cur += 1;
    int b = term();
    if (op == '+')
      sum += b;
    else
      sum -= b;
  }
  return sum;
}
int term() {
  int a = number();
  while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
    char op = S[cur];
    cur += 1;
    int b = number();
    if (op == '*')
      a *= b;
    else
      a /= b;
  }
  return a;
}
Problem p00109 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define INF (1 << 26)
using namespace std;
int n;

int compute(int a, int b, string str) {

  int i = a, sum = 0, tmp = INF, tmp2 = INF, cnt = 0;
  while (1) { // cout<<a<<' '<<b<<endl;

    sum = 0;
    if (str[i] == '(') {
      sum = compute(i + 1, 0, str);
      // cout<<sum<<endl;
      cnt = 0;
      for (i = i; i < (int)str.size(); i++) {
        if (str[i] == ')')
          cnt--;
        if (str[i] == '(')
          cnt++;
        if (cnt == 0)
          break;
      }
      i++;

    } else {
      while (1) {
        if ('0' <= str[i] && str[i] <= '9') {
          sum *= 10;
          sum += (str[i] - '0');
          i++;
        } else {
          break;
        }
      }
    }

    if (b == 1) {
      sum *= -1;
      b = 0;
    }
    if (tmp != INF) {
      sum *= tmp;
      tmp = INF;
    }
    if (tmp2 != INF) {
      sum = tmp2 / sum;
      tmp2 = INF;
    }
    // if(a==8)cout<<sum<<endl;
    if (str[i] == '\\0' || str[i] == ')' || str[i] == '=' ||
        i == (int)str.size())
      return sum;
    if (str[i] == '+')
      return sum + compute(i + 1, 0, str);
    if (str[i] == '-')
      return sum + compute(i + 1, 1, str);
    if (str[i] == '*') {
      i++;
      tmp = sum;
    } else if (str[i] == '/') {
      i++;
      tmp2 = sum;
    }
  }
}

int main() {
  string str;
  cin >> n;
  while (n--) {
    cin >> str;

    // cout<<str<<endl;
    cout << compute(0, 0, str) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define INF (1 << 26)
using namespace std;
int n;

int compute(int a, int b, string str) {
  int i = a, sum = 0, tmp = INF, tmp2 = INF, cnt = 0;
  while (1) {
    sum = 0;
    if (str[i] == '(') {
      sum = compute(i + 1, 0, str);
      for (i = a + 1; i < (int)str.size(); i++) {
        if (str[i] == ')')
          cnt--;
        if (str[i] == '(')
          cnt++;
        if (cnt < 0)
          break;
      }
      i++;
    } else {
      while (1) {
        if ('0' <= str[i] && str[i] <= '9') {
          sum *= 10;
          sum += (str[i] - '0');
          i++;
        } else {
          break;
        }
      }
    }

    if (b == 1) {
      sum *= -1;
      b = 0;
    }
    if (tmp != INF) {
      sum *= tmp;
      tmp = INF;
    }
    if (tmp2 != INF) {
      sum = tmp2 / sum;
      tmp2 = INF;
    }

    if (str[i] == '\\0' || str[i] == ')')
      return sum;
    if (str[i] == '+')
      return sum + compute(i + 1, 0, str);
    if (str[i] == '-')
      return sum + compute(i + 1, 1, str);
    if (str[i] == '*') {
      i++;
      tmp = sum;
    } else if (str[i] == '/') {
      i++;
      tmp2 = sum;
    }
  }
}

int main() {
  string str;
  cin >> n;
  while (n--) {
    cin >> str;
    if (str[str.size() - 1] == '=')
      str = str.substr(0, str.size() - 1);
    // cout<<str<<endl;
    cout << compute(0, 0, str) << endl;
  }
  return 0;
}
Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
#define INF (1 << 24)

bool isNum(char ch) { return ('0' <= ch && ch <= '9'); }

struct calcStr {
  string s;
  int pos, len;

  void init() {
    pos = 0;
    len = s.size();
  }

  int head2Num() {
    if (s[pos] == '(') {
      pos++;
      return getAns();
    }

    bool f = false;
    if (s[pos] == '-') {
      f = true;
      pos++;
    }
    int res = 0;
    while (pos < len) {
      if (isNum(s[pos])) {
        res *= 10;
        res += (s[pos] - '0');
        pos++;
      } else
        break;
    }
    if (f)
      res *= -1;
    return res;
  }

  int getNum() {
    int res = head2Num(), num;
    char ch;
    while (s[pos] == '*' || s[pos] == '/') {
      ch = s[pos++];
      num = head2Num();
      if (ch == '*')
        res *= num;
      else if (ch == '/')
        res /= num;
    }
    return res;
  }

  int getAns() {
    int res = getNum(), num;
    while (1) {
      if (s[pos] == ')') {
        pos++;
        break;
      }
      if (s[pos] == '=')
        break;
      char ch = s[pos++];
      num = getNum();
      if (ch == '+')
        res += num;
      else if (ch == '-')
        res -= num;
      else
        assert(0);
    }
    return res;
  }
};

int main() {
  int Tc;
  cin >> Tc;
  calcStr a;
  while (Tc--) {
    cin >> a.s;
    a.init();
    cout << a.getAns() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
#define INF (1 << 24)

bool isNum(char ch) { return ('0' <= ch && ch <= '9'); }

struct calcStr {
  string s;
  int pos, len;

  void init() {
    pos = 0;
    len = s.size();
  }

  int head2Num() {
    // bool f=false;
    // if(s[pos]=='-'){f=true;pos++;}
    int res = 0;
    while (pos < len) {
      if (isNum(s[pos])) {
        res *= 10;
        res += (s[pos] - '0');
        pos++;
      } else
        break;
    }
    // if(f)res*=-1;
    return res;
  }

  int getNum() {
    if (s[pos] == '(') {
      pos++;
      return getAns();
    }
    int res = head2Num();
    if (s[pos] == '*') {
      pos++;
      return res * getNum();
    } else if (s[pos] == '/') {
      pos++;
      return res / getNum();
    } else {
      return res;
    }
  }

  int getAns() {
    int res = getNum(), num;
    while (1) {
      if (s[pos] == ')') {
        pos++;
        break;
      }
      if (s[pos] == '=')
        break;
      char ch = s[pos++];
      num = getNum();
      if (ch == '+')
        res += num;
      else if (ch == '-')
        res -= num;
      else
        assert(0);
    }
    return res;
  }
};

int main() {
  int Tc;
  cin >> Tc;
  calcStr a;
  while (Tc--) {
    cin >> a.s;
    a.init();
    cout << a.getAns() << endl;
  }
  return 0;
}