软件工程第二次作业

一.作业信息

所属课程 软件工程
作业要求 作业要求
作业目标 实现四则运算
学号 3180701327

二.题目要求

写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:

  1. 除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
  2. 程序要求能处理用户的输入,判断对错,累积分数
  3. 程序支持可以由用户自行选择加、减、乘、除运算
  4. 使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目

三.代码提交

import random
from fractions import Fraction

def newint(symbol=-1):#生成整数算式
    operater = ['+', '-', '×', '÷']

    if symbol==-1:
        symbol = random.randint(0, 3)#生成符号

    op1 = random.randint(1, 20)#生成两个操作数
    op2 = random.randint(1, 20)#生成两个操作数

    currect_result = 0
    #计算结果
    if symbol == 0:
        currect_result = op1 + op2
    elif symbol == 1:
        op1, op2 = max(op1, op2), min(op1, op2)
        currect_result = op1 - op2
    elif symbol == 2:
        currect_result = op1 * op2
    elif symbol == 3:
        op1, op2 = max(op1, op2), min(op1, op2)
        while op1 % op2 != 0:
            op1 = random.randint(1, 10)
            op2 = random.randint(1, 10)
            op1, op2 = max(op1, op2), min(op1, op2)
        currect_result = int(op1 / op2)
    print(op1, operater[symbol], op2, '= ', end='')
    return currect_result

def newfra(symbol=-1):#生成分数算式
    operater = ['+', '-', '×', '÷']
    if symbol==-1:
        symbol = random.randint(0, 3)

    denominator = random.randint(1, 10)
    numerator = random.randint(denominator, 10)
    op1 = Fraction(denominator, numerator)

    denominator = random.randint(1, 10)
    numerator = random.randint(denominator, 10)
    op2 = Fraction(denominator, numerator)
  
    currect_result = 0
    if symbol == 0:
        currect_result = op1 + op2
    elif symbol == 1:
        op1, op2 = max(op1, op2), min(op1, op2)
        currect_result = op1 - op2
    elif symbol == 2:
        currect_result = op1 * op2
    elif symbol == 3:
        op1, op2 = max(op1, op2), min(op1, op2)
        currect_result = op1 / op2
    print(op1, operater[symbol], op2, '= ', end='')
    return currect_result

def newtest(n):#批量生成算式
    operater = ['+', '-', '×', '÷']
    currect_result=[]
    m=0
    while m<=(n-1):
        calc_mode = random.randint(0, 1)
        if calc_mode==0:
            print(m+1,end='.')
            currect_result.append(newfra())
            print(' ')
        else:
            print(m+1,end='.')
            currect_result.append(newint())
            print(' ')
        m=m+1
    m=0
    print('答案:')
    while m<=(n-1):
        print(m+1,'.',currect_result[m])
        m=m+1

if __name__ == '__main__':
    mode=int(input('请输入执行模式
 1.生成一个 2.生成n个'))
    if mode == 1:
        while True:
            score=0

            calc_method=input('请输入计算方式
1.+ 2.- 3.X 4.÷ 不填为随机生成
')
            if calc_method=='':
                calc_method=-1
            else:
                calc_method=int(calc_method)

            calc_mode=int(input('请输入计算模式
1.真分数 2.整数 3.退出
'))
            if calc_mode==1:
                correct_answer = newfra(calc_method)
            elif calc_mode==2:
                correct_answer = newint(calc_method)
            elif calc_mode==3:
                break
            input_answer = input()
            if calc_mode==1:
                input_answer = Fraction(input_answer)
            else:
                input_answer = int(input_answer)

            print(input_answer)

            if input_answer == correct_answer:
                score=score+1
                print('结果正确,当前分数{}'.format(score))
            else:
                score=score-1
                print('结果错误,正确结果为{},当前分数{}' .format(correct_answer,score))
    else:
        print('输入题库所需要的题目数量
')
        n=int(input())
        newtest(n)

运行截图

image.png

image.png

四.个人小结

psp 任务内容 计划完成需要的时间(min) 实际完成需要的时间(min)
Planning 计划 60 60
Estimate 估计这个任务需要多少时间,并规划大致工作时间 60 90
Development 开发 60 60
Analysis 需要分析(包括学习新技术) 0 0
Design Spec 生成设计文档 0 0
Design Review 设计复审 5 5
Coding Standard 代码规范 0 0
Design 具体设计 60 60
Coding 具体编码 60 60
Code Review 代码复审 5 5
Test 测试(自我测试,修改代码,提交代码) 3 3
Reporting 报告 0 0
Test Report 测试报告 3 3
Size Measurement 计算工作量 0 0
Postmortem & ProcessImprovement Plan 事后总结,并提出改进计划 5 5
原文地址:https://www.cnblogs.com/smallsung/p/13941038.html