forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_arithmetic.py
More file actions
204 lines (177 loc) · 4.76 KB
/
test_arithmetic.py
File metadata and controls
204 lines (177 loc) · 4.76 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# encoding: utf-8
"""
大数运算的实现
"""
__author__ = 'zhanghe'
def add(x, y):
"""
加法
"""
result = [int(p)+int(q) for i_p, p in enumerate(('0'*(max(len(x), len(y))-len(x))+str(x))[::-1]) for i_q, q in enumerate(('0'*(max(len(x), len(y))-len(y))+str(y))[::-1]) if i_p == i_q]
for index, v in enumerate(result):
if v >= 10:
result[index], k = v % 10, v / 10
if index+1 < max(len(str(x)), len(str(y))):
result[index+1] += k
else:
result.append(k)
return ''.join([str(i) for i in result][::-1])
def multiply(x, y):
"""
乘法
"""
i, result = 0, '0'
for m in [int(m) for m in x][::-1]:
k, t = 0, []
t.extend([0]*i)
for n in [int(n) for n in y][::-1]:
t.append((int(m)*int(n)+k) % 10)
k = (int(m)*int(n)+k) / 10
i += 1
if k > 0:
t.append(k)
result = add(result, ''.join([str(item) for item in t[::-1]]))
return result
def split_exp(exp_str):
"""
分解运算表达式
"""
r_list = []
t_list = []
for i in exp_str:
if i.isdigit() or i == '.':
t_list.append(i)
elif t_list:
r_list.append(''.join(t_list))
t_list = []
r_list.append(i)
else:
r_list.append(i)
if t_list:
r_list.append(''.join(t_list))
del t_list[:]
return r_list
def get_reversed_polish(exp):
"""
获取逆波兰式
"""
priority = {
'(': 1,
')': 1,
'*': 2,
'/': 2,
'%': 2,
'+': 3,
'-': 3,
}
r_list = []
s_list = []
for i in exp:
if i in priority.keys():
if i == '(' or not s_list:
s_list.append(i)
elif i == ')':
for sign in s_list[::-1]:
if sign == '(':
s_list.pop()
break
else:
r_list.append(s_list.pop())
else:
for sign in s_list[::-1]:
if priority.get(i) > sign:
r_list.append(s_list.pop())
else:
s_list.append(i)
break
elif type(eval(i)) in [int, float]:
r_list.append(i)
while s_list:
r_list.append(s_list.pop())
return r_list
def calculate_exp(exp_str):
"""
计算表达式
"""
symbol = ['+', '-', '*', '/', '%']
exp_list = split_exp(exp_str) # 分解表达式
exp = get_reversed_polish(exp_list) # 获取逆波兰式
s_list = []
for i in exp:
if i in symbol:
s_list.append(eval('%s %s %s' % (s_list.pop(-2), i, s_list.pop(-1))))
elif type(eval(i)) in [int, float]:
s_list.append(i)
return s_list[0]
def test_get_reversed_polish():
"""
测试逆波兰式
"""
exp_01 = '2+3*(5-2)' # 2 3 5 2 - * +
print get_reversed_polish(exp_01)
exp_02 = '2*(1+2/2)' # 2 1 2 2 / + *
print get_reversed_polish(exp_02)
exp_03 = '2*(1+2/2)*(1+2)' # 2 1 2 2 / + 1 2 + * *
print get_reversed_polish(exp_03)
exp_04 = '(2+0.5)*(1+2/2)*(1+2)' # 2.5 1 2 2 / + 1 2 + * *
print get_reversed_polish(exp_04)
def test_expression():
"""
表达式测试
"""
exp_01 = '2+3*(5-2)'
print calculate_exp(exp_01)
print eval(exp_01)
exp_02 = '2*(1+2/2)'
print calculate_exp(exp_02)
print eval(exp_02)
exp_03 = '2*(1+2/2)*(1+2)'
print calculate_exp(exp_03)
print eval(exp_03)
# 小数测试
exp_04 = '(2+0.5)*(1+2/2)*(1+2)'
print calculate_exp(exp_04)
print eval(exp_04)
def test_add():
"""
加法测试
"""
# 连续进位
a = '8838'
b = '3968'
print add(a, b)
print int(a)+int(b)
a_1 = '821111111111111181'
b_1 = '94222222222222226200'
print add(a_1, b_1)
print int(a_1)+int(b_1)
a_2 = '8211111111111333311181'
b_2 = '94222222222222226200'
print add(a_2, b_2)
print int(a_2)+int(b_2)
# 和的长度超过原始数值
a_3 = '82111111111116611181'
b_3 = '94222222222222226200'
print add(a_3, b_3)
print int(a_3)+int(b_3)
def test_multiply():
"""
乘法测试
"""
a_1 = '821111111111111181'
b_1 = '94222222222222226200'
print multiply(a_1, b_1)
print int(a_1)*int(b_1)
a_2 = '8211111111111333311181'
b_2 = '94222222222222226200'
print multiply(a_2, b_2)
print int(a_2)*int(b_2)
a_3 = '82111111111116611181'
b_3 = '94222222222222226200'
print multiply(a_3, b_3)
print int(a_3)*int(b_3)
if __name__ == '__main__':
# test_add()
# test_multiply()
# test_get_reversed_polish()
test_expression()