第三周作业06

此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/7631

代码地址https://e.coding.net/cjw_123/cptj.git

结对伙伴:位军营

1.项目分析及编程收获:

1.1 功能一

要求:四则运算:支持出题4个数的四则运算题目

1.1.1功能一重点、难点

(1)对数字进行随机

(2)将运算符存入列表随机输出

(3)将字符串存入eval()运算

1.1.2代码:

代码:

import random
def Get_Problem():
    opratiorList = ['+','-','*','/']
    numList = [2,4,5,8]
    opration1 = random.choice(opratiorList)
    opration2 = random.choice(opratiorList)
    opration3 = random.choice(opratiorList)
    a = random.randint(1,9)
    if opration1 == '/':
        b = random.choice(numList)
    else:
        b = random.randint(1,9)
    if opration2 == '/':
        c = random.choice(numList)
    else:
        c = random.randint(1,9)
    if opration3 == '/':
        d = random.choice(numList)
    else:
        d = random.randint(1,9)
    
    ToString = str(a)+opration1+str(b)+opration2+str(c)+opration3+str(d)
    print(ToString+'=')
    result = eval('%d%s%d%s%d%s%d'%(a,opration1,b,opration2,c,opration3,d))

    return result

 

1.2 功能二

要求:支持括号

1.2.1功能一重点、难点

(1)对括号的随机处理

(2)对有括号的运算顺序的逻辑处理

1.2.2代码:

 def calculate(exp):
        loc = 0
        count = 0
        # 读入第一个数字/整体(被括号括起来的)
        firstNum = exp[loc]
        if firstNum != "(":
            result = float(firstNum)
            loc += 1
        else:
            locStart = loc + 1
            count += 1  # 为了将( 与 ) 进行匹配
            while count != 0:
                loc += 1
                if exp[loc] == "(":
                    count += 1
                elif exp[loc] == ")":
                    count -= 1
            locEnd = loc
            loc += 1
            result = calculate(exp[locStart:locEnd])
        while loc < len(exp):
            operator = exp[loc]
            loc += 1
            # 完善第一个整体,即若后面有跟*或者/时读进来计算为一个整体
            while operator == "*" or operator == "/":
                secNum = exp[loc]
                if secNum != "(":
                    loc += 1
                else:
                    locStart = loc+1
                    count += 1  # 为了将( 与 ) 进行匹配
                    while count != 0:
                        loc += 1
                        if exp[loc] == "(":
                            count += 1
                        elif exp[loc] == ")":
                            count -= 1
                    locEnd = loc
                    loc += 1
                    secNum = calculate(exp[locStart:locEnd])
                if result == "worngproblem":
                        return "worngproblem"
                elif secNum == "worngproblem":
                        return "worngproblem"
                elif operator == "*":
                        result = result * float(secNum)
                elif float(secNum) == 0:
                        return "worngproblem"
                else:
                        result = result / float(secNum)
                if loc >= len(exp):
                    break
                operator = exp[loc]
                loc += 1
            # 加号或者减号
            operator1 = operator
            if loc >= len(exp):
                break
            secNum = exp[loc]
            if secNum != "(":
                secNum = float(secNum)
                loc += 1
            else:
                locStart = loc + 1
                count += 1  # 为了将( 与 ) 进行匹配
                while count != 0:
                    loc += 1
                    if exp[loc] == "(":
                        count += 1
                    elif exp[loc] == ")":
                        count -= 1
                locEnd = loc
                loc += 1
                secNum = calculate(exp[locStart:locEnd])
            # 完善第二个加数/减数
            if loc < len(exp):
                operator = exp[loc]
                loc += 1
                flag = False
                while operator == "*" or operator == "/":
                    flag = True
                    thirdNum = exp[loc]
                    if thirdNum != "(":
                        loc += 1
                    else:
                        locStart = loc+1
                        count += 1  # 为了将( 与 ) 进行匹配
                        while count != 0:
                            loc += 1
                            if exp[loc] == "(":
                                count += 1
                            elif exp[loc] == ")":
                                count -= 1
                        locEnd = loc
                        loc += 1
                        thirdNum = calculate(exp[locStart:locEnd])
                    if operator == "*":
                        secNum = secNum * float(thirdNum)
                    elif float(thirdNum) == 0:
                        return "worngproblem"
                    else:
                        secNum = secNum / float(thirdNum)
                    if loc >= len(exp):
                        break
                    operator = exp[loc]
                    loc += 1
                if not flag:
                    loc -= 1
            if result == "worngproblem":
                return "worngproblem"
            elif secNum == "worngproblem":
                return "worngproblem"
            elif operator1 == "+":
                result += float(secNum)
            else:
                #print(secNum)
                result -= float(secNum)
            if loc >= len(exp):
                break
        return result

1.3 功能三

要求:限定题目数量,打印输出,避免重复

1.3.1功能一重点、难点

(1)将已有的表达式存入列表进行比较 若已打印过则重新生成

(2)对输入的数字进行判断 只可以的正整数

1.3.2代码:

def main():
    if (sys.argv[1]=='-c'):
        
        list1 = []
        strnumofproblem = sys.argv[2]
        if strnumofproblem.isdecimal() == False:
            print("题目数量必须是 正整数。")
        else:
            intnumofproblem = int(strnumofproblem)
            for _ in range(intnumofproblem):       #避免生成重复式子
                strnum = Get_Problem()
                if strnum in list1:
                    intnumofproblem += 1
                elif Get_Result(strnum) == "worngproblem":
                    intnumofproblem += 1
                else:
                    list1.append(strnum)
                    print("%-30s %g"% (strnum+'=',Get_Result(strnum)))

2结对编程体会

  在结对编程中体会到了合作的重要性,以及对代码规范的必要性。在必要的规范下才能不产生歧义,才能很好的交流和沟通,这使我们体会到了真正工作环境下的实际环境。对我们以后的工作学习有很大帮助。

3花费时间较长的事

1、对作业的解读与设计

2、对bug的修改

3、对细节的分析

4工作照片

第三周作业06_第1张图片

5代码地址https://cjw_123.coding.net/p/cptj/git

你可能感兴趣的:(第三周作业06)