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: Wrong Answer)

import java.util.*;

public class Main{
    Scanner in = new Scanner(System.in);

    HashMap<String, Integer> op = new HashMap<String, Integer>(){{
        put("*", 1); put("/", 1); put("%", 1);
        put("+", 2); put("-", 2);
        put("(", 3); put(")", 3);
    }};

    void solve(){
        int n = in.nextInt(); in.nextLine();
        for(int i = 0; i < n; i++){
            ArrayDeque<String> q = toRevPolish(in.nextLine());
            System.out.println(calc(q));
            // while(q.size() > 0){
            //     System.out.println(q.pop());
            // }
        }
    }

    ArrayDeque<String> toRevPolish(String str){
        ArrayDeque<String> p = new ArrayDeque<>(), s = new ArrayDeque<>();
        str = str.substring(0, str.length() - 1);

        while(str.length() > 0){
            int idx = nextIndex(str);
            String t = str.substring(0, idx);
            if(t.equals("(")){
                s.push(t);
            }else if(t.equals(")")){
                while(!s.peek().equals("(")){
                    p.push(s.pop());
                }
                s.pop();
            }else{
                while(s.size() > 0 && getRank(t) > getRank(s.peek())){
                    p.push(s.pop());
                }
                s.push(t);
            }
            str = str.substring(idx);
        }

        while(p.size() > 0){
            s.push(p.pop());
        }

        return s;
    }

    int calc(ArrayDeque<String> q){
        Deque<Integer> t = new ArrayDeque<>();

        while(q.size() > 0){
            String s = q.pop();
            if(isOperator(s)){
                int a = t.pop(), b = t.pop();
                switch(s){
                    case "+": t.push(b + a); break;
                    case "-": t.push(b - a); break;
                    case "*": t.push(b * a); break;
                    case "/": t.push(b / a); break;
                    case "%": t.push(b % a); break;
                }
            }else{
                t.push(Integer.parseInt(s));
            }
           
        }

        return t.pop();
    }

    int nextIndex(String str){
        int idx = 0;
        while(idx < str.length() && !isOperator(str.charAt(idx))) idx++;
        if(idx == 0) idx = 1;
        return idx;
    }

    boolean isOperator(char ch){
        return "*/%+-()".indexOf(ch) > -1;
    }

    boolean isOperator(String s){
        return s.length() == 1 && isOperator(s.charAt(0));
    }

    int getRank(String str){
        return isOperator(str) ? op.get(str) : 0;
    }
    
    public static void main(String[] args){
        new Main().solve();    
    }
}