在将错题保存到文件的时候用的是C语言中的文件,我觉得值得学习的是ftell(fp)这个函数如果指向的是fp这个文件指针所指向的文件的末尾,则返回的是当前文件的大小。
下边是相关代码:
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<time.h>
using namespace std;
void question_bank(); //显示题库
int add(); //加法功能运算
int sub(); //减法功能运算
int multiply(); //乘法功能运算
int divide(); //除法功能运算
static int count=0; //记录做对的题数
FILE *fp;
int fn; //记录文件指针的当前位置
int x,y,z;
int subscript; //记录运算符号的下标
int wrong_question() //重新做错题
{
int a,b,answer_1,result_1,n;
char ch,ch1;
fp=fopen("Chen.txt","r");
fseek(fp,0,2 ); //让文件指针指向文件末尾
fn=ftell(fp); //返回文件指向的当前位置
fseek(fp,0,0); //让文件指针指向文件的开头
for(int wrong = 0;; wrong++ )
{
if(ftell(fp)!=fn) //判断是否到达文件的尾部 (当我再次做错时,任然会显示 没有错题,此处需要修改)
fscanf(fp,"%d %c %d",&a,&ch,&b);
else
{
cout<<"\n没有错题了!\n";
fclose(fp);
cout << "是否继续做题【Y/y / N/n】" ;
cin >> ch1 ;
if(ch1=='Y'||ch1=='y')
{
fp=fopen("Chen.txt","w"); //由于错题已经全部做对,此文件功能是将原来的清除 将原来的错题替换掉
fclose(fp);
question_bank();
}
else
{
exit(0);
}
return 0;
}
switch(ch)
{
case '+' : result_1 = a + b ; break ;
case '-' : result_1 = a - b ; break ;
case '*' : result_1 = a * b ; break ;
case '/' : result_1 = a / b ; break ;
}
cout << "第" << wrong+1 <<"题:"<< a << ch << b << '=' ;
while(1)
{
cin >> answer_1 ; ;
if( result_1 == answer_1 )
{
cout << "做对啦!\n" ;
break;
}
else
{
cout << "做错了!\n" ;
cout << "请重新输入答案:" ;
}
}
}
}
int main()
{
int num;
srand(time(0));
cout<< "\n\n\t\t\t" << "欢迎进入算术运算练习题系统" << endl << endl ;
cout<< "\t\t" << "请输入功能序号 1、题库\t2、错题本" << endl;
cin>>num;
switch(num)
{
case 1 :
question_bank();break;
case 2 :
wrong_question(); break;
}
fclose(fp);
return 0;
}
void question_bank() //题库
{
cout<< "\n\t\t\t" << "小试牛刀,10道数学计算题." << endl ;
int i,answer,result;
char ch[4]={'+','-','*','/'};
char c,c1;
for( i = 0 ; i < 10 ; i++ )
{
subscript = rand()%3+1; //记录运算符号的下标
switch( ch[subscript] )
{
case '+' : result = add(); c='+'; break;
case '-' : result = sub(); c='-'; break;
case '*' : result = multiply(); c='*'; break;
case '/' : result = divide(); c='/'; break;
}
cin>>answer; //输入你的答案
if( answer == result ) //看是否与正确答案一致
{
cout<<"第"<<i+1<<"题结果正确"<<endl;
count++;
}
else
{
cout<< "第" << i+1 << "题结果错误,记入错题本!" << endl ;
fp=fopen("Chen.txt","a"); //追加文件内容
fprintf(fp,"\n%d %c %d",x,c,y);
fclose(fp);
}
}
cout << "You total do the right " << count << " questions" << endl ;
cout << "是否进入错题本?【Y/y / N/n 】" ;
cin >> c1 ;
if(c1=='Y'||c1=='y')
{
wrong_question();
}
else
{
exit(0);
}
}
int add() //加法
{
y = rand()%100+1;
x = rand()%100+1;
z = x+y;
cout << x << "+" << y << "=" ;
return z;
}
int sub() //减法
{
while(1)
{
x = rand()%100+1;
y = rand()%100+1;
if(x>y)
{
z=x-y;
break;
}
}
cout << x << "-" << y << "=";
return z;
}
int multiply() //乘法
{
x = rand()%10+1;
y = rand()%10+1;
cout << x << "*" << y << "=";
z = x*y;
return z;
}
int divide() //除法
{
x = rand()%10+1;
y = rand()%10+1;
while(1)
{
if(x>y)
{
while(1)
{
if(x%y==0)
{
z=x/y;
break;
}
else
{
x = rand()%10+1;
y = rand()%10+1;
}
}
break;
}
else
{
x = rand()%10+1;
y = rand()%10+1;
}
}
cout << x << "/" << y << "=";
return z;
}
感觉写的已经差不多了,若有不完美的地方,希望大家帮忙更改