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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
| #! /usr/local/bin/python3.6
"""
Computation of big-digit values (Ver.2)
"""
import random
import sys
import traceback
class CalcBigDigits:
N_A = 1000
N_B = 996
LIMIT = 4
RANGE = 10**LIMIT
SIZE_A = int((N_A-1) / LIMIT) + 1
SIZE_B = int((N_B-1) / LIMIT) + 1
def __init__(self):
self.a = [random.randrange(self.RANGE) for _ in range(self.SIZE_A)]
self.b = [random.randrange(self.RANGE) for _ in range(self.SIZE_B)]
self.c = random.randrange(self.RANGE)
print("A =")
self.__display(self.a)
print("B =")
self.__display(self.b)
print("C =\n{:04d}\n\n".format(self.c))
def compute_add(self):
""" Addition """
try:
print("A + B =")
self.__display(self.__long_add(self.a, self.b))
except Exception as e:
raise
def compute_sub(self):
""" Subtraction """
try:
print("A - B =")
self.__display(self.__long_sub(self.a, self.b))
except Exception as e:
raise
# 計算 ( 乗算 )
def compute_mul(self):
""" Multiplication """
try:
print("A * C =")
self.__display(self.__long_mul(self.a, self.c))
except Exception as e:
raise
# 計算 ( 除算 )
def compute_div(self):
""" Division """
try:
print("A / C =")
self.__display(self.__long_div(self.a, self.c))
except Exception as e:
raise
def __long_add(self, a, b):
""" Computation of long + long
:param list a
:param list b
:return list z
"""
try:
z = [0 for _ in range(max(len(a), len(b)) + 1)]
for i in range(len(a)):
z[i] = a[i]
for i in range(len(b)):
z[i] += b[i]
if z[i] >= 10**self.LIMIT:
z[i] -= 10**self.LIMIT
z[i + 1] += 1
return z
except Exception as e:
raise
def __long_sub(self, a, b):
""" Computation of long - long
:param list a
:param list b
:return list z
"""
try:
z = [0 for _ in range(max(len(a), len(b)))]
for i in range(len(a)):
z[i] = a[i]
for i in range(len(b)):
z[i] -= b[i]
if z[i] < 0:
z[i] += 10**self.LIMIT
z[i + 1] -= 1
return z
except Exception as e:
raise
def __long_mul(self, a, c):
""" Computation of long * short
:param list a
:param list b
:return list z
"""
try:
z = [0 for _ in range(len(a) + 1)]
for i in range(len(a)):
z[i] += a[i] * c
if z[i] >= 10**self.LIMIT:
z[i + 1] += int(z[i] / 10**self.LIMIT)
z[i] %= 10**self.LIMIT
return z
except Exception as e:
raise
def __long_div(self, a, c):
""" Computation of long / short
:param list a
:param list b
:return list z
"""
try:
z = [0 for _ in range(len(a))]
for i in reversed(range(len(a))):
z[i] += int(a[i] / c)
if i > 0:
a[i - 1] += (a[i] % c) * 10**self.LIMIT
return z
except Exception as e:
raise
def __display(self, s):
""" Display
:param list s
"""
try:
size = len(s)
if s[size - 1] == 0:
size -= 1
for i in reversed(range(size)):
print("{:04d} ".format(s[i]), end="")
if (size - i) % 10 == 0 and i != 0:
print()
print("\n")
except Exception as e:
raise
if __name__ == '__main__':
try:
obj = CalcBigDigits()
obj.compute_add()
obj.compute_sub()
obj.compute_mul()
obj.compute_div()
except Exception as e:
traceback.print_exc()
sys.exit(1)
|