• 结对项目-自动生成小学四则运算题目


    题目:实现一个自动生成小学四则运算题目的命令行程序(也可以用图像界面,具有相似功能)。

    1.Github项目地址:

       结对项目成员:袁千喜3118005073;张择庆3118005076

       github地址:https://github.com/wry327/team_work/

    2. PSP2.1表格

    PSP2.1

    Personal Software Process Stages

    预估耗时(分钟)

    实际耗时(分钟)

    Planning

    计划

     30

    · Estimate

    · 估计这个任务需要多少时间

     1600

    Development

    开发

     1440

    · Analysis

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

     360

    · Design Spec

    · 生成设计文档

     60

    · Design Review

    · 设计复审 (和同事审核设计文档)

     5

    · Coding Standard

    · 代码规范 (为目前的开发制定合适的规范)

     30

    · Design

    · 具体设计

     30

    · Coding

    · 具体编码

     900

    · Code Review

    · 代码复审

     5

    · Test

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

     50

    Reporting

    报告

     120

    · Test Report

    · 测试报告

     60

    · Size Measurement

    · 计算工作量

     10

    · Postmortem & Process Improvement Plan

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

     50

    合计

     1590


    3.设计实现过程:

              思路:首先拿到一个如此多元复杂的编程问题,一上来直接着手解决问题是不可能的,而且其中也有很多我们不熟悉不会的问题。那么我们从简单的着手,运用面向对象编程的思路,先实现最基本的问题,设计一个函数解决。随后再逐渐使代码复杂化,逐渐增加它的功能,实现工程化面向对象解决问题。

    4.代码实现过程:

     源代码:

    def divide(a, b):   #除法
        return Fraction(a, b)
    
    
    def operation_one(a, b, op):                    #一个运算符的题目
        if op == 1:
            questions.append(str(a)+'+'+str(b)+'=?')
            answers.append(str(add(a,b)))
        elif op == 2:
            questions.append(str(a)+'-'+str(b)+'=?')
            answers.append(str(minus(a,b)))
        elif op == 3:
            questions.append(str(a)+'*'+str(b)+'=?')
            answers.append(str(multiply(a,b)))
        else :
            questions.append(str(a)+'/'+str(b)+'=?')
            answers.append(str(divide(a,b)))
    
        return questions,answers
    
    def operation_two(a, b, c, op1, op2):           #两个运算符的题目
            if op1 == 1:
                if op2 == 1:
                    questions.append(str(a)+'+'+str(b)+'+'+str(c)+'=?')
                    answers.append(str(add(add(a,b),c)))
                elif op2 == 2:
                    questions.append(str(a)+'+'+str(b)+'-'+str(c)+'=?')
                    answers.append(str(minus(add(a,b),c)))
                elif op2 == 3:
                    questions.append(str(a)+'+'+str(b)+'*'+str(c)+'=?')
                    answers.append(str(add(a,multiply(b, c))))
                else :
                    questions.append(str(a)+'+'+str(b)+'/'+str(c)+'=?')
                    answers.append(str(add(a,divide(b, c))))
            elif op1 == 2:
                if op2 == 1:
                    questions.append(str(a)+'-'+str(b)+'+'+str(c)+'=?')
                    answers.append(str(add(minus(a,b),c)))
                elif op2 == 2:
                    questions.append(str(a)+'-'+str(b)+'-'+str(c)+'=?')
                    answers.append(str(minus(minus(a,b),c)))
                elif op2 == 3:
                    questions.append(str(a)+'-'+str(b)+'*'+str(c)+'=?')
                    answers.append(str(minus(a,multiply(b, c))))
                else :
                    questions.append(str(a)+'-'+str(b)+'/'+str(c)+'=?')
                    answers.append(str(minus(a,divide(b, c))))
            elif op1 == 3:
                if op2 == 1:
                    questions.append(str(a)+'*'+str(b)+'+'+str(c)+'=?')
                    answers.append(str(add(multiply(a,b),c)))
                elif op2 == 2:
                    questions.append(str(a)+'*'+str(b)+'-'+str(c)+'=?')
                    answers.append(str(minus(multiply(a,b),c)))
                elif op2 == 3:
                    questions.append(str(a)+'*'+str(b)+'*'+str(c)+'=?')
                    answers.append(str(multiply(a,multiply(b, c))))
                else :
                    questions.append(str(a)+'*'+str(b)+'/'+str(c)+'=?')
                    answers.append(str(multiply(a,divide(b, c))))
            else :
                if op2 == 1:
                    questions.append(str(a)+'/'+str(b)+'+'+str(c)+'=?')
                    answers.append(str(add(divide(a,b),c)))
                elif op2 == 2:
                    questions.append(str(a)+'/'+str(b)+'-'+str(c)+'=?')
                    answers.append(str(minus(divide(a,b),c)))
                elif op2 == 3:
                    questions.append(str(a)+'/'+str(b)+'*'+str(c)+'=?')
                    answers.append(str(multiply(divide(a,b),c)))
                else :
                    questions.append(str(a)+'/'+str(b)+'/'+str(c)+'=?')
                    answers.append(str(divide(divide(a,b),c)))
            return questions,answers
    
    def operation_three(a, b, c, d, op1, op2, op3): #三个运算符的题目
        if op1 == 1:
            if op2 == 1:
                if op3 == 1:
                    questions.append(str(a)+'+'+str(b)+'+'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(add(add(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'+'+str(b)+'-'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(minus(add(add(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'+'+str(b)+'*'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(add(multiply(c,d),b),a)))
                else :
                    questions.append(str(a)+'+'+str(b)+'/'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(add(divide(c,d),b),a)))
            elif op2 == 2:
                if op3 == 1:
                    questions.append(str(a)+'+'+str(b)+'-'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(minus(add(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'+'+str(b)+'-'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(minus(add(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'+'+str(b)+'-'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(add(minus(b,multiply(c,d)),a)))
                else :
                    questions.append(str(a)+'+'+str(b)+'-'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(add(minus(b,divide(c,d)),a)))
            elif op2 == 3:
                if op3 == 1:
                    questions.append(str(a)+'+'+str(b)+'*'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(add(multiply(b,c),a),d)))
                elif op3 == 2:
                    questions.append(str(a)+'+'+str(b)+'*'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(add(multiply(b,c),a),d)))
                elif op3 == 3:
                    questions.append(str(a)+'+'+str(b)+'*'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(add(multiply(b,multiply(c,d)),a)))
                else :
                    questions.append(str(a)+'+'+str(b)+'*'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(add(multiply(b,divide(c,d)),a)))
            else :
                if op3 == 1:
                    questions.append(str(a)+'+'+str(b)+'/'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(add(divide(b,c),a),d)))
                elif op3 == 2:
                    questions.append(str(a)+'+'+str(b)+'/'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(add(divide(b,c),a),d)))
                elif op3 == 3:
                    questions.append(str(a)+'+'+str(b)+'/'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(add(divide(b,multiply(c,d)),a)))
                else :
                    questions.append(str(a)+'+'+str(b)+'/'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(add(divide(b,divide(c,d)),a)))
        elif op1 == 2:
            if op2 == 1:
                if op3 == 1:
                    questions.append(str(a)+'-'+str(b)+'+'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(add(minus(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'-'+str(b)+'+'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(add(minus(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'-'+str(b)+'+'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(minus(add(multiply(c,d),a),b)))
                else :
                    questions.append(str(a)+'-'+str(b)+'+'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(minus(add(divide(c,d),a),b)))
            elif op2 == 2:
                if op3 == 1:
                    questions.append(str(a)+'-'+str(b)+'-'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(minus(minus(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'-'+str(b)+'-'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(minus(minus(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'-'+str(b)+'-'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(minus(a,minus(b,multiply(c,d)))))
                else :
                    questions.append(str(a)+'-'+str(b)+'-'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(minus(a,minus(b,divide(c,d)))))
            elif op2 == 3:
                if op3 == 1:
                    questions.append(str(a)+'-'+str(b)+'*'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(minus(a,multiply(b,c)),d)))
                elif op3 == 2:
                    questions.append(str(a)+'-'+str(b)+'*'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(minus(a,multiply(b,c)),d)))
                elif op3 == 3:
                    questions.append(str(a)+'-'+str(b)+'*'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(minus(a,multiply(b,multiply(c,d)))))
                else :
                    questions.append(str(a)+'-'+str(b)+'*'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(minus(a,multiply(b,divide(c,d)))))
            else :
                if op3 == 1:
                    questions.append(str(a)+'-'+str(b)+'/'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(minus(a,divide(b,c)),d)))
                elif op3 == 2:
                    questions.append(str(a)+'-'+str(b)+'/'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(minus(a,divide(b,c)),d)))
                elif op3 == 3:
                    questions.append(str(a)+'-'+str(b)+'/'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(minus(a,divide(divide(b,c),d))))
                else :
                    questions.append(str(a)+'-'+str(b)+'/'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(minus(a,divide(divide(b,c),d))))
        elif op1 == 3:
            if op2 == 1:
                if op3 == 1:
                    questions.append(str(a)+'*'+str(b)+'+'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(add(multiply(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'*'+str(b)+'+'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(add(multiply(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'*'+str(b)+'+'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(add(multiply(a,b),multiply(c,d))))
                else :
                    questions.append(str(a)+'*'+str(b)+'+'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(add(multiply(a,b),divide(c,d))))
            elif op2 == 2:
                if op3 == 1:
                    questions.append(str(a)+'*'+str(b)+'-'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(minus(multiply(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'*'+str(b)+'-'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(minus(multiply(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'*'+str(b)+'-'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(minus(multiply(a,b),multiply(c,d))))
                else :
                    questions.append(str(a)+'*'+str(b)+'-'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(minus(multiply(a,b),divide(c,d))))
            elif op2 == 3:
                if op3 == 1:
                    questions.append(str(a)+'*'+str(b)+'*'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(multiply(a,multiply(b,c)),d)))
                elif op3 == 2:
                    questions.append(str(a)+'*'+str(b)+'*'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(multiply(a,multiply(b,c)),d)))
                elif op3 == 3:
                    questions.append(str(a)+'*'+str(b)+'*'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(multiply(a,multiply(b,multiply(c,d)))))
                else :
                    questions.append(str(a)+'*'+str(b)+'*'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(divide(multiply(multiply(a,b),c),d)))
            else :
                if op3 == 1:
                    questions.append(str(a)+'*'+str(b)+'/'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(divide(multiply(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'*'+str(b)+'/'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(divide(multiply(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'*'+str(b)+'/'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(multiply(divide(multiply(a,b),c),d)))
                else :
                    questions.append(str(a)+'*'+str(b)+'/'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(divide(divide(multiply(a,b),c),d)))
        else :
            if op2 == 1:
                if op3 == 1:
                    questions.append(str(a)+'/'+str(b)+'+'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(add(divide(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'/'+str(b)+'+'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(add(divide(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'/'+str(b)+'+'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(add(divide(a,b),multiply(c,d))))
                else :
                    questions.append(str(a)+'/'+str(b)+'+'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(add(divide(a,b),divide(c,d))))
            elif op2 == 2:
                if op3 == 1:
                    questions.append(str(a)+'/'+str(b)+'-'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(minus(divide(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'/'+str(b)+'-'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(minus(divide(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'/'+str(b)+'-'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(minus(divide(a,b),multiply(c,d))))
                else :
                    questions.append(str(a)+'/'+str(b)+'-'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(minus(divide(a,b),divide(c,d))))
            elif op2 == 3:
                if op3 == 1:
                    questions.append(str(a)+'/'+str(b)+'*'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(multiply(divide(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'/'+str(b)+'*'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(multiply(divide(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'/'+str(b)+'*'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(multiply(multiply(divide(a,b),c),d)))
                else :
                    questions.append(str(a)+'/'+str(b)+'*'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(divide(multiply(divide(a,b),c),d)))
            else :
                if op3 == 1:
                    questions.append(str(a)+'/'+str(b)+'/'+str(c)+'+'+str(d)+'=?')
                    answers.append(str(add(divide(divide(a,b),c),d)))
                elif op3 == 2:
                    questions.append(str(a)+'/'+str(b)+'/'+str(c)+'-'+str(d)+'=?')
                    answers.append(str(minus(divide(divide(a,b),c),d)))
                elif op3 == 3:
                    questions.append(str(a)+'/'+str(b)+'/'+str(c)+'*'+str(d)+'=?')
                    answers.append(str(multiply(divide(divide(a,b),c),d)))
                else :
                    questions.append(str(a)+'/'+str(b)+'/'+str(c)+'/'+str(d)+'=?')
                    answers.append(str(divide(divide(divide(a,b),c),d)))
            return questions,answers
        
    def write_in(questions,answers):                #将题目和答案分别写入文件
        for i,question in enumerate(questions):
            f1 = open('questions.txt','a')
            f1.write(""+str(i+1)+"题:")
            f1.write(question)
            f1.write('
    ')
            f1.close()
        for i,answer in enumerate(answers):
            f2 = open('answers.txt','a')
            f2.write(""+str(i+1)+"题答案:")
            f2.write(answer)
            f2.write('
    ')
            f2.close()
            
    
    
    def main():
        for i in range(0,10000):
            a = random.randint(1,9)
            b = random.randint(1,9)
            c = random.randint(1,9)
            d = random.randint(1,9)
            op_count = random.randint(1,3)  #通过随机的op_count控制运算符个数
            op1 = random.randint(1,4)       #第一个运算符
            op2 = random.randint(1,4)       #第二个运算符
            op3 = random.randint(1,4)       #第三个运算符
            if op_count == 1:       
                contents = operation_one(a, b, op1)
            elif op_count == 2:
                operation_two(a, b, c, op1, op2)
            else :
                operation_three(a, b, c, d, op1, op2, op3)
        write_in(contents[0],contents[1])
    
    if __name__ == "__main__":
        main()

    5.测试运行:

    由于直接使用write_in函数直接生成txt文件,此处给出部分示例。

    def write_in(questions,answers):                #将题目和答案分别写入文件
        for i,question in enumerate(questions):
            f1 = open('questions.txt','a')
            f1.write(""+str(i+1)+"题:")
            f1.write(question)
            f1.write('
    ')
            f1.close()
        for i,answer in enumerate(answers):
            f2 = open('answers.txt','a')
            f2.write(""+str(i+1)+"题答案:")
            f2.write(answer)
            f2.write('
    ')
            f2.close()

    6.实际花费时间:

    PSP2.1

    Personal Software Process Stages

    预估耗时(分钟)

    实际耗时(分钟)

    Planning

    计划

     30

     30

    · Estimate

    · 估计这个任务需要多少时间

     1600

     1700

    Development

    开发

     1440

     1500

    · Analysis

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

     360

     400

    · Design Spec

    · 生成设计文档

     60

     70

    · Design Review

    · 设计复审 (和同事审核设计文档)

     5

     5

    · Coding Standard

    · 代码规范 (为目前的开发制定合适的规范)

     30

     30

    · Design

    · 具体设计

     30

     30

    · Coding

    · 具体编码

     900

     900

    · Code Review

    · 代码复审

     5

     5

    · Test

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

     50

     50

    Reporting

    报告

     120

     100

    · Test Report

    · 测试报告

     60

     40

    · Size Measurement

    · 计算工作量

     10

     10

    · Postmortem & Process Improvement Plan

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

     50

     50

    合计

     1590

    7.项目小结:

             对于我们来说,这次的结对项目作业是一个很难得的学习经验。由于我们对与代码知识掌握并不深刻,所以趁此机会恶补了代码相关知识。

             结对项目共同感受:良好的代码习惯能更有助于团队开发。团队的沟通能有效减少开发困难。合理分配时间和项目侧重,有效提高了工作效率。

             袁千喜闪光点:对于项目实现代码有自己独特的见解,能够有效的解决问题。侧重编写代码。在队伍中是当之无愧的领导者,帮助团队里的其他成员。

             张择庆闪光点:学习能力强,能够努力跟进进度,侧重编写博文。

             

  • 相关阅读:
    Python数据结构与算法—栈
    var_export 和 var_dump
    PHp 下标是 区分大小写的
    和眼睛相处
    css 3 animation
    background-attachment: fixed | attachment 区别
    js 函数表达和函数声明
    function 和 new Function
    lastIndexOf js
    substring substr slice js比较
  • 原文地址:https://www.cnblogs.com/Zhangzeqing/p/12615867.html
Copyright © 2020-2023  润新知