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 JAVA (judge verdict: Accepted)

/**
 * @author yuichirw
 *
 */
import java.util.*;
public class Main {

	static int n;
	static String line;
	static Scanner sc = new Scanner(System.in);
	
	static void read() {
		line = sc.nextLine();
	}
	
	static int solve() {
		return new MyParser().parse(line.substring(0, line.length() - 1));
	}
	
	public static void main(String[] args) {
		n = sc.nextInt(); sc.nextLine();
		while(n-- > 0) {
			read();
			System.out.println(solve());
		}
	}
	
}

class MyParsed {
	int val;
	String str;
	
	MyParsed(int val, String str) {
		this.val = val; this.str = str;
	}
}

class MyParser {
	
	public int parse(String str) {
		MyParsed mp = expr(str);
		return mp.val;
	}
	
	private MyParsed expr(String str) {
		MyParsed mp = term(str);
		while(mp.str.length() > 0 && (mp.str.charAt(0) == '+' || mp.str.charAt(0) == '-')) {
			char op = mp.str.charAt(0);
			int tmp = mp.val;
			mp = term(mp.str.substring(1));				
			if(op == '+') {
				mp.val = tmp + mp.val;
			} else {
				mp.val = tmp - mp.val;
			}
				
		}
		return mp;
	}
	
	private MyParsed term(String str) {
		MyParsed mp = fact(str);
		while(mp.str.length() > 0 && (mp.str.charAt(0) == '*' || mp.str.charAt(0) == '/')) {
			char op = mp.str.charAt(0);
			int tmp = mp.val;
			mp = fact(mp.str.substring(1));
			if(op == '*') {
				mp.val = tmp * mp.val;
			} else {
				mp.val = tmp / mp.val;
			}
		}
		return mp;
	}
	
	private MyParsed fact(String str) {
		if(Character.isDigit(str.charAt(0))) {
			int pos = 0; String num = "";
			while(pos < str.length() && Character.isDigit(str.charAt(pos))) {
				num += str.charAt(pos++);			
			}
			return new MyParsed(Integer.parseInt(num), str.substring(pos));
		} else {
			MyParsed mp = expr(str.substring(1));
			mp.str = mp.str.substring(1);
			return mp;
		}
	}
}