Problem:
Smart Calculator

Your task is to write a program which reads an expression and evaluates it.

The expression consists of numerical values, operators and parentheses, and the ends with '='.

The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.

Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.

You may assume that there is no division by zero.

All calculation is performed as integers, and after the decimal point should be truncated

Length of the expression will not exceed 100.

-1 × 109 ≤ intermediate results of computation ≤ 109

Input

The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.

Output

For each datasets, prints the result of calculation.

Sample Input

2
4-2*3=
4*(8+4+3)=

Output for the Sample Input

-2
60

Solution in JavaScript (judge verdict: Accepted)

#!/usr/bin/node
// Generated by CoffeeScript 1.3.3
// manually optimized. Base for CodeIQ341.
(function() {
  var a=[];
  var stdin = process.openStdin();
  stdin.setEncoding('utf8');

  var proc = function(s){
    var bidx=s.indexOf('('),count,eidx,m;
    while(bidx!=-1){
      eidx=bidx+1;
      for(count=1;count;eidx++){
        if(s[eidx]=='(')count++;
        if(s[eidx]==')')count--;
      }
      s=s.substring(0,bidx)+proc(s.substring(bidx+1,eidx-1))+s.substring(eidx);
      bidx=s.indexOf('(');
    }
    while(m = /^(.*?)(-?\d+)([*/])(-?\d+)(.*)$/.exec(s)){
      if (m[3] == '*') {
        s = m[1] + (parseInt(m[2]) * parseInt(m[4])) + m[5];
      } else {
        s = m[1] + (parseInt(m[2]) / parseInt(m[4]) ^ 0) + m[5];
      }
    }
    while(m = /^(.*?)(-?\d+)([+Z])(-?\d+)(.*)$/.exec(s)){
      if (m[3] == '+') {
        s = m[1] + (parseInt(m[2]) + parseInt(m[4])) + m[5];
      } else {
        s = m[1] + (parseInt(m[2]) - parseInt(m[4])) + m[5];
      }
    }
    return s;
  };

  stdin.on('data', function(input) {
    var i=0,len,ref,x=[];
    ref=input.split("\n");
    for(len=ref.length;i<len;i++)x.push(ref[i]);
    if(x.length>2||!isNaN(x[1]))a=x;
    else a.push(x[0]);
  });

  stdin.on('end', function(z) {
    var i,n,s;
    n = parseInt(a[0]);
    for(i=1;i<=n;i++){
      s=a[i].substring(0,a[i].indexOf('=')).replace(/-/g,'Z');
      console.log(proc(s));
    }
  });

}).call(this);