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;
using System.Text;
using System.Collections.Generic;
using System.Linq;

static class SmartCalculator
{
   private class EquationNode
   {
      public EquationNode left;
      public EquationNode right;

      public int value { get { return GetValue(this); } }
      public Func<EquationNode, int> GetValue;

      public static EquationNode Constant(int value)
      {
         return new EquationNode{ GetValue = x => value };
      }
      public static EquationNode ReferenceRight()
      {
         return new EquationNode{ GetValue = x => x.right.value };
      }
   }

   public static int Calculate(string equation)
   {
      return MakeTree(equation).value;
   }

   private static EquationNode MakeTree(string equation)
   {
      var root = EquationNode.ReferenceRight();
      root.right = EquationNode.ReferenceRight();
      var node = root;

      EquationNode valueNode = null;
      for (int index = 0; index < equation.Length; index++)
      {
         char word = equation[index];
         switch (word)
         {
            case '+':
               node.right.right = valueNode;
               root = new EquationNode
               {
                  left = root,
                  right = EquationNode.ReferenceRight(),
                  GetValue = x => x.left.value + x.right.value,
               };
               node = root;
               break;
            case '-':
               node.right.right = valueNode;
               root = new EquationNode
               {
                  left = root,
                  right = EquationNode.ReferenceRight(),
                  GetValue = x => x.left.value - x.right.value,
               };
               node = root;
               break;
            case '*':
               node.right.right = valueNode;
               node.right = new EquationNode
               {
                  left = node.right,
                  GetValue = x => x.left.value * x.right.value,
               };
               break;
            case '/':
               node.right.right = valueNode;
               node.right = new EquationNode
               {
                  left = node.right,
                  GetValue = x => x.left.value / x.right.value,
               };
               break;
            case '(':
               {
                  int nest = 1;
                  int end;
                  for (end = index + 1; nest > 0; end++)
                  {
                     switch (equation[end])
                     {
                        case '(':
                           nest++;
                           break;
                        case ')':
                           nest--;
                           break;
                     }
                  }
                  var innerEquation = equation.Substring(index + 1, (end - 1) - (index + 1));
                  valueNode = MakeTree(innerEquation);
                  index = end - 1;
                  break;
               }
            default:
               // 数値
               {
                  var end = index;
                  while (end < equation.Length && '0' <= equation[end] && equation[end] <= '9')
                  {
                     end++;
                  }
                  var innerEquation = equation.Substring(index, end - index);
                  int value = int.Parse(innerEquation);
                  valueNode = EquationNode.Constant(value);
                  index = end - 1;
                  break;
               }
         }
      }

      node.right.right = valueNode;

      return root;
   }
}

class MainClass
{
   static void Main(string[] args)
   {
      var stringBuilder = new StringBuilder();

      int n = int.Parse(Console.ReadLine());
      for (; n > 0; n--)
      {
         var equation = Console.ReadLine().TrimEnd('=');
         var result = SmartCalculator.Calculate(equation);

         stringBuilder.AppendLine(result.ToString());
      }

      Console.Write(stringBuilder.ToString());
   }
}