第5次作业--四则运算

GitHub项目地址:   https://github.com/ZJW9633/hello-word/blob/master/ElArith.py


PSP

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划  10  23
Estimate 估计这个任务需要多少时间 15   /
Development 开发  40  60
Analysis 需求分析(包括学习新技术)  40

60

Design Spec 生成设计文档  /  /
Design Review 设计复审(和同事审核设计文档)  20  25
Coding Standard 代码规范(为目前的开发制定合适的规范) 10  10
Design 具体设计  20  25
Coding 具体编码  60  120
Code Review 代码复审  15  20
Test 测试(自我测试,修改代码,提交修改)  25  25
Reporting 报告  25  60
Test Report 测试报告  40  45
Size Measurement 计算工作量  10  10
Postmortem & Process Improvement Plan 事后总结,并提出过程改进计划  20  25
合计    325  460

项目要求

  • 参与运算的操作数(operands)除了100以内的整数以外,还要支持真分数的四则运算。操作数必须随机生成。
  • 运算符(operators)为 +, −, ×, ÷ 运算符的种类和顺序必须随机生成。
  • 要求能处理用户的输入,并判断对错,打分统计正确率。
  • 使用 参数控制生成题目的个数。

解题思路:

  • 定义一个函数用于随机生成随机长度的算式
  • 把字符串型的算式转换为逆波兰式(RPN,也称后缀表达式)
  • 再把后缀表达式利用栈结构计算出结果
  • 最后再与用户的输入做比较

重点难点:

  • 计算结果为负数的情况
  • 分数的表示和计算

难点解决思路:

  • 使用python的fractions库
  • 分数的表示和计算

代码说明:

#算式转换形式
    def toRpn(self, question):
        self.stack = []
        s = ''
        for x in question:
            if x != '+' and x != '-' and x != '×' and x != '÷' and x != '/':
                s += x  #若为数字,直接输出
            else:  # 若为运算符,进栈
                if not self.stack:  #栈空
                    self.stack.append(x)
                else:
                    if self.this_bigger(x, self.stack[-1]):  #运算级高于栈顶元素
                        self.stack.append(x)  #直接进栈
                    else:
                        while self.stack:
                            if self.this_bigger(x, self.stack[-1]):
                                break
                            s += self.stack.pop()
                        self.stack.append(x)
        while self.stack:
            s += self.stack.pop()
        return s

代码测试

C:UsersAdministratorPycharmProjectsuntitledvenvScriptspython.exe C:/Users/Administrator/PycharmProjects/untitled/size.py
please input the num of question:10
----------the total of question is:10----------
the 1/10 question is :5×3-2+4-8×8/9=
1
Your answer is wrong !The right answer is :89/9
the 2/10 question is :5+8/9÷1×3×7/9-4=
2
Your answer is wrong !The right answer is :83/27
the 3/10 question is :7÷8-1+8×8/9-4/9×8=
3
Your answer is wrong !The right answer is :247/72
the 4/10 question is :4+6/9+5=
87/9
Your answer is wrong !The right answer is :29/3
the 5/10 question is :2÷8+7÷3×1+8=
1
Your answer is wrong !The right answer is :127/12
the 6/10 question is :0+7/9+2+7÷6÷1=
2
Your answer is wrong !The right answer is :71/18
the 7/10 question is :3/5-8÷3+1×6/7×7×1=
3
Your answer is wrong !The right answer is :59/15
the 8/10 question is :4/6÷6=
4/36
Your answer is wrong !The right answer is :1/9
the 9/10 question is :7+3-6×0÷6=

Your answer is wrong !The right answer is :10
the 10/10 question is :8÷1÷6+7×2÷5/8×7/8=
2
Your answer is wrong !The right answer is :787/480
-------------------------------------
----------you have pass 0 question----------

Process finished with exit code 0

 效能分析,可用python的profile进行

结果:暂无

原文地址:https://www.cnblogs.com/zjinwei/p/8886865.html