-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbig_number_computation.py
69 lines (55 loc) · 1.56 KB
/
big_number_computation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
def to_int(s):
try:
return int(s)
except ValueError:
print('Error: \'%s\' is not an integer.' % s, file=sys.stderr)
sys.exit(1)
def big_number_computation(a, o, b):
a_s = None
b_s = None
d_s = None
if '.' in a:
a_s, d_s = a.split('.')
b_s = b
elif '.' in b:
a_s = a
b_s, d_s = b.split('.')
else:
a_s = a
b_s = b
d_s = []
a_n = to_int(a_s)
b_n = to_int(b_s)
if len(d_s) > 0:
d_n = to_int(d_s)
else:
d_n = 0
if o == '+':
r = a_n + b_n
elif o == '-':
r = a_n - b_n
elif o == '*':
r = a_n * b_n
if o == '-' and d_n > 0:
z = int('1' + ('0' * (len(d_s))))
d_n = z - d_n
r += 1
if d_n > 0:
return '%d.%d' % (r, d_n)
else:
return str(r)
if __name__ == '__main__':
if len(sys.argv) < 4:
print('Not enough arguments to perform computation.', file=sys.stderr)
sys.exit(5)
elif sys.argv[2] != '+' and sys.argv[2] != '-' and sys.argv[2] != '*':
print('Error: Operator must be \'+\' or \'-\'', file=sys.stderr)
sys.exit(2)
if '.' in sys.argv[1] and '.' in sys.argv[3]:
print('Error: Only one value can have a decimal.', file=sys.stderr)
sys.exit(3)
elif ('.' in sys.argv[1] or '.' in sys.argv[3]) and sys.argv[2] == '*':
print('Error: No value can have a decimal point if multiplying.')
sys.exit(4)
print(big_number_computation(sys.argv[1], sys.argv[2], sys.argv[3]))