自动生成小学四则运算题目

github地址:https://github.com/ysd973/-

题目要求:
能自动生成小学四则运算题目(注意是给小学生用的,要是结果出现负数的话他们会迷茫的!)
除了整数外,还要支持真分数的四则运算
工具清单:
编程语言:Python

 解题思路:

1、随机生成运算符个数

2、按照运算符个数随机生成运算符

3、对于真分数的运算和表示只能查阅资料

功能设计:

(1)基础功能:实现四则运算题目的自动生成,并打印出题目的答案

(2)扩展功能:实现菜单操作

程序设计:

(1)四则运算加减乘除,采用两个随机数,由于不能出现负数,所以在对两个随机数进行减法运算的时候,需要进行比较大小,而除法在运算中,除数不能取0。

(2)进行真分数运算应注意假分数的影响

(3)定义函数:用def szys()实现随机生成四则运算

(4)代码分为两部分,第一部分是随机生成四则运算函数,程序输出算式并返回正确答案;第二部分是主函数,用户输入数字1进行四则运算,通过调用syzs()函数得到算式和返回值,用户输入答案后,便与用户输入值进行比较。

 

 源代码:

 

import profile #测试性能
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

print('输入数字1进行四则运算')

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 )
    

  

测试运行:

 性能测试:

ncall:函数运行次数

tottime: 函数的总的运行时间,减去函数中调用子函数的运行时间

第一个percall:percall = tottime / nclall 

cumtime:函数及其所有子函数调整的运行时间,也就是函数开始调用到结束的时间。

第二个percall:percall = cumtime / nclall 

 

PSP 2.1表格

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning 计划 20 30
· Estimate · 估计这个任务需要多少时间 40 40
Development 开发 40 50
· Analysis · 需求分析 (包括学习新技术) 15 20
· Design Spec · 生成设计文档 25 30
· Design · 具体设计 30 30 
· Coding · 具体编码 40 50
· Test · 测试(自我测试,修改代码,提交修改)  30 40
Reporting 报告  20 30
合计   260 320

 

为了方便测试程序性能,我将profile运行直接加入到代码中:

 
import profile
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

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 )
if n==2:
    profile.run('szys()')
    

 运行代码:

 

 

总结:

由于对python学艺不精,所以此次的代码借鉴了网上的,对于真分数部分没能按要求弄出来,下次会继续努力

posted @ 2020-09-20 22:21  ysd将暮未暮  阅读(195)  评论(0编辑  收藏  举报