需求分析
随着IT的发展,IT在教学方面的应用也越来越广泛,一个好的教学软件能够帮助教师减轻各种不必要的负担,通过软件的自动批改,省时省力,教师可以花更多的时间在工作的其他方面,利用教学软件,同时也方便同学们学习,同学们无论在哪里,只要能用到软件,就可以开始学习,节约了用于购买练习本的费用,而软件出题的题目数量也大大多余花钱购买的题目本,所以很有必要运用该技术来服务教学行业。
功能设计
基本功能
实现基本的四则运算随机出题,答题,批改
设计实现
利用Math.random()*10方法,随机生成0-10之间的随机数,以此作为三个操作数,将+,-,*,\四个运算符存入List集合中,再利用随机生成的数取得对应的操作符,将所生成的操作数操作符拼接为一个字符串,再通过中缀表达式与后缀表达式的转换,最后利用后缀表达式求出该式子的值,并将题目,求出的值(即正确答案),用户作答,批改结果存入文本中。
实现代码
#include<stdio.h>
#include<iostream>#include<time.h>#include<stdlib.h>#include<string.h>#include<iomanip>#define random(x)(rand()%x)using namespace std;int gys(int fz,int fm){ int tmp;if(fz==fm)return fz;if(fz<fm){ tmp=fz;fz=fm;fm=tmp;}while(fz%fm!=0){ tmp=fz;fz=fm;fm=tmp%fm;}return fm;}int gbs(int n,int m){ int i; for (i=n; ; i++) if (i%n==0&&i%m==0) break; return i;}int main(){ int i,g,n,m1,m2,m3,m4,m5,m6,sum1,sum2; //i=atoi(argv[2]); cout<<"输入题数:"; cin>>i; double count1=0,count2=0; char string0[10]={'-'},string1[10],string2[10],string3[10],string4[10],string5[10]; //cin>>i; cout<<"题数:"<<i<<endl; srand((int)time(0)); for( int x=0;x<i;x++) {/*cout<<random(3)<<endl;*/ switch(random(8)){ case 0:{ m1=rand()%10+1;//rand()产生随机数 m2=rand()%10+1; count1++; cout<<m1<<"+"<<m2<<"="; cin>>sum1; if(sum1==m1+m2){ cout<<"回答正确"<<endl; count2++;} else cout<<"错误,正确答案是:"<<m1+m2<<endl; break; } case 1:{ m1=rand()%10+1; m2=rand()%10+1; count1++; cout<<m1<<"-"<<m2<<"="; cin>>sum1; if(sum1==m1-m2){ cout<<"回答正确"<<endl; count2++;} else cout<<"错误,正确答案是:"<<m1-m2<<endl; break; } case 2:{ m1=rand()%10+1; m2=rand()%10+1; count1++; cout<<m1<<"*"<<m2<<"="; cin>>sum1; if(sum1==m1*m2){ cout<<"回答正确"<<endl; count2++; } else cout<<"错误,正确答案是:"<<m1*m2<<endl; break; } case 3:{ m1=rand()%10+1; m2=rand()%10+1; count1++; g=gys(m1,m2); m2=m2/g; m1=m1/g; if(m2==1){ itoa(m1,string2,10); cout<<m1<<"÷"<<m2<<"="; cin>>string1; } else if(m2==m1){ itoa(1,string2,10); cout<<m1<<"÷"<<m2<<"="; cin>>string1; } else{ itoa(m1,string2,10); itoa(m2,string3,10); strcat(string2,"/"); strcat(string2,string3); cout<<m1<<"÷"<<m2<<"="; cin>>string1; } //string2=m1+"/"+m2; if(strcmp(string1,string2)==0){ cout<<"回答正确"<<endl; count2++;} else cout<<"错误,正确答案是:"<<string2<<endl; break; } case 4:{ m1=rand()%10+1; m2=rand()%10+1; m3=rand()%10+1; m4=rand()%10+1; count1++; if(m1>=m2){ sum2=m1; m1=m2; m2=sum2; } g=gys(m1,m2); m1=m1/g; m2=m2/g; if(m1==m2) itoa(1,string2,10); else{ itoa(m1,string2,10); itoa(m2,string3,10); strcat(string2,"/"); strcat(string2,string3);} if(m3>=m4){ sum2=m3; m3=m4; m4=sum2; } g=gys(m3,m4); m3=m3/g; m4=m4/g; if(m3==m4) itoa(1,string4,10); else{ itoa(m3,string4,10); itoa(m4,string5,10); strcat(string4,"/"); strcat(string4,string5);} cout<<string2<<"+"<<string4<<"="; cin>>string1; g=gbs(m2,m4); sum1=(g/m2*m1)+(g/m4*m3); sum2=g; g=gys(sum1,sum2); sum1=sum1/g; sum2=sum2/g; if(sum1%sum2==0) { itoa(sum1,string4,10); } else { itoa(sum1,string4,10); itoa(sum2,string5,10); strcat(string4,"/"); strcat(string4,string5); } if(strcmp(string1,string4)==0){ cout<<"回答正确"<<endl; count2++;} else cout<<"错误,正确答案是:"<<string4<<endl; break; } case 5:{ //两个分数相减 m1=rand()%10+1; m2=rand()%10+1; m3=rand()%10+1; m4=rand()%10+1; count1++; if(m1>=m2){ sum2=m1; m1=m2; m2=sum2; } g=gys(m1,m2); m1=m1/g; m2=m2/g; if(m1==m2) itoa(1,string2,10); else{ itoa(m1,string2,10); itoa(m2,string3,10); strcat(string2,"/"); strcat(string2,string3);} if(m3>=m4){ sum2=m3; m3=m4; m4=sum2; } g=gys(m3,m4); m3=m3/g; m4=m4/g; if(m3==m4) itoa(1,string4,10); else{ itoa(m3,string4,10); itoa(m4,string5,10); strcat(string4,"/"); strcat(string4,string5); } cout<<string2<<"-"<<string4<<"="; cin>>string1; g=gbs(m2,m4); sum1=(g/m2*m1)-(g/m4*m3); sum2=g; if(sum1<0) { sum1=-sum1; g=gys(sum1,sum2); sum1=sum1/g; sum2=sum2/g; if(sum1%sum2==0) { string4[0]=0; strcat(string4,"-"); itoa(sum1,string0,10); strcat(string4,string0); } else { string4[0]=0; strcat(string4,"-"); itoa(sum1,string0,10); itoa(sum2,string5,10); strcat(string4,string0); strcat(string4,"/"); strcat(string4,string5); } } else { if(sum1==0) { itoa(0,string4,10); } else { g=gys(sum1,sum2); sum1=sum1/g; sum2=sum2/g; if(sum1%sum2==0) { itoa(sum1,string4,10); } else { itoa(sum1,string4,10); itoa(sum2,string5,10); strcat(string4,"/"); strcat(string4,string5); } } } if(strcmp(string1,string4)==0){ cout<<"回答正确"<<endl; count2++;} else cout<<"错误,正确答案是:"<<string4<<endl; break; } case 6:{ //两个分数相乘 m1=rand()%10+1; m2=rand()%10+1; m3=rand()%10+1; m4=rand()%10+1; count1++; if(m1>m2){ sum2=m1; m1=m2; m2=sum2; } g=gys(m1,m2); m1=m1/g; m2=m2/g; if(m1==m2) itoa(1,string2,10); else{ itoa(m1,string2,10); itoa(m2,string3,10); strcat(string2,"/"); strcat(string2,string3); } if(m3>m4){ sum2=m3; m3=m4; m4=sum2; } g=gys(m3,m4); m3=m3/g; m4=m4/g; if(m3==m4) itoa(1,string4,10); else{ itoa(m3,string4,10); itoa(m4,string5,10); strcat(string4,"/"); strcat(string4,string5);} cout<<string2<<"*"<<string4<<"="; cin>>string1; g=gbs(m2,m4); sum1=m1*m3; sum2=m2*m4; g=gys(sum1,sum2); sum1=sum1/g; sum2=sum2/g; if(sum1%sum2==0) { itoa(sum1,string4,10); } else { itoa(sum1,string4,10); itoa(sum2,string5,10); strcat(string4,"/"); strcat(string4,string5); }if(strcmp(string1,string4)==0){
cout<<"回答正确"<<endl; count2++;} else cout<<"错误,正确答案是:"<<string4<<endl; break; } case 7:{ m1=rand()%10+1; m2=rand()%10+1; m3=rand()%10+1; m4=rand()%10+1; count1++; if(m1>=m2){ sum2=m1; m1=m2; m2=sum2; } g=gys(m1,m2); m1=m1/g; m2=m2/g; if(m1==m2) itoa(1,string2,10); else{ itoa(m1,string2,10); itoa(m2,string3,10); strcat(string2,"/"); strcat(string2,string3);} if(m3>=m4){ sum2=m3; m3=m4; m4=sum2; } g=gys(m3,m4); m3=m3/g; m4=m4/g; if(m3==m4) itoa(1,string4,10); else{ itoa(m3,string4,10); itoa(m4,string5,10); strcat(string4,"/"); strcat(string4,string5);} cout<<string2<<"÷"<<string4<<"="; cin>>string1; g=gbs(m2,m4); sum1=m1*m4; sum2=m2*m3; g=gys(sum1,sum2); sum1=sum1/g; sum2=sum2/g; if(sum1%sum2==0) { itoa(sum1,string4,10); } else { itoa(sum1,string4,10); itoa(sum2,string5,10); strcat(string4,"/"); strcat(string4,string5); } if(strcmp(string1,string4)==0){ cout<<"回答正确"<<endl; count2++;} else cout<<"错误,正确答案是:"<<string4<<endl; break; } } } //cout<<"The rate is:"<<setprecision(2)<<100*count2/count1<<endl; printf("正确率:%.2lf%%\n",count2/count1*100); return 0; }
实现测试:
存在不足
未实现题目查重功能,可能导致随机生成的题目中具有相同的题目。在本次代码中,并未实现分数计算,运算数定义为int,可能导致结果为,在除数运算中,如果分母大于分子,只会保留整数部分,如果整数部分为0的也只会保留0,导致结果出现误差,此点需要进一步优化。