python实现四则运算

GitHub链接:

https://github.com/history5201/python

在程序的各个模块的开发上耗费的时间PSP表格:

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning 计划  30 42 
· Estimate · 估计这个任务需要多少时间  30 42 
Development 开发  305  395
· Analysis · 需求分析 (包括学习新技术)  20 30 
· Design Spec · 生成设计文档  25 30 
· Design Review · 设计复审 (和同事审核设计文档)  10 15 
· Coding Standard · 代码规范 (为目前的开发制定合适的规范)  15 25 
· Design · 具体设计  30 45 
· Coding · 具体编码  150 180 
· Code Review · 代码复审  30 35 
· Test · 测试(自我测试,修改代码,提交修改)  25 35 
Reporting 报告  100  110
· Test Report · 测试报告  70 65 
· Size Measurement · 计算工作量  15 25 
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划  15 20 
合计    435  547

 

3)解题思路描述

  在百度以及GitHub上寻找适合的报告书作为参考以及参考网络代码进行修改

 

4)设计实现过程:

  1、定义三个函数:出题函数、判断函数、主函数

  2、出题函数:在字符数组[+ - * /]中随机抽取两个或一个符号以及0-30之间抽取随机数进行四则运算

  3、判断函数:判断同学从键盘内输入的答案是否正确;

  4、主函数:输入一个数值N,以便学生选择做几题,然后循环出题以及判断函数。

 

流程图如下所示

 

 

5)代码说明:

import random
from fractions import Fraction


##两个整数的四则运算
def c1(q, ans):
    symbol = random.choice(['+', '-', '*', '/'])  # 生成随机符号
    if symbol == '+':
        n1 = random.randint(0, 20)
        n2 = random.randint(0, 20)
        q.append(str(n1) + '+' + str(n2) + '=')
        ans.append(n1 + n2)
    elif symbol == '-':
        n1 = random.randint(0, 20)
        n2 = random.randint(0, 20)
        n1,n2 = max(n1,n1),min(n1,n2)#防止出现负数
        q.append(str(n1) + '-' + str(n2) + '=')
        ans.append(n1 - n2)
    elif symbol == '*':
        n1 = random.randint(0, 20)
        n2 = random.randint(0, 20)
        q.append(str(n1) + '×' + str(n2) + '=')
        ans.append(n1 * n2)
    else:
        n1 = random.randint(0, 20)
        if n1 == 0:
            n2 = random.randint(1, 20)
        else:
            n2 = random.randint(1, n1 + 1)
        q.append(str(n1) + '÷' + str(n2) + '=')
        ans.append(Fraction(n1, n2))

##随机生成两个分数
def createF():
    fz1 = random.randint(0, 20)
    if fz1 == 0:
        fm1 = random.randint(1, 20)
    else:
        fm1 = random.randint(1, 20)
    f1 = Fraction(fz1, fm1)
    fz2 = random.randint(1, 20)
    fm2 = random.randint(20, 20)
    f2 = Fraction(fz2, fm2)
    return f1, f2

def f(f):#分数的转换
    a=f.numerator #分子
    b=f.denominator #分母
    if a%b==0:#为整数
        return '%d'%(a/b)
    elif a<b:#为真分数
        return '%d%s%d' % (a,'/',b)
    else:#为带分数
        c=int(a/b)
        a = a - c * b
        return '%d%s%d%s%d' % (c,'',a,'/',b)


##两个分数的四则运算
def c2(q,ans):
    symbol = random.choice(['+','-','*','/'])
    f1,f2 = createF()
    if symbol =='+':
        while f1+f2>1:
            f1,f2 = createF()
        q.append(str(f1)+'+'+str(f2)+'=')
        ans.append(f1+f2)
    elif symbol =='-':
        f1,f2 = max(f1,f2),min(f1,f2)#防止出现负数
        q.append(str(f1)+'-'+str(f2)+'=')
        ans.append(f1-f2)
    elif symbol == '*':
        while f1*f2>1:
            f1,f2 = createF()
        q.append(str(f1)+'×'+str(f2)+'=')
        ans.append(f1*f2)
    else:
        while f1/f2>1:
            f1,f2=createF()
        q.append(str(f1)+'÷'+str(f2)+'=')
        ans.append(Fraction(f1,f2))



def main():
    while 1:
        print("输入题目的数量", end='  ')
        k = int(input())
        p = 100 / k
        s = 0
        q = []
        ans = []
        ans2 = []
        for i in range(k):
            n = random.randint(1, 4)
            if n == 1:
                c1(q, ans)
                g = Fraction(ans[i])
                ans2.append(f(g))
            else:
                c2(q, ans)
                g = Fraction(ans[i])
                ans2.append(f(g))#记录带分数答案
        for i in range(k):
            print("第{}题:{}".format(i + 1, q[i]), end="  ")
            a = input()
            if a == str(ans[i]):
                s = s + p
        print("所得的分数为:{}".format(s))
        print("正确答案:", end="  ")
        for i in range(k):
            if str(ans[i]) == str(ans2[i]):
                print(q[i] + str(ans[i]))
            else:
                print("{}{}或{}".format(q[i],str(ans2[i]),str(ans[i])))

if __name__ == '__main__':
    main()

 

6)测试运行:

 

 

7)你程序中消耗最大的函数

该函数所需要的空间复杂度最大

 

def c1(q, ans):
    symbol = random.choice(['+', '-', '*', '/'])  # 生成随机符号
    if symbol == '+':
        n1 = random.randint(0, 20)
        n2 = random.randint(0, 20)
        q.append(str(n1) + '+' + str(n2) + '=')
        ans.append(n1 + n2)
    elif symbol == '-':
        n1 = random.randint(0, 20)
        n2 = random.randint(0, 20)
        n1,n2 = max(n1,n1),min(n1,n2)#防止出现负数
        q.append(str(n1) + '-' + str(n2) + '=')
        ans.append(n1 - n2)
    elif symbol == '*':
        n1 = random.randint(0, 20)
        n2 = random.randint(0, 20)
        q.append(str(n1) + '×' + str(n2) + '=')
        ans.append(n1 * n2)
    else:
        n1 = random.randint(0, 20)
        if n1 == 0:
            n2 = random.randint(1, 20)
        else:
            n2 = random.randint(1, n1 + 1)
        q.append(str(n1) + '÷' + str(n2) + '=')
        ans.append(Fraction(n1, n2))

##随机生成两个分数
def createF():
    fz1 = random.randint(0, 20)
    if fz1 == 0:
        fm1 = random.randint(1, 20)
    else:
        fm1 = random.randint(1, 20)
    f1 = Fraction(fz1, fm1)
    fz2 = random.randint(1, 20)
    fm2 = random.randint(20, 20)
    f2 = Fraction(fz2, fm2)
    return f1, f2

 

posted @ 2020-09-21 15:25  辰光依旧  阅读(3171)  评论(0编辑  收藏  举报