Simple Arithmetics

def Add(a, b):
l = []
alen = len(a)
blen = len(b)
result = str(int(a) + int(b))
relen = len(result)
llen = alen > blen and alen or blen+1
llen = relen > llen and relen or llen
l.append(' ' * (llen - alen) + a)
l.append(' ' * (llen - blen - 1) + '+' + b)
l.append('-' * llen)
l.append(' ' * (llen - relen) + result)
List.append(l) def Sub(a, b):
l = []
alen = len(a)
blen = len(b)
result = str(int(a) - int(b))
relen = len(result)
llen = alen > blen and alen or blen+1
l.append(' ' * (llen - alen) + a)
l.append(' ' * (llen - blen - 1) + '-' + b)
l.append('-' * llen)
l.append(' ' * (llen - relen) + result)
List.append(l) def Multi(a, b):
l = []
alen = len(a)
blen = len(b)
result = str(int(a) * int(b))
relen = len(result)
llen = relen > blen+1 and relen or blen+1
_len_bove = alen > blen and alen or blen+1
_len_below = len(str(int(a) * int(b[-1])))
_len = _len_bove > _len_below and _len_bove or _len_below
l.append(' ' * (llen - alen) + a)
l.append(' ' * (llen - blen - 1) + '*' + b)
l.append(' ' * (llen - _len) + '-' * _len)
for i in range(blen):
cpt = str(int(a) * int(b[-i-1]))
l.append(' ' * (llen - len(cpt) - i) + cpt)
if blen != 1:
l.append('-' * llen)
l.append(' ' * (llen - relen) + result)
List.append(l) n = input()
List = []
for i in range(n):
string = raw_input()
if '+' in string:
a, b = string.split('+')
Add(a, b)
elif '-' in string:
a, b = string.split('-')
Sub(a, b)
elif '*' in string:
a, b = string.split('*')
Multi(a, b) for l in List:
for i in l:
print i
print
上一篇:PHP缓冲区强制及时输出


下一篇:android90 bind方式启动服务service调用service里的方法