四则运算作业

一、作业信息

软件工程 软件工程
作业要求 作业要求
作业目标 学会使用markdown语法攥写博客,对题目需求进行分析并实现
学号 3180701227

二、题目要求

写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:

1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24

2)程序要求能处理用户的输入,判断对错,累积分数

3)程序支持可以由用户自行选择加、减、乘、除运算

4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目

三、代码

`import random

四则运算

def szys():

sym = ['+', '-', '×', '÷']

f= random.randint(0, 3)

n1 = random.randint(1, 20)

n2 = random.randint(1, 20)

result = 0

if f== 0:#加法

   result  = n1 + n2

elif f == 1:#减法,要先比较大小,防止输出负数

    n1, n2 = max(n1, n2), min(n1, n2)

    result  = n1 - n2

elif f== 2:#乘法

    result  = n1 * n2

elif f == 3:#除法,要比较大小,并循环取整除

    n1, n2 = max(n1, n2), min(n1, n2)

    while n1 % n2 != 0:

        n1 = random.randint(1, 10)

        n2 = random.randint(1, 10)

        n1, n2 = max(n1, n2), min(n1, n2)

    result  = int(n1 / n2)

print(n1, sym[f], n2, '= ', end='')

return result

制作题库

def test():

sym = ['+', '-', '×', '÷']

print('输入所需要的题目数量')

n=int(input())

result =[]

m=0

while m<=(n-1):

    print(m+1,end='、')

    result .append(szys())

    print(' ')

    m=m+1

m=0

print('对应的答案:')

while m<=(n-1):

    print(m+1,'、',result [m])

    m=m+1

print('选择想要的模式')

print('1、进行四则运算')

print('2、制作题库')

n=int(input())

当输入1时,进行四则运算,调用函数syzs()

if n==1:

while True:

    result  = szys()

    j= input()

    s= int(j)

    if s== result :

        print('right')

    else:

        print('error.,the answer is', result )

当输入2时,进行制作题库

if n==2:

 test()`

先运行第一个模式----进行四则运算,结果:

四、总结

  这次作业我是用python实现的,但由于我python学得还不是很精,所以我参考了网上的代码,基本上实现了四则运算,也方便了教师及时提取题库,但由于时间不充分,所以在实现真分数上,没有达到预期,这点还需要在后续中进行改进。
原文地址:https://www.cnblogs.com/ahpu/p/13946420.html