位军营 20190919-6 代码规范,结对要求
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/7631
代码地址https://cjw_123.coding.net/p/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的修改,我们两个由于是都刚刚接触python,所以都还不太熟悉,所以对于空格和tab缩进时不能同时使用这一点大为头痛,找了好久没找到原因(因为有时候看着真的是没有混用),解决掉这个bug之后也是大为舒心,感觉上像是多了一项技能。
3、对细节的分析,刚上手时并没有意识到题目的难度,越做到后面就看到了越多的题目细节。