50字范文,内容丰富有趣,生活中的好帮手!
50字范文 > python的加减乘除运算_python实现四则运算

python的加减乘除运算_python实现四则运算

时间:2023-03-06 16:24:40

相关推荐

python的加减乘除运算_python实现四则运算

/szh123/four_arithmetic_implementation.git

1.需求分析:

实现四则运算题目及答案生成,控制生成题目的个数,题目中数值不超过10且有分数存在并用真分数表示。

可以生成10000道题

运算符不超过3个

题目不能出现重复即不能经过有限次交换成为相同题目

可以提供对给出的题目进行查重及答案求解并输出结果

2.功能设计:

(1)基础功能:实现四则运算题目和答案的生成,对生成的四则运算题目进行查重,支持对提供的题目进行查重和答案求解并给出正确错误及题目重复的结果

(2)扩展功能:

(3)高级功能:

3.设计实现:

类1:生成随机数

类2:生成整个表达式

类3:表达式转换成为逆波兰式

类4:树

类5:类中存放一个数的分子与分母,同时toString方法输出真分数形式

类6:计算结果

类7:二叉树的查重

类8:主函数

4.代码说明:

生成表达式

def createarithmetic(self):

list = []

f1 = function1.function1()

f2 = function2()

operator_no = random.randint(1,3)

if operator_no == 1:

list.append(f1.createNum())

list.append(f2.createOperator())

list.append(f1.createNum())

elif operator_no == 2:

start = random.randint(0,2)

end = 0

if start == 0:

end == 0

else:

end = start +1

for i in range(1,4):

if i == start:

list.append("(")

list.append(f1.createNum())

if i == end:

list.append(")")

list.append(f2.createOperator())

list.pop()

elif operator_no == 3:

start = random.randint(0, 3)

end = 0

if start == 0:

end == 0

else:

end = start + 1 + random.randint(0,1)

if end >= 4:

end=4

for i in range(1, 5):

if i == start:

list.append("(")

list.append(f1.createNum())

if i == end:

list.append(")")

list.append(f2.createOperator())

list.pop()

else:

list.append(f1.createNum())

list.append(f2.createOperator())

list.append(f1.createNum())

return list

逆波兰式生成

def toRPN(self,list):

right = []

aStack = []

position = 0

while True:

if self.isOperator(list[position]):

if list ==[] or list[position] == "(" :

aStack.append(list[position])

else:

if list[position] == ")":

while True:

if aStack != [] and aStack[-1] !="(" :

operator = aStack.pop()

right.append(operator)

else :

if aStack !=[]:

aStack.pop()

break

else:

while True:

if aStack != [] and self.priority(list[position],aStack[-1]):

operator = aStack.pop()

if operator != "(":

right.append(operator)

else:

break

aStack.append(list[position])

else:

right.append(list[position])

position = position +1

if position >= len(list):

break

while aStack != []:

operator = aStack.pop()

if operator != "(":

right.append(operator)

return right

5.测试运行:

主界面

选择功能1:

题目

答案

选择功能2:

6.PSP

7.小结:

逆波兰式的生成和计算,二叉树的查重非常方便

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。