四则运算版本升级

目标:对上一次软件项目做进一步升级

一、项目要求:

  • 在上一次的项目基础上做一次升级
  • 至少选择一下一个方向:
    • 功能升级
    • 性能升级
    • 界面升级
    • 使用不同语言升级
       

二、项目改进情况:

1.改进选定方向:功能升级+性能升级

2.代码展示(版本2.0)【Github】

3.改进具体情况:

  • 优化了算式计算方式,不再出现负数情况
  • 优化算法效率,加快计算时间
  • 增加题目即时问答模式功能

三、PSP2.1表格:

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning 计划  20  16
· Estimate · 估计这个任务需要多少时间  20  16
Development 开发  245  313
· Analysis · 需求分析 (包括学习新技术)  60  46
· Design Spec · 生成设计文档  20  35
· Design Review · 设计复审 (和同事审核设计文档)  15  20
· Coding Standard · 代码规范 (为目前的开发制定合适的规范)  20  22
· Design · 具体设计  30  34
· Coding · 具体编码  60  84
· Code Review · 代码复审  20  31
· Test · 测试(自我测试,修改代码,提交修改)  20  41
Reporting 报告  50  68
· Test Report · 测试报告  20  33
· Size Measurement · 计算工作量  20  24
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划  10  11
合计    315  397

四、代码展示:

1.关键函数result(bo,k)

def result(bo,k):          #运算函数分类别计算
    contain = ''
    num = 0
    rtype = random.randint(2,k)          #生成随机项目数
    if bo == 1:
        for i in range(rtype):           #生成随机数以及随机运算符
            rsign = random.choice(['+','-','*','/'])  
            rnum1 = random.randint(0,100)
            contain += str(rnum1)+str(rsign)
            num += 1
            if num == rtype:
                contain = contain[0:-1] + '='     #置换最后一个字符为“=”放在contain字符串中
                rescon = contain.replace('=','')  #置换最后一个字符为空放在rescon字符串中
        if eval(rescon) < 0:                      #判断结果是否为负数
            result(bo,k)
        else:
            print('题目为:{}{}'.format(contain,eval(rescon)))#输出
    else:
        for i in range(rtype):
            #生成随机真分数以及随机运算符
            rsign = random.choice(['+','-','*','/'])  
            snum1 = random.randint(0,100)
            mnum1 = random.randint(1,100)
            zfs1 = Fraction(snum1, mnum1)
            contain += str(zfs1)+str(rsign)
            num += 1
            if num == rtype:
                contain = contain[0:-1] + '='
                rescon = contain.replace('=','')
        if eval(rescon) < 0:
            result(bo,k) #复用
        else:
            print('题目为:{}{}'.format(contain,eval(rescon)))#输出

 测试结果:

 可以看到输出算式不再有负数结果出现。

 2.关键函数result(bo,bo2,k)

def result(bo,bo2,k):          #运算函数分类别计算
    contain = ''
    num = 0
    rtype = random.randint(2,k)          #生成随机项目数
    if bo == 1:
        for i in range(rtype):           #生成随机数以及随机运算符
            rsign = random.choice(['+','-','*','/'])  
            rnum1 = random.randint(0,100)
            contain += str(rnum1)+str(rsign)
            num += 1
            if num == rtype:
                contain = contain[0:-1] + '='     #置换最后一个字符为“=”放在contain字符串中
                rescon = contain.replace('=','')  #置换最后一个字符为空放在rescon字符串中
        if eval(rescon) < 0:                      #判断结果是否为负数
            result(bo,bo2,k)
        else:
            if bo2 == 1:
                print('题目为:{}{}'.format(contain,eval(rescon)))#输出
            else:
                print('请解答:{}'.format(contain))
    else:
        for i in range(rtype):
            #生成随机真分数以及随机运算符
            rsign = random.choice(['+','-','*','/'])  
            snum1 = random.randint(0,100)
            mnum1 = random.randint(1,100)
            zfs1 = Fraction(snum1, mnum1)
            contain += str(zfs1)+str(rsign)
            num += 1
            if num == rtype:
                contain = contain[0:-1] + '='
                rescon = contain.replace('=','')
        if eval(rescon) < 0:
            result(bo,bo2,k)
        else:
            if bo2 == 1:
                print('题目为:{}{}'.format(contain,eval(rescon)))#输出
            else:
                print('请解答:{}'.format(contain))

添加style参数,是一个bool量,判断模式选择,若输入不为1,则为问答形式

测试结果:

 隐藏答案,模拟出题形式。

原文地址:https://www.cnblogs.com/iconangle/p/13744245.html