- 项目要求
1能自动生成小学四则运算题目(注意是给小学生用的,要是结果出现负数的话他们会迷茫的!)
2除了整数外,还要支持真分数的四则运算
- PSP表格
PSP | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
Planning | 计划 | 25 | 35 |
Estimate | 估计这个任务需要多少时间 | 30 | 30 |
Development | 开发 | 240 | 330 |
Analysis | 需求分析 (包括学习新技术) | 60 | 70 |
Design Spec | 生成设计文档 | 30 | 30 |
Design Review | 设计复审 (和同事审核设计文档) | 30 | 60 |
Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 30 | 30 |
Design | 具体设计 | 30 | 45 |
Coding | 具体编码 | 120 | 200 |
Code Review | 代码复审 | 30 | 30 |
Test | 测试(自我测试,修改代码,提交修改) | 30 | 30 |
Reporting | 报告 | 20 | 30 |
Test Report | 测试报告 | 20 | 40 |
Size Measurement | 计算工作量 | 30 | 10 |
Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 30 | 30 |
合计 | 755 | 1000 |
1 import tkinter 2 import random 3 from fractions import Fraction 4 5 m=['+','-','×','÷'] 6 nn='0' 7 8 window = tkinter.Tk() 9 window.title('四则运算') 10 window.geometry('500x250') 11 m1=tkinter.StringVar() 12 m1.set('') 13 m2=tkinter.StringVar() 14 m2.set('') 15 m3=tkinter.StringVar() 16 m3.set('') 17 m4=tkinter.StringVar() 18 m4.set('') 19 name=tkinter.Label(text='算式:',justify=tkinter.RIGHT,width=80) 20 name.place(x=5,y=5,width=80,height=20) 21 m1name=tkinter.Label(width=80,textvariable=m1) 22 m1name.place(x=60,y=5,width=80,height=20) 23 en=tkinter.Entry(width=80,textvariable=m3) 24 en.place(x=180,y=5,width=80,height=20) 25 name2=tkinter.Label(text='结果:',justify=tkinter.RIGHT,width=80) 26 name2.place(x=5,y=30,width=80,height=20) 27 m2name=tkinter.Label(width=80,textvariable=m2) 28 m2name.place(x=60,y=30,width=80,height=20) 29 name3=tkinter.Label(text='答案:',justify=tkinter.RIGHT,width=80) 30 name3.place(x=5,y=60,width=80,height=20) 31 m4name=tkinter.Label(width=80,textvariable=m4) 32 m4name.place(x=75,y=60,width=50,height=20) 33 34 def s(): 35 b['text']='下一题' 36 b2['text'] = '真分数' 37 m1.set('') 38 m2.set('') 39 m3.set('') 40 m4.set('') 41 mm = f() 42 a = mm 43 def g(a): 44 nn = en.get() 45 if str(nn) == str(mm): 46 m2.set('正确') 47 else: 48 m2.set('错误') 49 m4.set(mm) 50 en.bind('<Key-Return>', g) 51 def s1(): 52 b['text'] = '整数' 53 b2['text']='下一题' 54 m1.set('') 55 m2.set('') 56 m3.set('') 57 m4.set('') 58 mm = f2() 59 a=mm 60 def g(a): 61 nn = en.get() 62 if str(nn) == str(mm): 63 m2.set('正确') 64 else: 65 m2.set('错误') 66 m4.set(mm) 67 en.bind('<Key-Return>', g) 68 69 def s2(): 70 window.destroy() 71 b=tkinter.Button(text='整数',command=s) 72 b.place(x=60,y=90,width=50,height=20) 73 b2=tkinter.Button(text='真分数',command=s1) 74 b2.place(x=120,y=90,width=50,height=20) 75 b1=tkinter.Button(text='关闭',command=s2) 76 b1.place(x=180,y=90,width=50,height=20) 77 78 def f(): 79 f=random.randint(0,3) 80 n1=random.randint(1,10) 81 n2=random.randint(1,10) 82 r = 0 83 if f==0: 84 r=n1+n2 85 elif f==1: 86 n1,n2=max(n1,n2),min(n1,n2) 87 r=n1-n2 88 elif f==2: 89 r=n1*n2 90 elif f==3: 91 if n1==n2: 92 r=1 93 else: 94 n1, n2 = max(n1, n2), min(n1, n2) 95 if n2==1: 96 r=n1 97 else: 98 while n1 % n2 != 0: 99 n1 = random.randint(1, 10) 100 n2 = random.randint(1, 10) 101 n1, n2 = max(n1, n2), min(n1, n2) 102 r = int(n1 / n2) 103 a=str(n1)+str(m[f])+str(n2)+'=' 104 m1.set(a) 105 return r 106 107 def f2(): 108 q = [] 109 f = random.randint(0, 3) 110 t1 = random.randint(0, 20) 111 t2 = random.randint(t1, 20) 112 a = Fraction(t1, t2) 113 t1 = random.randint(0, 20) 114 t2 = random.randint(t1, 20) 115 b = Fraction(t1, t2) 116 if f==0: # 加法 117 q.append(a + b) 118 q.append(str(a)+m[f]+str(b)) 119 m1.set(q[1]) 120 return q[0] 121 elif f==1: # 减法 122 if a < b: 123 tm = a 124 a = b 125 b = tm 126 q.append(a - b) 127 q.append(str(a) + m[f] + str(b)) 128 m1.set(q[1]) 129 return q[0] 130 elif f==2: # 乘法 131 q.append(a * b) 132 q.append(str(a) + m[f] + str(b)) 133 m1.set(q[1]) 134 return q[0] 135 else: # 除法 136 c = Fraction(a, b) 137 q.append(str(c)) 138 q.append(str(a) + m[f] + str(b)) 139 m1.set(q[1]) 140 return q[0] 141 142 window.mainloop()
函数说明:
def s() #按钮事件,判断整数部分答案是否正确
def s1() #按钮事件,判断真分数部分答案是否正确
def s2() #按钮事件,关闭窗口
def f() #计算整数部分的算式及答案
def f1() #计算真分数部分的算式及答案
整数测试界面:
真分数测试界面: