#include<stdio.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<Windows.h>
int i;
int a=0,b=0,c,d; //a为孩子做对题数,b为错题数,c,d为随机数
void cont(); //继续游戏的选择
void process(); //不同难度
void ab(); //读取ab文件
void read();//保存a,b值
void parent();//家长模式
void menu() //程序的开始界面
{
int a;
system("cls");
printf("\n\n\n\n\n\n");
printf("\t\t\t\t\t\b\b\b\b\b欢迎使用小竞学生帮!\n\n");
printf("\t\t\t\t\t\b\b\b\b\b请选择:\n\n");
printf("\t\t\t\t\t1.简单\n");
printf("\t\t\t\t\t2.中等\n");
printf("\t\t\t\t\t3.困难\n");
printf("\t\t\t\t\t4.家长模式\n");
printf("\t\t\t\t请选择(1-4):");
scanf("%d",&a);
switch(a)
{
case 1:i=10;break;
case 2:i=100;break;
case 3:i=1000;break;
case 4:parent();
default :menu();
}
process();
}
void random() //产生随机数
{
srand((unsigned)time(NULL));//播种
c=rand()%i+1,d=rand()%i+1;
}
void add() //加
{
system("cls"); //清屏
int t;
random();
printf("%d+%d = ",c,d);
scanf("%d",&t);
if(t==(c+d))
{
printf("回答正确!");
a++;
}
else
printf("回答错误!!");
b++;
cont();
}
void minus() //减法
{
system("cls"); //清屏
int t;
random();
printf("%d-%d = ",c,d);
scanf("%d",&t);
if(t==(c-d))
{
printf("回答正确!");
a++;
}
else
{
printf("回答错误!");
b++;
}
cont();
}
void multiply() //乘法
{
system("cls"); //清屏
int t ;
random();
printf("%dx%d = ",c,d);
scanf("%d",&t);
if(t==(c*d))
{
printf("回答正确!");
a++;
}
else
{
printf("回答错误!");
b++;
}
cont();
}
void divide() //除法
{
system("cls"); //清屏
int t;
random();
printf("(省略小数点后面的数据)%d/%d = ",c,d);
scanf("%d",&t);
if(t==(c/d))
{
printf("回答正确!");
a++;
}
else
{
printf("回答错误!");
b++;
}
cont();
}
void process()
{
int n,t;
srand((unsigned)time(NULL)); //播种
n = rand()%3+1;
switch(n)
{
case 1 : add(); break;
case 2 : minus(); break;
case 3 : multiply(); break;
case 4 : divide(); break;
}
}
void cont () //继续游戏的选择
{
int t;
read();
printf("是否继续(1继续/0停止):");
scanf("%d",&t);
if(t==1)
{
process();
}
else
system("cls"); //清屏
menu();
}
void ab()
{
FILE *fp;
if(fopen("c:\\题.txt","rb")==NULL)
fp=fopen("c:\\题.txt","wb");
else
{
fp=fopen("c:\\题.txt","rb");
fscanf(fp,"%d%d",&a,&b);
}
fclose(fp);
}
void read()
{
FILE *fp;
fp=fopen("c:\\题.txt","rb");
fprintf(fp,"%d%d",a,b);
fclose(fp);
}
void parent()
{
int ch;
printf("答题:%d\n",a+b);
printf("答对题数:%d\n",a);
printf("答错题数:%d\n",b);
printf("正确率%2f\n",a/(a+b));
printf("请按任何键继续\n");
scanf("%d",&ch);
}
int main()
{
int n;
ab();
menu();
}
写入c盘权限不够,修改见注释处,供参考:
#include<stdio.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<Windows.h>
int i;
int a = 0, b = 0, c, d; //a为孩子做对题数,b为错题数,c,d为随机数
void cont(); //继续游戏的选择
void process(); //不同难度
void ab(); //读取ab文件
void read();//保存a,b值
void parent();//家长模式
void menu() //程序的开始界面
{
int a;
system("cls");
printf("\n\n\n\n\n\n");
printf("\t\t\t\t\t\b\b\b\b\b欢迎使用小竞学生帮!\n\n");
printf("\t\t\t\t\t\b\b\b\b\b请选择:\n\n");
printf("\t\t\t\t\t1.简单\n");
printf("\t\t\t\t\t2.中等\n");
printf("\t\t\t\t\t3.困难\n");
printf("\t\t\t\t\t4.家长模式\n");
printf("\t\t\t\t请选择(1-4):");
scanf("%d", &a);
switch (a)
{
case 1:i = 10; break;
case 2:i = 100; break;
case 3:i = 1000; break;
case 4:parent();
default:menu();
}
process();
}
void random() //产生随机数
{
srand((unsigned)time(NULL));//播种
c = rand() % i + 1, d = rand() % i + 1;
}
void add() //加
{
system("cls"); //清屏
int t;
random();
printf("%d+%d = ", c, d);
scanf("%d", &t);
if (t == (c + d))
{
printf("回答正确!");
a++;
}
else
printf("回答错误!!");
b++;
cont();
}
void minus() //减法
{
system("cls"); //清屏
int t;
random();
printf("%d-%d = ", c, d);
scanf("%d", &t);
if (t == (c - d))
{
printf("回答正确!");
a++;
}
else
{
printf("回答错误!");
b++;
}
cont();
}
void multiply() //乘法
{
system("cls"); //清屏
int t;
random();
printf("%dx%d = ", c, d);
scanf("%d", &t);
if (t == (c * d))
{
printf("回答正确!");
a++;
}
else
{
printf("回答错误!");
b++;
}
cont();
}
void divide() //除法
{
system("cls"); //清屏
int t;
random();
printf("(省略小数点后面的数据)%d/%d = ", c, d);
scanf("%d", &t);
if (t == (c / d))
{
printf("回答正确!");
a++;
}
else
{
printf("回答错误!");
b++;
}
cont();
}
void process()
{
int n, t;
srand((unsigned)time(NULL)); //播种
n = rand() % 3 + 1;
switch (n)
{
case 1: add(); break;
case 2: minus(); break;
case 3: multiply(); break;
case 4: divide(); break;
}
}
void cont() //继续游戏的选择
{
int t;
read();
printf("是否继续(1继续/0停止):");
scanf("%d", &t);
if (t == 1)
{
process();
}
else
system("cls"); //清屏
menu();
}
void ab()
{
FILE* fp;
if ((fp = fopen("d:\\题.txt", "rb")) == NULL) //if ((fp = fopen("c:\\题.txt", "rb")) == NULL)
{
printf("Open file fail!\n");
return;
//fp = fopen("c:\\题.txt", "wb");
}
else
{
//fp = fopen("c:\\题.txt", "rb");
fscanf(fp, "%d %d", &a, &b);
fclose(fp);
}
}
void read()
{
FILE* fp;
fp = fopen("d:\\题.txt", "wb"); //fp = fopen("c:\\题.txt", "wb");
fprintf(fp, "%d %d", a, b);
fclose(fp);
}
void parent()
{
int ch;
printf("答题:%d\n", a + b);
printf("答对题数:%d\n", a);
printf("答错题数:%d\n", b);
printf("正确率%2f\n", a / (a + b));
printf("请按任何键继续\n");
scanf("%d", &ch);
}
int main()
{
int n;
ab();
menu();
return 0;
}