设计一个程序,用来实现帮助小学生进行算术运算练习,它具有以下功能:
(1) 提供10道加、减、乘、除四种基本算术运算的题目,每道题中的操作数是随机产生的;
(2) 加减是100以内的数;乘除为乘法表里的;被减数大于减数;除法要求能整除;被乘数小与乘数;(若不合格重新产生)
(3) 练习者根据显示的题目输入自己的答案,程序自动判断输入的答案是否正确并显示出相应的信息。最后显示做对了几道题;
(1) 提供10道加、减、乘、除四种基本算术运算的题目,每道题中的操作数是随机产生的;
(2) 加减是100以内的数;乘除为乘法表里的;被减数大于减数;除法要求能整除;被乘数小与乘数;(若不合格重新产生)
(3) 练习者根据显示的题目输入自己的答案,程序自动判断输入的答案是否正确并显示出相应的信息。最后显示做对了几道题;
(4) 每次答题要记录做错的题目,下次做题可选择上次做错的题;
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<ctime>
using namespace std;
void menu(); //菜单函数
void get_value1(); //加法
void get_value2(); //减法
void get_value3(); //乘法
void get_value4(); //除法
void get_value(); //调用函数
void read(string filename);
static int n=0;
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:
get_value();break;
case 2:
read("1.txt");break;
}
}
}
//调用函数
void get_value()
{
int m,i;
FILE *fp;
if((fp=fopen("1.txt","w"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
fclose(fp);
for(i=0;i<10;i++)
{
cout<<"==================="<<endl;
cout<<"第"<<i+1<<"题:"<<endl;
m=rand()%4;
switch(m)
{
case 0:
get_value1();break;
case 1:
get_value2();break;
case 2:
get_value3();break;
case 3:
get_value4();break;
}
}
cout<<"共做对了"<<n<<"道题"<<endl;
}
//加法函数
void get_value1()
{
FILE * fp;
if((fp=fopen("1.txt","a"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
int a,b,x;
char m='+',ch;
a=1+rand()%100;
b=1+rand()%100;
cout<<a<<m<<b<<'=';
cin>>x;
if(x==a+b)
{
cout<<"做对了"<<endl;
n++;
}
else
{
cout<<"做错了"<<endl;
cout<<"是否输出正确答案:(Y或N)"<<endl;
cin>>ch;
if(ch=='Y')
{
cout<<"正确答案为:"<<a+b<<endl;
}
fprintf(fp,"%d+%d=\n",a,b);
}
}
//减法函数
void get_value2()
{
FILE * fp;
if((fp=fopen("1.txt","a"))==NULL)
{
printf("cannot open file\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;
if(x==a-b)
{
cout<<"做对了"<<endl;
n++;
}
else
{
cout<<"做错了"<<endl;
cout<<"是否输出正确答案:(Y或N)"<<endl;
cin>>ch;
if(ch=='Y')
{
cout<<"正确答案为:"<<a-b<<endl;
}
fprintf(fp,"%d-%d=\n",a,b);
}
}
else
get_value2();
}
//乘法函数
void get_value3()
{
FILE * fp;
if((fp=fopen("1.txt","a"))==NULL)
{
printf("cannot open file\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;
if(x==a*b)
{
cout<<"做对了"<<endl;
n++;
}
else
{
cout<<"做错了"<<endl;
cout<<"是否输出正确答案:(Y或N)"<<endl;
cin>>ch;
if(ch=='Y')
{
cout<<"正确答案为:"<<a*b<<endl;
}
fprintf(fp,"%d*%d=\n",a,b);
}
}
else
get_value3();
}
//除法函数
void get_value4()
{
FILE * fp;
fclose(fp);
if((fp=fopen("1.txt","a"))==NULL)
{
printf("cannot open file\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;
if(x==a/b)
{
cout<<"做对了"<<endl;
n++;
}
else
{
cout<<"做错了"<<endl;
cout<<"是否输出正确答案:(Y或N)"<<endl;
cin>>ch;
if(ch=='Y')
{
cout<<"正确答案为:"<<a/b<<endl;
}
fprintf(fp,"%d/%d=\n",a,b);
}
}
else
get_value4();
}
//数据读出函数
void read(string filename)
{
FILE * fp;
int a,b,s;
char c,d;
cout<<"查看上次错题集:"<<endl;
if((fp=fopen("1.txt","r+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
while(!feof(fp))
{
int s,t;
fscanf(fp,"%d %c %d %c\n",&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;
}
}