18_homework.py 419 B

12345678910111213
  1. #!/usr/bin/env python3
  2. import re
  3. import sys
  4. class Term(int):
  5. def __mul__(self, other): return Term(int.__mul__(self, other))
  6. def __add__(self, other): return Term(int.__add__(self, other))
  7. __sub__ = __mul__
  8. __pow__ = __add__
  9. exprs = re.sub(r'(\d+)', r'Term(\1)', sys.stdin.read()).splitlines()
  10. print(sum(eval(e.replace('*', '-')) for e in exprs))
  11. print(sum(eval(e.replace('+', '**')) for e in exprs))