软工作业三 结对项目

软件工程 计科1班
作业要求 作业要求
作业目标 结对项目 通过python编程来实现四则运算。

GitHub地址:

https://github.com/Kklvu6/sizeyunsuan

成员

学号 姓名
3121004665 赛尔达尔·艾思开尔
3121004771 艾孜买提·艾合提

psp表格

PSP Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 40 40
Estimate 估计这个任务需要多少时间 500 700
Development 开发 400 500
Analysis 需求分析 (包括学习新技术) 200 280
Design Spec 生成设计文档 30 20
Design Review 设计复审 30 20
Coding Standard 代码规范 (为目前的开发制定合适的规范) 10 10
Design 具体设计 10 10
Coding 具体编码 200 300
Code Review 代码复审 20 10
Test 测试(自我测试,修改代码,提交修改) 40 40
Reporting 报告 30 20
Test Repor 测试报告 20 10
Size Measurement 计算工作量 5 5
Postmortem & Process Improvement Plan 事后总结, 并提出过程改进计划 5 5

效能分析

fracHandle函数,分析处理字符串与分数的关系,提高运算效率

代码说明

  • 生成题目
def makeExe(self):
        localtime = time.asctime(time.localtime(time.time()))
        # 清理文件内容
        with open("Exercise.txt", 'w') as exercise_file:
            exercise_file.write("题目数量 : " + str(self.account) + "\t时间 :" + localtime + "\n")
            exercise_file.close()
        with open("Answer.txt", 'w') as Answer_file:
            Answer_file.close()

        que_order = 0
        repeat = testRepeat()
        makeQue = randomMake(repeat, self.range)
  • 两个数题目生成
  • 生产两个文件,分别为Exercise.txt,Answer.txt,题目生成在Exercise.txt中,将答案写入Answer.txt
import random
from fractions import Fraction
from fracHandle import fracHandle
from isRepeat import testRepeat
class randomMake:
    def __init__(self, repeat, range):
        self.repeat = repeat
        self.range = range

    # 生成两个数的题目
    def question_2bits(self, order):
        while True:
            frac_Handle = fracHandle()
            sign = self.random_sign()
            numberList = []
            signList = []
            firstNum = self.random_frac()
            secondNum = self.random_frac()  # 生成两个分数

            question = ''
            rightAnswer = ''
            numberList.append(str(firstNum) + ',' + str(secondNum))  # 将生成的数字和符号放在字典同一个位置
            signList.append(sign)
            if self.repeat.isRepeat(numberList, signList) == True:  # 检测此组数字和运算符是否重复
                rightAnswer = frac_Handle.fracAccount(firstNum, secondNum, sign)
                if rightAnswer != False:
                    rightAnswer = fracHandle.fracToStr(self, rightAnswer)
                    question = str(order) + ". " + frac_Handle.fracToStr(
                        firstNum) + " " + sign + " " + frac_Handle.fracToStr(
                        secondNum) + " ="
                else:
                    continue
            else:
                continue
            # 将问题写入 Exercise.txt中,将答案写入Answer.txt中
            with open("Exercise.txt", 'a') as exercise_file:
                exercise_file.write(question + "\n")
                exercise_file.close()
            with open("Answer.txt", 'a') as Answer_file:
 Answer_file.write(str(order) + ". " + rightAnswer + "\n")
                Answer_file.close()
            break
          
  • 随机生成加减乘除四个符号
# 随机生成符号
   def random_sign(self):
       randomSign = random.randint(0, 3)
       if randomSign == 0:
           sign = '+'
       elif randomSign == 1:
           sign = '-'
       elif randomSign == 2:
           sign = '*'
       else:
           sign = '÷'
       return sign
