软件工程 小学生数学题的四则运算

 

1、github代码地址

https://github.com/lzx123kk/-

 

2、psp表格

第一次作业PSP

任务内容

计划完成需要的时间(h)

实际完成需要的时间(h)

Planning

计划

8

12

 Estimate

估计这个任务需要多少时间,并规划大致工作步骤

0.6

1

Analysis

需求分析 (包括学习新技术)

0.4

1

Design

具体设计

0.6

2

Coding

具体编码

3

3

test

测试(自我测试,修改代码,提交修改)

3

4

Postmortem & Process

Improvement Plan

事后总结 ,并提出过程改进计划

0.4

1

Summary

合计

 

8

 

12

 

3、解题思路

  一开始:生成一个简单的四则运算函数,和一个答案函数,写出一个主函数调用运算函数,对其进行判断。对则回复回答正确,错则回复回答错误。其中用到random随机库

  之后:进行改进对其进行添加一个参数,使得我们可以自己随机设置题目的数量。其中可以用到sys中的argv函数

 

4、关系流程图

 

 

 5、关键代码及其注释

5.1

#生成随机等式
    #@profile
    def getEquation(self):
        number = randint(2,9)
        tmpstring = ""
        tmpop = ''
        tmpint = 0
        for i in range(number):
            if tmpop == '/':            #分数情况
                tmpint = randint(tmpint+1,9)
                tmpop = choice(self.op[:-1])
            elif tmpop == '÷':          #除号情况
                tmpint = randint(1,8)
                tmpop = choice(self.op)
            else:
                tmpint = randint(0,8)
                tmpop = choice(self.op)

5.2计算答案函数

  #求算式答案
      #@profile
  def getAnswer(self,exp):        
        #将带有分号的表达式化成带分数的list
        equlist = []
        i = 0
        while(i < len(exp)-1):
            if exp[i+1] != '/':
                equlist.append(exp[i])
                i += 1
            else:
                equlist.append(Fraction(int(exp[i]),int(exp[i+2])))
                i += 3
        #将中缀表达式转化为后缀
        new_equlist = self.change_list(equlist)
        #计算后缀表达式的结果
        return(self.calculate(new_equlist))

5.3 转化为后缀表达式后的算式

#转化为后缀表达式
    #@profile
    def change_list(self,equation):
        tmplist = []
        stack = []
        for op in equation:
            if type(op) == str and op >= '0' and op <= '9':
                tmplist.append(int(op))
            elif type(op) != str:
                tmplist.append(op)
            elif op == ')':
                tmpTopStack = ''
                while tmpTopStack != '(':
                    tmpTopStack = stack.pop()
                    if tmpTopStack != '(':
                        tmplist.append(tmpTopStack)
            elif len(stack) == 0 or op == '(' or stack[-1] == '(':
                stack.append(op)
            else:
                while(len(stack) > 0 and stack[-1] != '(' and self.priority[stack[-1]] >= self.priority[op]): #栈顶优先级大于等于该符号,持续出栈
                    tmplist.append(stack.pop())
                stack.append(op)
        while(len(stack) != 0):
            tmplist.append(stack.pop())
        return tmplist

5.4 转化后的答案

#计算后缀表达式的结果
    #@profile
    def calculate(self,_list):
        tmpStack = []
        for tmpValue in _list:
            if type(tmpValue) != str:
                tmpStack.append(tmpValue)
            else:
                number_y = tmpStack.pop()
                number_x = tmpStack.pop()
                if tmpValue == "+":
                    tmp = number_x+number_y
                elif tmpValue == "-":
                    tmp = number_x-number_y
                elif tmpValue == "*":
                    tmp = number_x*number_y
                else:
                    tmp = Fraction(number_x,number_y)
                tmpStack.append(tmp)
        return tmpStack[0]

5.5 进行主函数调用

def main():
    if sys.argv[1] != "-n":
        raise IOError("Please enter the right command!")
    num = int(sys.argv[2])
    score = 0
    print("本次测试共{}题,满分100分".format(num))
    for i in range(1,num+1):
        equation = Equation()
        equation.start()
        print("-----------------------------")
        print("第{}题: {}".format(i,equation.equ),end = '')
        ans = input().strip()
        if ans == str(equation.answer):
            score += 1
            print("回答正确!:)")
        else:
            print("回答错误。:( 正确答案:{}".format(equation.answer))
    print("-----------------------------")
    print("测试结束,本次测试得分:{}分".format(round(float(score)/float(num)*100)))

6、测试运行

 

 代码运行效果

 

 

 

posted @ 2020-09-21 10:54  roseim  阅读(93)  评论(0编辑  收藏  举报