付宇泽四则运算试题生成,结对

本作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/7631

git链接:https://e.coding.net/kangzhe/size.git

结对伙伴:康哲

功能1. 四则运算

支持出题4个数的四则运算题目,所有题目要求作者有能力正确回答 (提示:1/3 != 0.33333333333333333333333333333333,而是无限长)。

功能2. 支持括号

功能一、二重点和难点:首先随机生成四则运算表达式,然后将算式转为逆波兰表达式,再计算逆波兰表达式的值即最后算式结果。

                  收获:首先就是学会了如何用python语言创建一栈,运用random类随机生成一个表达式,还重新复习了一下逆波兰表达式

                 部分代码:

def function1():
    right = 0
    i = 0

    while i in range(20):
        i = i + 1
        equation = create_formula()
        re_equation = reverse_polish(equation)
        result = calculate(re_equation)
        answer0 = result
        if result is False or result is None or len(str(result)) > 12:
            i = i - 1
            continue
        str_equation = " ".join('%s' % id for id in equation)
        print(str_equation + "=")
        print("?", end="")
        answer = float(input())
        if abs(result - int(result)) < 1.0e-16:
            result = int(result)
        if answer == answer0:
            print("答对啦,你真是个天才!")
            right = right + 1
        else:
            print("再想想吧,答案似乎是" + repr(result) + "喔!")
    print("你一共答对" + repr(right) + "道题,共20道题。")

                  运行截图:

                              

                                  

功能3. 限定题目数量,"精美"打印输出,避免重复

 功能三重点和难点:功能3对于小数和负数都输需要输出提示语句的,还需要将生成的表达式写入到本地的txt文件中。

                      收获:学会了如何用Python生成写入文本文件,进一步巩固了命令行参数的使用

               部分代码:

ef function3(n):
    n = int(n)
    i = 0

    while i in range(n):
        i = i+1
        equation = create_formula()
        re_equation = reverse_polish(equation)
        result = calculate(re_equation)
        if result is False or result is None or len(str(result)) > 12:
            i = i-1
            continue
        if i is 1:
            if os.path.exists('result.txt'):
                os.remove('result.txt')
        write_file(equation, result)

            运行截图:

                

要求一:结对编程的体会:自己编代码经常会分析问题的时候屡不清思路,但是两个人的时候就很少会出现这个问题,有困难的时候也不会觉得太过沮丧,两个人合作很快就会整理出个解决方法。

          讨论时间较长的功能及收获:讨论时间最长的应该是栈的生成和使用吧,用随机生成函数random控制生成数字和运算符也讨论了很久。最大的收获应该就是栈的实际应用吧,之前一直在试题书上了解过,还从来没有实际生成过栈。

要求二:工作环境 冬华B521  

              

猜你喜欢

转载自www.cnblogs.com/Fuyuze/p/11580901.html
今日推荐