# 20183411 李丞灏 2019-2020《Python程序设计》实验二 计算器设计 报告

## 2. 实验过程及结果

• 代码如下：*
import re
import math
d = [0]
f = 0
for k in range(0, len(c)):
if (c[k] == '-' or c[k] == '+'):
f += 1
if (c[k] == '-'):
d.append(f)
w = re.split('[+-]', c)
for item in w:
if (item.find('*') != -1):
y = w.index(item)
t = item.split('*')
n = 1
for i in t:
if (i.find('/') != -1):
q = t.index(i)
p = i.split('/')
h = 1
for bo in p:
h *= float(bo)
u = (float(p[0]) * float(p[0])) / h
t[q] = u
for i in t:
n *= float(i)
w[y] = str(n)
for item in w:
if (item.find('/') != -1):
Q = w.index(item)
P = item.split('/')
H = 1
for Bo in P:
H *= float(Bo)
U = (float(P[0]) * float(P[0])) / H
w[Q] = U
else:
continue
m = 0
r = 0
for item in w:
m += float(item)
for fi in d:
r += float(w[fi])
m = m - 2 * r + 2 * float(w[d[0]])
M = round(m,v)
return M
def mod(A1,B1):
y = A1%B1
return y
def equation(a2,b2,c2):
y = (c2 - b2)/c2
return y
def rou(r,v):
c = math.pi*r*2
C = round(c,v)
return C
def area(r,v):
s = math.pi*r**2
S = round(s, v)
return S
A = -1
while (A != 0):
A = int(input("欢迎使用至简计算器！\n请输入您想要实现的功能：\n1.普通计算  2.求余运算  3.计算一元一次方程"
"  4.计算圆面积和周长  0.退出""\n"))
if (A == 1):
c = str(input("请输入计算式：\n"))
v = int(input("请输入您想要保留的位数：\n"))
print(c + '=' + str(add(c, v)))
print("\n")
elif (A == 2):
print("A mod B\n")
A1 = int(input("请输入 A：\n"))
B1 = int(input("请输入 B：\n"))
print(str(A1)+' '+'mod'+' '+str(B1)+' '+'='+' '+str(mod(A1,B1)))
print("\n")
elif (A == 3):
print("ax + b = c\n")
a2 = float(input("请输入 a：\n"))
b2 = float(input("请输入 b：\n"))
c2 = float(input("请输入 c：\n"))
print(str(a2)+'x'+' '+'+'+' '+str(b2)+' '+'='+' '+str(equation(a2,b2,c2)))
print("\n")
elif (A == 4):
r = float(input("请输入半径：\n"))
v = int(input("请输入您想要保留的位数：\n"))
print("该圆的周长为：",rou(r,v))
print("该圆的面积为：",area(r,v))
print("\n")


## 其他（感悟、思考等）

*** 人生苦短，我用Python!!! ***

## 参考资料

posted @ 2020-04-16 11:12  李丞灏  阅读(242)  评论(0编辑  收藏