队友:http://home.cnblogs.com/u/SurverDevin/
一、设计思路
主要是将四则运算设计成图形界面,因为我们一直用的是Python语言,所以继续使用python语言进行编写。由于python web以及脚本语言的连接近期没有学会,所以只能用图形界面。
运用python的自身空间GUI,Tkinter编程图形界面
整个图形界面基于上次的实验,先制图形界面,然后在对其进行完善
因为环境问题浪费了很多时间,最终没有用python 的web开发框架开发。
选择了GUI。
具体思路:定义函数,制作运算式的函数,运算运算式的结果的函数,然后制作可视化界面要用到的函数
即事件,确定事件发生时用于获取答案,计算结果,
判断结果正误功能组成.下一题事件是由制作运算式,显示运算式组成
结束函数是由统计结果显示结果组成
其他用来显示信息
二、代码
#encoding=utf-8 # -*- coding: cp936 -*- from Tkinter import * from fractions import Fraction#分数 from random import randint#随机数 str_timu="3+2/6*4+4/7=" ans=1 global ans yici=0 tihao=1 def replace(line): line=line.replace('+',' + ') line=line.replace('-',' - ') line=line.replace('*',' * ') line=line.replace('/',' / ') line=line.replace('(',' ( ') line=line.replace(')',' ) ') line=line.replace(' ',' ') line=line.replace('=',' = ') return line def calculate(operator_cal,operator_num1,operator_num2): answer=0 if(operator_cal=="+"): answer=operator_num1+operator_num2 if(operator_cal=="-"): answer=operator_num1-operator_num2 if(operator_cal=="*"): answer=operator_num1*operator_num2 if(operator_cal=="/"): answer=operator_num1/operator_num2 #print"####结果",answer return answer def result_get(str1): operator_anw=[""]*100#存取运算符的数组 operator_ord=0#指针,计算运算符位置,统计运算符的个数 figure_anw=[0]*100#存取运算数的数组 figure_ord=0#指针,计算运算数位置,统计运算数的个数 line=replace(str1) #print line line = line.split() for word in line: #print "word:",word if(word=="+"): operator_anw[operator_ord]="+" operator_ord=operator_ord+1 elif(word=="-"): operator_anw[operator_ord]="-" operator_ord=operator_ord+1 elif(word=="*"): operator_anw[operator_ord]="*" operator_ord=operator_ord+1 elif(word=="/"): operator_anw[operator_ord]="/" operator_ord=operator_ord+1 elif(word=="("): operator_anw[operator_ord]="(" operator_ord=operator_ord+1 elif(word==")"): operator_anw[operator_ord]=")" operator_ord=operator_ord+1 elif(word=="="): if(operator_ord==2):#如果出现运算符剩两个的情况,运算第二个运算符 figure_anw[1]=calculate(operator_anw[1],figure_anw[1],figure_anw[2]) figure_anw[0]=calculate(operator_anw[0],figure_anw[0],figure_anw[1]) #print figure_anw[0],"end" return figure_anw[0] else: word=int(word) word=Fraction(word,1) figure_anw[figure_ord]=word figure_ord=figure_ord+1 #print "已存入数字",word #print "下一个数字位置",figure_ord #print "下一个运算符位置",operator_ord #print operator_anw #print figure_anw #判断并进行运算,进栈出栈 #优先级进行判断,是否入栈是否运算(+—同一类,*/同一类) #*+问题 if((word=="+"or word=="-")and operator_ord>1 and (operator_anw[operator_ord-1]=="*" or operator_anw[operator_ord-1]=="/")): figure_anw[figure_ord-2]=calculate(operator_anw[operator_ord-2],figure_anw[figure_ord-2],figure_anw[figure_ord-1]) operator_anw[operator_ord-1]="" figure_anw[figure_ord-1]=0 operator_ord=operator_ord-1 operator_anw[operator_ord-1]=word figure_ord=figure_ord-1 #print operator_anw #print figure_anw if(word==")"):#1判断是否出现右括号 #运算函数 #if 如果出现+*两层运算问题,这个if解决第一层*/ if(operator_anw[operator_ord-3]=="+" or operator_anw[operator_ord-3]=="-"): figure_anw[figure_ord-2]=calculate(operator_anw[operator_ord-2],figure_anw[figure_ord-2],figure_anw[figure_ord-1]) figure_anw[figure_ord-1]=0 operator_anw[operator_ord-1]="" operator_anw[operator_ord-2]=")" figure_anw[figure_ord-1]=0 operator_ord=operator_ord-1 figure_ord=figure_ord-1 #这段是将括号中残存的唯一运算符进行运算并消掉括号和运算符 figure_anw[figure_ord-2]=calculate(operator_anw[operator_ord-2],figure_anw[figure_ord-2],figure_anw[figure_ord-1]) figure_anw[figure_ord-1]=0 operator_anw[operator_ord-3]="" operator_anw[operator_ord-2]="" operator_anw[operator_ord-1]="" operator_ord=operator_ord-3 figure_ord=figure_ord-1 #+*+问题 解决 if((word=="+"or word=="-")and (operator_anw[operator_ord-2]=="*" or operator_anw[operator_ord-2]=="/")and operator_ord>1): figure_anw[figure_ord-2]=calculate(operator_anw[operator_ord-2],figure_anw[figure_ord-2],figure_anw[figure_ord-1]) operator_anw[operator_ord-2]=word operator_anw[operator_ord-1]="" figure_anw[figure_ord-1]=0 figure_ord=figure_ord-1 operator_ord=operator_ord-1 #print"*+" #++问题 if((word=="+"or word=="-")and operator_ord>1): #print "************************************************",operator_anw[operator_ord-2] #print operator_anw[operator_ord-2] if(operator_anw[operator_ord-2]=="+"or operator_anw[operator_ord-2]=="-"): figure_anw[figure_ord-2]=calculate(operator_anw[operator_ord-2],figure_anw[figure_ord-2],figure_anw[figure_ord-1]) operator_anw[operator_ord-1]="" figure_anw[figure_ord-1]=0 operator_ord=operator_ord-1 operator_anw[operator_ord-1]=word figure_ord=figure_ord-1 #print operator_anw #print figure_anw def layer(layer_accual2,operat_number2,brackets2,layer_amount2):#递归程序 global operator if(layer_accual2>0):#对第一层开始计算,将形成3个以上的数字,层数暂时为设定的3。 #选择数字标号 #print"layer_accual2",layer_accual2 opreation_radom=randint(0,layer_accual2-1)#第一层加1,抽取号码,进行替换 find_operat_number=operat_number[opreation_radom] #即两个数中选择一个数进行替换成为一个简单的四则二元运算 #print "operater_num",operater_num #将选中的数字从第二层开始,用一个简单的二元运算式替换选中的数字,并插入数组 #插入时依据数字编号判断是否加入括号,依据此数字所在的周围是否有*符号 #判断是否有添加括号 if((operator[opreation_radom]=="-")or operator[opreation_radom+1]=="-")or operator[opreation_radom]=="/"or(operator[opreation_radom]=="*")or(operator[opreation_radom+1]=="/")or(operator[opreation_radom+1]=="*"):#判断选中数字周围的符号 brackets[layer_accual2]=1 if(multiplication_and_division==2): brackets[layer_accual2]=0 operater_num=randint(1,multiplication_and_division) #将运算符入数组 operator_one="?" #operater_num=2 if(operater_num==1): operator_one="+" if(operater_num==2): operator_one="-" if(operater_num==3): operator_one="*" if(operater_num==4): operator_one="/" if(layer_accual2==0): operator[1]=operator_one else: mov_amount=layer_accual2+2-opreation_radom for i in range(0,mov_amount): operator[layer_accual2+2-i]=operator[layer_accual2+2-i-1] operator[opreation_radom+1]=operator_one zhen_zheng=randint(1,2) #是真分数或者整数,随机 if(fraction_exist==0): zhen_zheng=1 if(zhen_zheng==1): #产生第一个数字 first_num=randint(1,number_range) first_num=str(first_num) else: first_num1=2 first_num2=1 while (first_num1>=first_num2): first_num1=randint(1,number_range) first_num2=randint(1,number_range) first_num=Fraction(first_num1,first_num2) if(first_num!=0): first_num="("+str(first_num)+")" first_num=str(first_num) zhen_zheng=randint(1,2) #是真分数或者整数,随机 if(fraction_exist==0): zhen_zheng=1 if(zhen_zheng==1): #产生第二个数字 second_num=randint(1,10) second_num=str(second_num) else: second_num1=2 second_num2=1 while (second_num1>=second_num2): second_num1=randint(1,number_range) second_num2=randint(1,number_range) second_num=Fraction(second_num1,second_num2) if(second_num!=0): second_num="("+str(second_num)+")" if(layer_accual2==0):#第0层,将最开始的两个数字存入数组 operat_number[0]=first_num operat_number[1]=second_num if(negative_exit==0):#(如果不存在负数) if(second_num>first_num and operator_one==2): while(second_num>=first_num): second_num=randint(1,number_range) if(remainder==0):#(如果不存在余数) if(operator_one==4): while(second_num%first_num!=0): second_num=randint(1,number_range) #从第一层开始存入两个数字 if(layer_accual2>0): mov_amount=layer_accual2+2-opreation_radom for i in range(0,mov_amount): operat_number[layer_accual2+1-i]=operat_number[layer_accual2+1-i-1] operat_number[opreation_radom]=first_num operat_number[opreation_radom+1]=second_num #整理算式 if(layer_accual2<1): expressions="" if(layer_accual2==1): tempperate1=str(operat_number[0]) tempperate2=str(operat_number[1]) expressions=operat_number[0]+operator[1]+operat_number[1] if(layer_accual2>1): #先找到替换数字,然后产生表达式2,用2替换表达式1 global expressions kk=str(operat_number[opreation_radom]) expressions2=first_num+operator_one+second_num if ( brackets[layer_accual2]==1): expressions2="("+first_num+operator_one+second_num+")" #创建一个查找机制,寻找不同的数字将其替换? #while(same_amount>0): #print"上一层句子",expressions #print"替换句子",expressions2 #print"用于替换的的数字",find_operat_number expressions=expressions.replace(find_operat_number," "+find_operat_number+" ") expressions3="" recording_1=0 line=expressions.split() for word2 in line: if (word2==find_operat_number and recording_1==0): word2=expressions2 recording_1=1 expressions3=expressions3+word2 expressions3=expressions3.replace(" ","") expressions=expressions3 layer_accual2=layer_accual2+1 if(layer_accual2<layer_amount2+1): layer(layer_accual2,operat_number2,brackets2,layer_amount2) #if(layer_accual==layer_amount2): #return expressions def queding_event():#确定事件,将输入框里的字符转化成为答案,并判断是否正确 if(yici==0): #判断结果 global yici tishi_panduan=Label(root,text=" ") tishi_panduan.grid(row=4,column=1) global ans global expressions answer_calculate=result_get(expressions) answer_user=cin.get() if(answer_user==""): tishi_panduan=Label(root,text="输入错误,请重新输入") tishi_panduan.grid(row=0,column=1) return find_chu=0 tishi_panduan=Label(root,text=" ") tishi_panduan.grid(row=4,column=1) for i in answer_user: if(i=="/"): find_chu=1 answer_user=answer_user.split("/") answer_user_matrix_momentary=[0]*2 i=0 for answer_user_i in answer_user: answer_user_matrix_momentary[i]=int(answer_user_i) i=i+1 answer_user=Fraction(answer_user_matrix_momentary[0],answer_user_matrix_momentary[1]) if(find_chu==0): answer_user=Fraction(int(answer_user),1) ans=answer_user if(ans==answer_calculate): tishi_panduan=Label(root,text="√",bg="green") tishi_panduan.grid(row=1,column=4) global right_amount right_amount=right_amount+1 else: tishi_panduan=Label(root,text="×",bg="red") tishi_panduan.grid(row=1,column=4) zhuanhua=str(answer_calculate) zhuanhua="正确答案为"+zhuanhua tishi_cuowu=Label(root,text=zhuanhua,bg="red") tishi_cuowu.grid(row=3,column=1) yici=1 def xiayiti_event(): if(counter1>expressions_amount): tishi3=Label(root,text="题已做完,请提交",bg="red") tishi3.grid(row=3,column=0) return 0 expressions="" tishi_cuowu=Label(root,text=' ') tishi_cuowu.grid(row=3,column=1) cin.delete('0', 'end')#输入框清除其中内容 layer_accual=0#层数 operator=['k']*(layer_amount+3)#记录运算符的记录 operat_number=["?"]*(layer_amount+2)#记录运算数的记录器 brackets=[0]*(layer_amount+1)#记录括号的存在标志 operator[0]="?" operator[2]="?" global ounter1 global layer_amount global operator global layer_accual global operat_number global brackets global expressions layer(layer_accual,operat_number,brackets,layer_amount) expressions=expressions+"=" timu=Label(root,text=" ") timu.grid(row=1,column=0) tishi_panduan=Label(root,text=" ") tishi_panduan.grid(row=1,column=4) timu=Label(root,text=expressions) timu.grid(row=1,column=0) global yici global ans global counter1 yici=0 ans=11 counter1=counter1+1 tishi1=Label(root,text=" ") tishi1.grid(row=0,column=0) tishi1=Label(root,text=" ") tishi1.grid(row=0,column=0) num=str(counter1) str_tihao="第"+num+"题:" tishi1=Label(root,text=str_tihao) tishi1.grid(row=0,column=0) def jieshu(): global counter1 global right_amount counter1=str(counter1) counter1="做题数目:"+counter1 tishu=Label(root,text=counter1) tishu.grid(row=7,column=0) right_amount=str(right_amount) right_amount="答对数目:"+right_amount dadui=Label(root,text=right_amount) dadui.grid(row=8,column=0) ##############程序开始 expressions_amount=99999#算式数量 layer_amount=3 #层数,即数的个数 number_range=20#整数数值的大小范围 fraction_exist=0#是否有分数 multiplication_and_division=4#是否有乘除,有则为4 negative_exit=1#负数是否存在,1存在 remainder=1#余数是否存在,1存在 pritenr=1#打印机模式 quit_num=1#退出的标志 #print "expressions_amount",expressions_amount counter1=0 right_amount=0 global counter1 global right_amount right_amount=0 answer_matrix=[0]*expressions_amount answer_matrix_human=[0]*expressions_amount root=Tk() root.geometry('450x300+0+0') num=0 num=str(num) str_tihao=" " #显示题号 tishi1=Label(root,text=str_tihao) tishi1.grid(row=0,column=0) #显示题目 timu=Label(root,text='请点击开始按钮,开始答题') timu.grid(row=1,column=0) #输入框 cin=Entry(root) cin.grid(row=1,column=1) #确定按钮 queding=Button(root,text='确定',command=queding_event) queding.grid(row=1,column=3) #答案正误判断 #提交 tijiao=Button(root,text='提交',command=queding_event) tijiao.grid(row=3,column=3) global tijiao #下一题 next_ti=Button(root,text='下一题',command=xiayiti_event) next_ti.grid(row=3,column=3) #确定按钮 queding=Button(root,text='开始',command=xiayiti_event) queding.grid(row=6,column=0) #结束按钮 queding=Button(root,text='结束',command=jieshu) queding.grid(row=6,column=1) #k=Message(root,text = 'hello Message') #k.grid(row=1,column=0) root.mainloop()
3、截图