四则运算

一、作业信息

博客班级https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18
作业要求 https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18/homework/11377
作业目标 写一个四则运算出题程序
学号 3180701240

二、作业要求

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

1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24

2)程序要求能处理用户的输入,判断对错,累积分数

3)程序支持可以由用户自行选择加、减、乘、除运算

4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目

三、代码实现

import random

from fractions import Fraction

operation = ['+', '-', '*', '/']   #四则运算的符号

global f

 

 

def integer_score():

    #rand = operation[random.randint(0,3)]

    number = random.randint(1,4)     #随机产生的表达式长度

    f = ''

    for i in range(number):

        a = random.randint(1,20)       #随机产生的表达式中的数

        rand = operation[random.randint(0, 3)]     #随机选择一个四则运算中的符号

        if rand == '/':

            b = random.randint(a, 20)               #随机产生的真分数的分母

            f += str(a) + rand + str(b)               #数与符号相连

            rand = operation[random.randint(0, 2)]     #随机选择一个四则运算中的符号

            f += rand

        else:

            f += str(a) + rand

        #print(a,rand,end='')

    b = random.randint(1, 20)

    f += str(b)                     #得到完整的表达式

    n = eval(f)                      #得到表达式的结果

    n = Fraction('{}'.format(n)).limit_denominator()    #小数转化为分数

    if n > 0:

        print('题目:')

        print(f,'=')

        print('请输出答案:')

        x = Fraction('{}'.format(eval(input()))).limit_denominator()

        if n == x:                  #输入的数与表达式比较

            print(True)

        else:

            print(False)

            print('正确的答案为:',n)

    else:

        integer_score()

def integer():

    # rand = operation[random.randint(0,3)]

    number = random.randint(1, 3)

    f = ''

    for i in range(number):

        a = random.randint(1, 10)

        rand = operation[random.randint(0, 3)]

        f += str(a) + rand

    b = random.randint(1, 10)

    f += str(b)

    n = eval(f)

    if isinstance(n, int) and n > 0:

        print('题目:')

        print(f, '=')

        print('请输出答案:')

        x = eval(input())

        if n == x:

            print(True)

        else:

            print(False)

            print('正确的答案为:', n)

    else:

        integer()

def score():

    op = ['+', '-']

    number = random.randint(1, 3)

    f = ''

    for i in range(number):

        a = random.randint(1, 10)

        b = random.randint(a, 10)

        rand = op[random.randint(0, 1)]

        f += str(a) + '/'+ str(b)+rand

    a = random.randint(1, 10)

    b = random.randint(a, 10)

    f += str(a) + '/'+ str(b)

    n = eval(f)

    n = Fraction('{}'.format(n)).limit_denominator()

    if n > 0:

        print('题目:')

        print(f,'=')

        print('请输出答案:')

        x = Fraction('{}'.format(eval(input()))).limit_denominator()

        if n == x:

            print(True)

        else:

            print(False)

            print('正确的答案为:',n)

    else:

        score()

 

if __name__ == '__main__':

    while True:

        print('选择你想做的题目:')

        print('0(退出)1(分数题目),2(整数题目),3(综合题目)')

        m = int(input())

        if m == 1:

            score()

        elif m == 2:

            integer()

        elif m == 3:

            integer_score()

        elif m == 0:

            exit()

        else:

            print('请重新输入你的选择')

 

四、运行结果

 PSP表格

psp2.1任务内容计划完成需要的时间(min)实际完成需要的时间(min)
Planning 计划 10 15
Estimate 估计这个任务需要多少时间,并规划大致工作步骤 120 180
Development 开发 40 60
Analysis 需求分析(包括学习新技术) 15 18
Design Spec 生成设计文档 10 12
Design Review 设计复审 10 15
Coding Standard 代码规范 5 10
Design 具体设计 15 20
Coding 具体编码 30 32
Code Review 代码复审 5 9
Test 测试(自我测试,修改代码,提交修改) 10 25
Reporting 报告 20 30
Test Report 测试报告 10 15
Size Measurement 计算工作量 5 10
Postmortem & Process Improvement Plan 事后总结,并提出过程改进计划 10 20

五、心得体会

用python写一个关于小学数学四则运算的程序,是对之前整数四则运算的拓展,也是对python知识的巩固。同时也学会了使用博客园!

原文地址:https://www.cnblogs.com/zzh0/p/13943890.html