Java教程

2021-2022-1 20211420《信息安全专业导论》小学四则运算编程实践

本文主要是介绍2021-2022-1 20211420《信息安全专业导论》小学四则运算编程实践,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

设计思路

小学四则运算少不了自然数,符号,等号等等,所以需要将每一个功能都定义一个函数

代码

# 导入库并定义列表
import random
import fractions

questions = []
answers = []
#实现四则运算基本功能
def randomcalc(questions, answers):
    symbol = random.choice(["+", "-", "×", "÷"])
    if symbol == "+":
        a = random.randint(0,10)
        b = random.randint(0,10)
        while a == b:
            a = random.randint(0, 10)
            b = random.randint(0, 10)
        else:
            questions.append(str(a) + '+' + str(b) + '=')
            answers.append(a + b)
    elif symbol == "-":
        a = random.randint(0,10)
        b = random.randint(0,10)
        while a == b:
            a = random.randint(0, 10)
            b = random.randint(0, 10)
        else:
            questions.append(str(a) + '-' + str(b) + '=')
            answers.append(a - b)
    elif symbol == "×":
        a = random.randint(0,10)
        b = random.randint(0,10)
        while a == b:
            a = random.randint(0, 10)
            b = random.randint(0, 10)
        else:
            questions.append(str(a) + '×' + str(b) + '=')
            answers.append(a * b)
    else:
        a = random.randint(0, 10)
        if a == 0:
            b = random.randint(1, 10)
        else:
            b = random.randint(1, a+1)
        while a == b:
            a = random.randint(0, 10)
            if a == 0:
                b = random.randint(1, 10)
            else:
                b = random.randint(1, a + 1)
        else:
            questions.append(str(a) + '÷' + str(b) + '=')
            answers.append(fractions.Fraction(a, b))
# 生成分数
def createfraction():
    fenzi1 = random.randint(1, 10)
    fenmu1 = random.randint(1, 10)
    fenzi2 = random.randint(1, 10)
    fenmu2 = random.randint(1, 10)
    fraction1 = fractions.Fraction(fenzi1, fenmu1)
    fraction2 = fractions.Fraction(fenzi2, fenmu2)
    return fraction1, fraction2
# 两个分数四则运算(输出应为大于0的真分数)
def fractioncalc(questions, answers):
    symbol = random.choice(["+", "-", "×", "÷"])
    a, b = createfraction()
    if symbol == "+":
        while a+b>1:
            a, b = createfraction()
        while a == b:
            a, b = createfraction()
        else:
            questions.append(str(a) + '+' + str(b) + '=')
            answers.append(a + b)
    elif symbol == "-":
        a, b = createfraction()
        a, b = max(a, b), min(a, b)         # 防止出现负数
        while a == b:
            a, b = createfraction()
        else:
            questions.append(str(a) + '-' + str(b) + '=')
            answers.append(a - b)
    elif symbol == "×":
        while a*b>1:
            a, b = createfraction()
        while a == b:
            a, b = createfraction()
        else:
            questions.append(str(a) + '×' + str(b) + '=')
            answers.append(a * b)
    else:
        while a/b>1:
            if b == 0:
                b += 1
            else:
                a, b = createfraction()
        while a == b:
            if b == 0:
                b += 1
            else:
                a, b = createfraction()
        else:
            questions.append(str(a) + '÷' + str(b) + '=')
            answers.append(fractions.Fraction(a, b))
# 输出函数
def output():
    n = k = 10000  # 第一个while循环的n值会改变
    m = 0
    txt1 = open('exercises.txt', 'w+')
    txt2 = open('answers.txt', 'w')
    # 写入10000道题目
    while n>0:
        choice = random.choice([0, 1])
        if choice == 0:
            randomcalc(questions, answers)
        else:
            fractioncalc(questions, answers)
        n -= 1
    # 向两个文件中分别写入题目和答案
    while m<k:
        question = questions[m]
        answer = answers[m]
        txt1.write(str(m+1) + '.')
        txt2.write(str(m + 1) + '.')
        txt1.write(question)
        txt2.write(str(answer))
        txt1.write('\n')
        txt2.write('\n')
        m += 1
# 实现统计错题,判定对错以及计算得分的功能
def judge():
    k = 10000
    print("请将题目的答案按题号写入answer.txt文件中")
    text = input("请输入\'yes'获得文本:")
    if text == 'yes':
        youranswer = open('answer.txt')
        answers = open('answers.txt')
        x = list(youranswer)
        y = list(answers)
        o = 0
        C = 0
        Correct = []
        Wrong = []
        while o < k:
            if x[o] == y[o]:
                t = o + 1
                Correct.append(t)
            else:
                C += 1
                p = o + 1
                Wrong.append(p)
            o += 1
        Grade = k - C
        G = open('Grade.txt', 'w')
        G.write('Correct:')
        G.write(str(Correct))
        G.write('\n')
        G.write('Wrong:')
        G.write(str(Wrong))
        G.write('\n')
        G.write('Grade:')
        G.write(str(Grade))
        G.close()

# 判断重复题目数量
def repeatjudger():
    repeatfile = open('repeat.txt', 'w')
    repeater = input('请输入continue以继续程序:')
    open('repeat.txt', 'w')
    if repeater == 'continue':
        repeat = []
        repeatnum = 0
        j = 0
        for i in range(0, 10000):
            while j < i:
                if questions[i] == questions[j]:
                    repeatnum += 1
                    repeatq = questions[i]
                    repeat.append(questions[i])
                    repeatfile.write('重复的题目是:')
                    repeatfile.write(repeatq)
                    repeatfile.write('\n')
                    repeatfile.write('对应的题号为:')
                    repeatfile.write(str(j+1))
                    repeatfile.write('和')
                    repeatfile.write(str(i+1))
                    repeatfile.write('\n')
                    j += 1
                else:
                    break

        repeatfile.write('\n')
        repeatfile.write('重复题目的数量为:')
        repeatfile.write(str(repeatnum))
        repeatfile.close()
        print(repeat)
    print("程序结束!")

到这里基本已经实现任务目标,但是还有一些细节并没有处理得当。至于生成的题目数量,我这用的10000道题目,可以更多,也可以较少,只要你的计算机不会崩溃就能够运行。

这篇关于2021-2022-1 20211420《信息安全专业导论》小学四则运算编程实践的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!