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

using System.Linq;
using System.Collections.Generic;
using System;

public class hello
{
    public static void Main()
    {
        var n = int.Parse(Console.ReadLine().Trim());
        while (n-- > 0)
        {
            var s = Console.ReadLine().Trim();
            var res = conv3(s);
            var q = conv2(res);
            var ans = getAns(q);
            Console.WriteLine(ans);
        }
    }
    static int getAns(Queue<string> q)
    {
        var st = new Stack<int>();
        while (q.Count() > 0)
        {
            var w = q.Dequeue();
            if (w == "+")
            {
                var t = st.Pop() + st.Pop();
                st.Push(t);
            }
            else if (w == "-")
            {
                var t = -st.Pop() + st.Pop();
                st.Push(t);
            }
            else if (w == "*")
            {
                var t = st.Pop() * st.Pop();
                st.Push(t);
            }
            else if (w == "/")
            {
                var a1 = st.Pop();
                var a2 = st.Pop();
                var t = a2 / a1;
                st.Push(t);
            }
            else st.Push(int.Parse(w));
        }
        return st.Pop();
    }

    static Queue<string> conv2(List<string> line)
    {
        var q = new Queue<string>();
        var st = new Stack<string>();
        foreach (var x in line)
        {
            if (x == "(") st.Push(x);
            else if (x == "+" | x == "-")
            {
                if (st.Count() == 0) st.Push(x);
                else
                {
                    if (st.Peek() == "*" | st.Peek() == "/")
                    {
                        q.Enqueue(st.Pop());
                        if (st.Count() > 0 && (st.Peek() == "+" | st.Peek() == "-")) q.Enqueue(st.Pop());
                        st.Push(x);
                    }
                    else if (st.Peek() == "+" | st.Peek() == "-")
                    {
                        q.Enqueue(st.Pop());
                        st.Push(x);
                    }
                    else if (st.Peek() == "(") { st.Push(x); }
                }
            }
            else if (x == "*" | x == "/")
            {
                if (st.Count() == 0) st.Push(x);
                else
                {
                    if (st.Peek() == "*" | st.Peek() == "/")
                    { q.Enqueue(st.Pop()); st.Push(x); }
                    else if (st.Peek() == "+" | st.Peek() == "-")
                    { st.Push(x); }
                    else if (st.Peek() == "(") { st.Push(x); }
                }
            }
            else if (x == ")")
            {
                while (true)
                {
                    if (st.Peek() == "(") { st.Pop(); break; }
                    else q.Enqueue(st.Pop());
                }
            }
            else q.Enqueue(x);
        }
        while (st.Count() > 0)
        {
            q.Enqueue(st.Pop());
        }
        return q;
    }
    static List<string> conv3(string s)
    {
        var res = new List<string>();
        var sL = s.Length;
        var flag = false;
        var nn = "";
        for (int i = 0; i < sL; i++)
        {
            var w = s.Substring(i, 1);
            if (w == "*" | w == "/" | w == "+" | w == "-" | w == "(" | w == ")")
            {
                if (flag) { res.Add(nn); nn = ""; flag = false; }
                res.Add(w);
            }
            else if (w == "=" && flag) res.Add(nn);
            else { nn += w; flag = true; }
        }
        return res;
    }
}