要求0
作业要求博客:https://edu.cnblogs.com/campus/nenu/2016CS/homework/2266
要求1
git仓库地址:https://git.coding.net/neneee/f4.git
百度网盘链接:https://pan.baidu.com/s/1xdEPIGrmMGWihGihMwPEVg
exe过大存放至网盘
要求2
- 结对编程同学
潘乐冰:https://www.cnblogs.com/panlb/
- 介绍解题思路:
功能一要接受输入,生成n次4个随机整数和3个随机运算符,进行+、-、*、/四则运算。
对于除法要额外考虑将分母通分以避免出现精度错误,并且不能除0;
对于小数位数要做相应处理;判断用户答案并计数
功能二在功能一的基础上要随机生成小数,随机出现(),
功能三在功能二的基础上要对表达式除重,生成指定路径文件,保证格式正确
- 每个功能的重点/难点:
功能一:重点生成随机元素,除法的改进;难点除法的改进,结果是整数不能输出小数点
功能二:重点/难点对()进行添加及运算
功能三:重点文件读写;难点表达式除重,格式调整
- 编程结果展示:
功能一:
功能二:
功能三:
- 展示重要代码片断:
判断用户输入是否合法:
arg列表存放用户输入
1 try: 2 n = int(arg[2]) 3 if(n <= 0): 4 print("题目数量范围为1到100之间的正整数。") 5 os._exit(0) 6 7 #此处省略处理代码 8 9 except: 10 print("题目数量范围为1到100之间的正整数。") 11 os._exit(0)
生成随机元素:
得到运算数列表num,运算符列表op
1)功能一
1 operator = ['+','-','*','/'] 2 numList = np.random.randint(1,10,4) 3 num = list(numList)
4 5 opList = np.random.randint(0,4,3) 6 op = [] 7 for i in opList: 8 op.append(operator[i])
2)功能二
1 operator = ['+', '-', '*', '/'] 2 numList = np.random.randint(1, 10, 5) 3 num = list(numList) 4 index = np.random.randint(0, 4) 5 if (np.random.random() > 0.5): 6 num[index] = round(np.random.uniform(1, 10), 2) 7 del num[4] 8 else: 9 num.remove(num[index]) 10 11 opList = np.random.randint(0, 4, 3) 12 op = [] 13 for i in opList: 14 op.append(operator[i]) 15 if (np.random.random() > 0.1): 16 index1 = np.random.randint(0, 4) 17 index2 = np.random.randint(0, 4) 18 if index1 - index2 < 2 and index1 - index2 > -2: 19 i 20 elif index1 > index2: 21 if index1 == 4: 22 op.append(')') 23 op.insert(index2, '(') 24 else: 25 op.insert(index2, '(') 26 op.insert(index1, ')') 27 else: 28 if index2 == 4: 29 op.append(')') 30 op.insert(index1, ')') 31 else: 32 op.insert(index1, '(') 33 op.insert(index2, ')')
文件读写:
path为文件存储路径;生成文件的格式,控制表达式的右对齐而不是结果的左对齐
1 path = arg[4] 2 f = open(path,"w") 3 4 f.write(s.ljust(15, " ")) 5 cal_ans = eval(s) 6 ans_list = re.split('(\.)', str(cal_ans)) 7 if (len(ans_list) > 1): 8 if (len(ans_list[2]) > 5): 9 ans_list[2] = ans_list[2][0:3] 10 cal_ans = ''.join([str(x) for x in ans_list]) 11 if (ans_list[2] == '0'): 12 cal_ans = ans_list[0] 13 14 f.write("\t") 15 f.write(str(cal_ans)) 16 f.write("\n")
- 展示你感觉得意、突破、困难的地方:
带括号表达式的连接
1 (num, op) = getEle2() 2 s="" 3 lenOp = len(op) 4 lenNum = len(num) 5 if lenOp == 3: 6 for index, i in enumerate(num): 7 s += str(i) 8 if index <= lenOp - 1: 9 s += op[index] 10 else: 11 k = 0 12 if op[0] == '(': 13 s += op[0] 14 k += 1 15 for index, i in enumerate(num): 16 s += str(i) 17 if index + k < lenOp: 18 s += op[index + k] 19 if index + k + 1 < lenOp and op[index + k + 1] == '(': 20 k += 1 21 s += op[index + k] 22 elif index + k + 1 < lenOp and op[index + k] == ')': 23 k += 1 24 s += op[index + k]
处理整数的小数点以及小数:
1 ans_list = re.split('(\.)', str(cal_ans)) 2 if (len(ans_list)>1): 3 if (len(ans_list[2]) > 5): 4 ans_list[2] = ans_list[2][0:3] 5 cal_ans = ''.join([str(x) for x in ans_list]) 6 if (ans_list[2] == '0'): 7 cal_ans = ans_list[0]
- 给出结对编程的体会:
1、编程是快乐的;
2、结对编程是更快乐的,思想的碰撞让我们互相促进,互相提高;
3、分工协作非常有必要;
4、遇到困难十分需要冷静思考;
5、简单方法通常能有效的解决问题;
6、要尽量了解使用的语言,能省不少事儿。
- 在编码、争论等活动中花费时间较长,给你较大收获的事件:
思想步调一致,基本没有冲突
花费时间较长在于对于()计算的处理:重要的是理清思路,对于报错信息可以先预处理,以便明确出错原因
- 结对编程留念:
- 题外话: python 打包成exe多数会比较大,不适用。不好搞啊不好搞。