output_description: For each dataset, print the value in a line.
input_description: The input consists of multiple datasets. Each dataset is given in the
  following format.
  
      
      
      a op b
      
  
  The input ends with a dataset where op = '?'. Your program should not process
  for this dataset.
attempt: import fractions
  import re
  
  mode = "fractions"
  symbols = {"$": 0, "+": 1, "-": 1, "*": 2, "/": 2, "%": 2, "^": 3}
  operations = {
      "+": (lambda a, b: a + b),
      "-": (lambda a, b: a - b),
      "*": (lambda a, b: a * b),
      "/": (lambda a, b: a / b),
      "%": (lambda a, b: a % b),
      "^": (lambda a, b: a**b),
  }
  
  
  def insert(string, char, target):
      return string[:target] + char + string[target:]
  
  
  def add_blank(formula):
      targets = []
      for n, i, j in zip(range(1, len(formula)), formula[:-1], formula[1:]):
          if i.isnumeric() != j.isnumeric():
              targets.append(n)
      n = 0
      for i in targets:
          formula = insert(formula, " ", i + n)
          n += 1
      return formula
  
  
  def to_rpn(str_formula):
      op_stack = []
      result_list = []
      for i in str_formula.split():
          if i.isnumeric():
              result_list.append(i)
          elif not op_stack or symbols[op_stack[-1]] < symbols[i]:
              op_stack.append(i)
          else:
              while op_stack and not symbols[op_stack[-1]] < symbols[i]:
                  result_list.append(op_stack.pop())
              op_stack.append(i)
      result_list.extend(op_stack[::-1])
      return " ".join(result_list)
  
  
  def calculate_rpn(rpn_formula):
      result_stack = []
      for i in rpn_formula.split():
          if i in operations.keys():
              a = result_stack.pop()
              b = result_stack.pop()
              result_stack.append(operations[i](b, a))
          elif i == "$":
              a = result_stack.pop()
              result_stack.append(a)
          else:
              result_stack.append(fractions.Fraction(i))
      result = result_stack[0]
      if result.denominator == 1:
          return int(result)
      elif mode == "float":
          return float(result)
      elif mode == "fractions":
          return result
  
  
  def calculate(formula):
      regex = re.compile(r"\([^()]*\)")
      while regex.search(formula):
          for i in regex.finditer(formula):
              formula = formula.replace(
                  i[0], str(calculate_rpn(to_rpn(add_blank(i[0][1:-1]))))
              )
      return calculate_rpn(to_rpn(add_blank(formula)))
  
  
  def repl():
      global mode
      while True:
          s = input()
          if not s:
              break
          elif s.split()[0] == ":mode":
              mode = s.split()[1]
          else:
              print(calculate(s))
  
  
  if __name__ == "__main__":
      repl()
  
problem_description: Simple Calculator
  
  Write a program which reads two integers a, b and an operator op, and then
  prints the value of a op b.
  
  The operator op is '+', '-', '*' or '/' (sum, difference, product or
  quotient). The division should truncate any fractional part.
samples: [{"input": "1 + 2\n    56 - 18\n    13 * 2\n    100 / 10\n    27 + 81\n    0 ? 0", "output": "3\n    38\n    26\n    10\n    108"}]