这是一个关于小学生的100以内的加,减,乘,除四则运算,还有错题重做。
#include <iostream>
#include<fstream>
#include<cstdlib>
#include<string.h>
#include<ctime>
using namespace std;
voidmenu(); // 菜单函数
void add(); //加法
voidsubstract(); // 减法
voidmultify(); // 乘法
void divide(); // 除法
void generate_item(); //产生试题
voidreview_error(string filename); // 从文件读取错题
intcheck_correct(int x, int a, int b, char op); // 检查是否正确
static int n =0; // Calc Number of correction
FILE * fp;
int main()
{
cout<<"==========================================="<<endl;
cout<<"* 小学生算数运算测试系统 *"<<endl;
cout<<"= ="<<endl;
cout<<"* 1.出10道新题 *"<<endl;
cout<<"= 2.上次错题集 ="<<endl;
cout<<"* *"<<endl;
cout<<"==========================================="<<endl;
srand((unsigned)time(NULL));
menu();
return 0;
}
// 菜单函数
void menu()
{
int y;
cout<<"选择功能:";
cin>>y;
if(y!=1 && y!=2)
cout<<"输入有误!"<<endl;
else
{
switch(y)
{
case 1:
generate_item();
break;
case 2:
review_error("n.txt");
break;
}
}
}
// 产生试题
voidgenerate_item()
{
int m,i;
for(i=0;i<10;i++)
{
cout<<"==================="<<endl;
cout<<"第"<<i+1<<"题:"<<endl;
m=rand()%4;
switch(m)
{
case 0:
add();break;
case 1:
substract();break;
case 2:
multify();break;
case 3:
divide();break;
}
}
cout<<"共做对了"<<n<<"道题"<<endl;
}
void add()
{
if((fp=fopen("n.txt","a"))==NULL)
{
printf("cannot openfile\n");
exit(0);
}
int a,b,x;
char m='+',ch;
a=1+rand()%100;
b=1+rand()%100;
cout<<a<<m<<b<<'=';
cin>>x;
check_correct(x,a,b,m);
}
voidsubstract()
{
if((fp=fopen("n.txt","a"))==NULL)
{
printf("cannot openfile\n");
exit(0);
}
int a,b,x;
char m='-',ch;
a=1+rand()%100;
b=1+rand()%100;
if(a>=b)
{
cout<<a<<m<<b<<'=';
cin>>x;
check_correct(x,a,b,m);
}
else
substract();
}
intcheck_correct(int x, int a, int b, char op)
{
int cor;
char ch;
switch(op)
{
case '+':
cor = a+b;
break;
case '-':
cor = a-b;
break;
case '*':
cor = a*b;
break;
case '/':
cor = a/b;
break;
}
if(x==cor)
{
cout<<"做对了"<<endl;
n++;
}
else
{
cout<<"做错了"<<endl;
cout<<"是否输出正确答案:(Y或N)"<<endl;
cin>>ch;
if(ch=='Y' || ch=='y')
{
cout<<"正确答案为:"<<cor<<endl;
}
fprintf(fp,"%d%c%d=\n",a,op,b);
}
}
void multify()
{
if((fp=fopen("n.txt","a"))==NULL)
{
printf("cannot openfile\n");
exit(0);
}
int a,b,x;
char m='*',ch;
a=1+rand()%9;
b=1+rand()%9;
if(a<b)
{
cout<<a<<m<<b<<'=';
cin>>x;
check_correct(x,a,b,m);
}
else
multify();
}
void divide()
{
if((fp=fopen("n.txt","a"))==NULL)
{
printf("cannot openfile\n");
exit(0);
}
int a,b,x;
char m='/',ch;
a=1+rand()%9;
b=1+rand()%9;
if(a>=b&&a%b==0)
{
cout<<a<<m<<b<<'=';
cin>>x;
check_correct(x,a,b,m);
}
else
divide();
}
voidreview_error(string filename)
{
FILE * fp;
int a,b,s;
char c,d;
cout<<"查看上次错题集:"<<endl;
if((fp=fopen(filename.c_str(),"r+"))==NULL) //将C++字符串变为C字符串
{
printf("cannot openfile\n");
exit(0);
}
while(!feof(fp)) // file, end of file
{
int s,t;
fscanf(fp,"%d%c%d%c",&a,&c,&b,&d);
cout<<a<<c<<b<<d;
cin>>t;
switch(c)
{
case '+':
s=a+b;
if(s==t)
{
cout<<"做对了!";
break ;
}
else
{
cout<<"做错了!";
}
break ;
case '-' :
s=a-b;
if(s==t)
{
cout<<"做对了!";
break ;
}
else
{
cout<<"做错了!";
}
break ;
case '*':
s=a*b;
if(s==t)
{
cout<<"做对了!";
break ;
}
else
{
cout<<"做错了!";
}
break ;
case '/' :
s=a/b;
if(s==t)
{
cout<<"做对了!";
break ;
}
else
{
cout<<"做错了!";
}
break ;
default:
break;
}
cout<<endl;
}
}