fork download
  1. #! /usr/bin/env python
  2. ## spoj.pl/problems/ARITH
  3. ## 31.07.2011 cmiN
  4.  
  5. from decimal import getcontext, Decimal
  6.  
  7. def init():
  8. #from sys import stdin
  9. #stdin = open("arith.in", "rt")
  10. getcontext().prec = 1024
  11.  
  12. def add():
  13. global second
  14. second = sub + second
  15. res = str(Decimal(first) + Decimal(second))
  16. sep1 = max(len(first), len(second))
  17. sep2 = max(sep1, len(res))
  18. pat = "%" + str(sep2) + "s"
  19. print pat % first
  20. print pat % second
  21. print pat % ("-" * sep1)
  22. print pat % res
  23.  
  24. def mul():
  25. res = str(Decimal(first) * Decimal(second))
  26. sep1 = max(len(first), len(second) + 1)
  27. sep2 = max(sep1, len(res))
  28. pat = "%" + str(sep2) + "s"
  29. print pat % first
  30. print pat % ("*" + second)
  31. print pat % ("-" * sep1)
  32. seclen = len(second)
  33. if seclen > 1:
  34. for i in xrange(1, seclen + 1):
  35. print ("%" + str(sep2 - i + 1) + "s") % (int(second[-i]) * Decimal(first))
  36. print "-" * sep2
  37. print pat % res
  38.  
  39. def process():
  40. global first, second, sub
  41. tests = input()
  42. while tests:
  43. var = raw_input()
  44. for sub in var:
  45. if not sub.isdigit():
  46. break
  47. (first, second) = var.split(sub)
  48. if sub in "+-":
  49. add()
  50. else:
  51. mul()
  52. print
  53. tests -= 1
  54.  
  55. def main():
  56. init()
  57. process()
  58.  
  59. if __name__ == "__main__":
  60. main()
  61.  
Success #stdin #stdout 0.03s 7284KB
stdin
4
999+1
1208-1100
1*0
1337*543
stdout
 999
  +1
 ---
1000

 1208
-1100
-----
  108

 1
*0
--
 0

  1337
  *543
  ----
  4011
 5348
6685
------
725991