1 #!/usr/bin/env python 2 # 3 # This solution performs all addition and subtraction with Python ints/longs; 4 # longs have no size limit. 5 6 __author__ = 'Matt Eastman' 7 8 import string 9 10 11 class CrazyInt(object): 12 _charset = string.digits + string.uppercase 13 _max = 0 # replaced after class definition 14 15 def __init__(self, value): 16 self._valid = True 17 if value is None: 18 self._valid = False 19 elif isinstance(value, str): 20 self._value = 0 21 negative = value.startswith('-') 22 if negative: 23 value = value[1:] 24 if len(value) >= 36: 25 self._valid = False 26 return 27 base = len(value) + 1 28 for c in value: 29 digit = self._charset.find(c) 30 if digit >= base: 31 self._valid = False 32 return 33 self._value = self._value * base + digit 34 base -= 1 35 if negative: 36 self._value *= -1 37 elif isinstance(value, (int, long)): 38 self._value = value 39 if abs(value) > self._max: 40 self._valid = False 41 elif isinstance(value, CrazyInt): 42 self._value = value._value 43 self._valid = value._valid 44 else: 45 raise TypeError() 46 47 def __str__(self): 48 if not self._valid: 49 return 'Invalid' 50 if self._value < 0: 51 return '-' + str(CrazyInt(-self._value)) 52 out = [] 53 n = self._value 54 while n: 55 digit_base = len(out) + 2 56 n, i = divmod(n, digit_base) 57 out.insert(0, self._charset[i]) 58 if not out: 59 out.append('0') 60 return ''.join(out) 61 62 def __add__(self, other): 63 if not (self._valid and other._valid): 64 return CrazyInt(None) 65 return CrazyInt(self._value + other._value) 66 67 def __sub__(self, other): 68 if not (self._valid and other._valid): 69 return CrazyInt(None) 70 return CrazyInt(self._value - other._value) 71 72 def __neg__(self): 73 if not self._valid: 74 return CrazyInt(self) 75 return CrazyInt(-self._value) 76 77 CrazyInt._max = CrazyInt(CrazyInt._charset[1:][::-1])._value 78 79 80 def main(): 81 for _ in xrange(input()): 82 pieces = raw_input().split() 83 assert len(pieces) == 3 84 a = CrazyInt(pieces[0]) 85 b = CrazyInt(pieces[2]) 86 op = pieces[1] 87 result = CrazyInt(None) 88 if op == '+': result = a + b 89 elif op == '-': result = a - b 90 print result 91 92 if __name__ == '__main__': 93 main()