# 随机生成分数
   def random_frac(self):
       while (True):
           numer = random.randint(0, self.range)  # 分子
           dno = random.randint(1, self.range)  # 分母
           Num = Fraction(numer, dno)
           if Num:
               break
       return Num
 # 生成三个数的题目
    def question_3bits(self, order):
        while True:
            frac_Handle = fracHandle()
            # 生成相应的数字和符号
            sign1 = self.random_sign()
            sign2 = self.random_sign()
            numberList = []
            signList = []
            firstNum = self.random_frac()
            secondNum = self.random_frac()
            thirdNum = self.random_frac()

            question = ''
            rightAnswer = ''
            numberList.append(str(firstNum) + ',' + str(secondNum) + ',' + str(thirdNum))
            signList.append(sign1 + ',' + sign2)
            if self.repeat.isRepeat(numberList, signList) == True:  # 检测是否重复
                brackets = random.randint(0, 2)  # 随机生成括号的位置
                # 不生成括号
                if (brackets == 0):
                    question = str(order) + ". " + frac_Handle.fracToStr(
                        firstNum) + " " + sign1 + " " + frac_Handle.fracToStr(
                        secondNum) + " " + sign2 + " " + frac_Handle.fracToStr(thirdNum) + " ="
                    # 如果前面是加号和减号而后面不是
                    if (sign1 == '+' or sign1 == '-') and (sign2 == '*' or sign2 == '÷'):
                        firstSum = frac_Handle.fracAccount(secondNum, thirdNum, sign2)
                        if firstSum != False:
                            rightAnswer = frac_Handle.fracAccount(firstNum, firstSum, sign1)
                            if rightAnswer != False:
                                rightAnswer = frac_Handle.fracToStr(rightAnswer)
                            else:
                                continue
                        else:
                            continue
                    # 其他情况
                    else:
                        firstSum = frac_Handle.fracAccount(firstNum, secondNum, sign1)
                        if firstSum != False:
                            rightAnswer = frac_Handle.fracAccount(firstSum, thirdNum, sign2)
                            if rightAnswer != False:
                                rightAnswer = frac_Handle.fracToStr(rightAnswer)
                            else:
                                continue
                        else:
                            continue
                # 左边加一个括号:
                elif (brackets == 1):  # 左边加括号
                    question = str(order) + ". (" + frac_Handle.fracToStr(
                        firstNum) + " " + sign1 + " " + frac_Handle.fracToStr(
                        secondNum) + ") " + sign2 + " " + frac_Handle.fracToStr(thirdNum) + " ="
                    firstSum = frac_Handle.fracAccount(firstNum, secondNum, sign1)
                    if firstSum != False:
                        rightAnswer = frac_Handle.fracAccount(firstSum, thirdNum, sign2)
                        if rightAnswer != False:
                            rightAnswer = frac_Handle.fracToStr(rightAnswer)
                        else:
                            continue
                    else:
                        continue
                # 右边加括号
                else:
                    question = str(order) + ". " + frac_Handle.fracToStr(
                        firstNum) + " " + sign1 + " (" + frac_Handle.fracToStr(
                        secondNum) + " " + sign2 + " " + frac_Handle.fracToStr(thirdNum) + ") ="

                    firstSum = frac_Handle.fracAccount(secondNum, thirdNum, sign2)
                    if firstSum != False:
                        rightAnswer = frac_Handle.fracAccount(firstNum, firstSum, sign1)
                        if rightAnswer != False:
                            rightAnswer = frac_Handle.fracToStr(rightAnswer)
                        else:
                            continue
                    else:
                        continue

                with open("Exercise.txt", 'a') as exercise_file:
                    exercise_file.write(question + "\n")
                    exercise_file.close()
                with open("Answer.txt", 'a') as Answer_file:
                    Answer_file.write(str(order) + ". " + rightAnswer + "\n")
                    Answer_file.close()
                break
            # 运算式子重复
            else:
                continue

测试运行

  • 题目生成
  • 题目答案

posted @ 2023-09-28 19:06  kklvu  阅读(51)  评论(0编辑  收藏  举报