小学数学算数出题程序
程序调试运行过程:
① 开始的输入界面,可以选择计算的最大数值限制和题目个数。
② 题做对后,会有“√”号出现,表明评判完毕,题目做对。
③ 题做错后,会有“×”号出现,表明评判完毕,题目做错。
④ 除法运算,输入完商后,要求输入余数。
⑤ 分数的运算。
⑥ 做完规定的5道题后,会要求选择继续或退出。
我自认为自己做的这个小程序功能还是比较全面的,主要利用随机数的产生从而随机出题,函数较多,所以代码敲的可能有点多,有不足之处,还希望老师指正!
以下为程序源代码
#include "iostream" #include "stdio.h" #include "stdlib.h" #include "math.h" using namespace std; int m,sum; int com (int x,int y) //求最大公约数 { int p; if (y>x) { p=x; x=y; y=p; } while(x%y!=0) { p=x%y; x=y; y=p; } return y; } void add () //求加法 { int i,j,l; i=rand()%m; j=rand()%m; cout<<i<<" + "<<j<<" = "; cin>>l; if (l==i+j) { sum++; cout<<" √"<<endl; } else cout<<" ×"<<endl; return; } void sub () //求减法 { int i,j,l; i=rand()%m; j=rand()%(i+1); cout<<i<<" - "<<j<<" = "; cin>>l; if (l==i-j) { sum++; cout<<" √"<<endl; } else cout<<" ×"<<endl; return; } void mul () //求乘法 { int i,j,l; i=rand()%m; j=rand()%m; cout<<i<<" × "<<j<<" = "; cin>>l; if (l==i*j) { sum++; cout<<" √"<<endl; } else cout<<" ×"<<endl; return; } void div () //求除法 { int i,j,l,o; i=rand()%m; j=rand()%i+1; cout<<i<<" ÷ "<<j<<" = "; cin>>l; cout<<" 余数 : "; cin>>o; if ((l==i/j)&&(o==i%j)) { sum++; cout<<" √"<<endl; } else cout<<" ×"<<endl; return; } void fadd () //求分数相加 { int i1,j1,i2,j2,i3,j3,l,o,a,b,c; char g; i1=rand()%m; j1=rand()%(m-1)+1; i2=rand()%m; j2=rand()%(m-1)+1; c=com(j1,i1); j1=j1/c; i1=i1/c; c=com(j2,i2); j2=j2/c; i2=i2/c; while (j1==1) { j1=rand()%(m-1)+1; c=com(j1,i1); j1=j1/c; i1=i1/c; } while (j2==1) { j2=rand()%(m-1)+1; c=com(j2,i2); j2=j2/c; i2=i2/c; } cout<<i1<<"/"<<j1<<" + "<<i2<<"/"<<j2<<" = "; cin>>l>>g>>o; j3=j1*j2; i3=i1*j2+i2*j1; a=i3; b=j3; c=com(a,b); if ((l==a/c)&&(o==b/c)) { sum++; cout<<" √"<<endl; } else cout<<" ×"<<endl; return; } void fsub () //求分数相减 { int i1,j1,i2,j2,i3,j3,l,o,a,b,c,p; char g; i1=rand()%m; j1=rand()%(m-1)+1; i2=rand()%m; j2=rand()%(m-1)+1; c=com(j1,i1); j1=j1/c; i1=i1/c; c=com(j2,i2); j2=j2/c; i2=i2/c; while (j1==1) { j1=rand()%(m-1)+1; c=com(j1,i1); j1=j1/c; i1=i1/c; } while (j2==1) { j2=rand()%(m-1)+1; c=com(j2,i2); j2=j2/c; i2=i2/c; } if (i1/j1<i2/j2) { p=j2; j2=j1; j1=p; p=i2; i2=i1; i1=p; } cout<<i1<<"/"<<j1<<" - "<<i2<<"/"<<j2<<" = "; cin>>l>>g>>o; j3=j1*j2; i3=i1*j2-i2*j1; a=i3; b=j3; c=com(a,b); if ((l==a/c)&&(o==b/c)) { sum++; cout<<" √"<<endl; } else cout<<" ×"<<endl; return; } int main () //主函数 { int k,n,x; char c; cout<<"请输入题目允许的最大数(输入0退出) : "; cin>>m; if (m==0) return 0; m++; cout<<endl<<"请输入题目个数 : "; while (cin>>n!=0) { cout<<endl; x=n; sum=0; while (x--) { k=rand()%5; switch (k) //随机数随机选出类型 { case 0:add(); break; case 1:sub(); break; case 2:mul(); break; case 3:div(); break; case 4:int t=rand()%2; if (t=0) fadd(); else if (t=1) fsub(); break; } } cout<<endl<<"共做题目 "<<n<<" 道,其中做对 "<<sum<<" 道,做错 "<<n-sum<<" 道!"<<endl<<endl; cout<<"继续请按回车,退出请输入0 : "; c=getchar(); c=getchar(); if (c=='0') return 0; cout<<endl<<"请输入题目允许的最大数 : "; cin>>m; m++; cout<<endl<<"请输入题目个数 : "; } return 0; }
: