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


object Main extends App{
  import scala.io.StdIn.{readLine}
  type Operator = Option[((Int, Int) ⇒ Int, List[Char])]
  type CharParseResult = Option[(Char, List[Char])]
  type ParseResult = Option[(List[Char], List[Char])]
  type CalResult = Option[(Int, List[Char])]
  type CalMidResult = Option[(Int ⇒ Int, List[Char])]
  for (_ ← 0 until readLine().toInt){
    println(readLine().toList match {
      case value AsExpression '='::Nil ⇒ value
      case _ ⇒ ???
    })
  }
  object AsDigit{
    def unapply(arg: List[Char]):CharParseResult = arg match{
      case h::t if h.isDigit ⇒ Some(h, t)
      case _ ⇒ None
    }
  }
  object AsNumber{
    def unapply(arg: List[Char]):ParseResult = arg match{
      case digit AsDigit (number AsNumber rest) ⇒ Some(digit::number, rest)
      case digit AsDigit rest ⇒ Some(digit::Nil, rest)
      case _ ⇒ None
    }
  }
  object AsParenthesis{
    def unapply(arg: List[Char]): CalResult = arg match{
      case '(':: (result AsExpression ')':: rest)⇒ Some(result, rest)
      case _ ⇒ None
    }
  }
  object AsTerm{
    def unapply(arg: List[Char]):CalResult = arg match{
      case number AsNumber rest ⇒ Some(number.mkString.toInt, rest)
      case value AsParenthesis rest ⇒ Some(value, rest)
      case _ ⇒ None
    }
  }
  object AsHighPriorityOperator{
    def unapply(arg: List[Char]):Operator = arg match{
      case '*'::rest ⇒ Some(_ * _ , rest)
      case '/'::rest ⇒ Some(_ / _, rest)
      case _ ⇒ None
    }
  }
  object AsLowPriorityOperator{
    def unapply(arg: List[Char]):Operator = arg match{
      case '+'::rest ⇒ Some(_ + _, rest)
      case '-'::rest ⇒ Some(_ - _, rest)
      case _ ⇒ None
    }
  }
  implicit class BinomialOperator(val operator:(Int, Int) ⇒ Int){
    def <|(num:Int):Int ⇒ Int = operator(_, num)
  }
  implicit class MonomialOperator(val number:Int){
    def |>(op:Int ⇒ Int):Int = op(number)
  }
  object AsHighPriorityOperation{
    def unapply(arg: List[Char]):CalMidResult = arg match{
      case op AsHighPriorityOperator (term AsTerm (converter AsHighPriorityOperation rest)) ⇒ Some(op <| term andThen converter, rest)
      case op AsHighPriorityOperator (term AsTerm rest) ⇒ Some(op <| term, rest)
      case _ ⇒ None
    }
  }
  object AsHighPriorityPartial{
    def unapply(arg: List[Char]):CalResult = arg match{
      case term AsTerm (converter AsHighPriorityOperation rest) ⇒ Some(term |> converter, rest)
      case term AsTerm rest ⇒ Some(term, rest)
      case _ ⇒ None
    }
  }
  object AsLowPriorityOperation{
    def unapply(arg: List[Char]):CalMidResult = arg match{
      case op AsLowPriorityOperator (partial AsHighPriorityPartial (converter AsLowPriorityOperation rest)) ⇒ Some(op <| partial andThen converter, rest)
      case op AsLowPriorityOperator (partial AsHighPriorityPartial rest) ⇒ Some(op <| partial, rest)
      case _ ⇒ None
    }
  }
  object AsExpression{
    def unapply(arg: List[Char]): CalResult = arg match{
      case left AsHighPriorityPartial (right AsLowPriorityOperation rest) ⇒ Some(left |> right, rest)
      case value AsHighPriorityPartial rest ⇒ Some(value, rest)
      case _ ⇒ None
    }
  }
}