急求:C语言小学生算法练习软件
#include
#include
#include
#include
intsumScore=0;
intcorrN=0;//全局变量
//总的分数
//写入文件
voidwriteTo(intcorrN,intscore)
{
FILE*fp;
if((fp=fopen(c:\score.txt,a+))==NULL)//a+为读写打开一个文本文件
{
printf(打开文件错误!\n);
return;
}
fprintf(fp,正确数为:%d,得分为:%d\n,corrN,score);//fprintf函数,将数据储存在fp指向的文件中
fclose(fp);//关闭文件
}
voidaddScore(intx,inty,intz,charop)//获得成绩值,x,y是两个计算的数,z是计算结果,c是运算符号,z是总分
{
ints;
printf(%d%c%d=,x,op,y);
scanf(%d,&s);
if(s==z)
{
sumScore=sumScore+10;
corrN++;
}
else
{
printf(你的答案错误,请重新输入答案!\n);
printf(%d%c%d=,x,op,y);
scanf(%d,&s);
if(s==z)
{
sumScore=sumScore+7;
corrN++;
}
else
{
printf(你的答案错误,请重新输入答案!\n);
printf(%d%c%d=,x,op,y);
scanf(%d,&s);
if(s==z)
{
sumScore=sumScore+5;
corrN++;
}
else
printf(你的三次答案均是错误答案,正确答案是%d\n,z);
}
}
}
//加法运算
voidjiafayunsuan()
{
intx,y,z,i;
for(i=0;i<10;i++)
{
x=rand()%100+1;
y=rand()%100+1;
z=x+y;
addScore(x,y,z,'+');
}
}
//减法运算
voidjianfayunsuan()
{
intx,y,z,i;
for(i=0;i<10;i++)
{
x=rand()%100+1;
y=rand()%x+1;//保证y恒大于x
z=x-y;
addScore(x,y,z,'-');
}
}
//乘法运算
voidchengfayunsuan()
{
intx,y,z,i;
for(i=0;i<10;i++)
{
x=rand()%10+1;
y=rand()%10+1;
z=x*y;
addScore(x,y,z,'*');
}
}
//除法运算
voidchufayunsuan()
{
intx,y,z,i;
for(i=0;i<10;i++)
{do{
x=rand()%100+1;
y=rand()%100+1;
}
while(x<y||y==0||x%y!=0);//可以整除的数字里的任意一个数
z=x/y;
addScore(x,y,z,'/');
}
}
//菜单
charmenu()
{
chartype;
printf(1.加法运算\n);
printf(2.减法运算\n);
printf(3.乘法运算\n);
printf(4.除法运算\n);
printf(5.退出\n);
srand((int)time(0));
printf(\n请选择你要进行的运算:);
do{
type=getch();//显示在屏幕上
if(type>='1'&&type<='5')
{
printf(%c,type);
getch();
printf(\n);
switch(type)
{
case'1':
jiafayunsuan();
break;
case'2':
jianfayunsuan();
break;
case'3':
chengfayunsuan();
break;
case'4':
chufayunsuan();
break;
}
writeTo(corrN,sumScore);
returntype;
}
}while(1);//while语句的原型是while(表达式)语句,当表达式为非0值时,执行while语句中的嵌套语句。
}
intmain()
{
charop;
do{
op=menu();
if(op!='5')
{
switch(sumScore/10)
{case10:
case9:
printf(\nSMART!\n);
break;
case8:
printf(\nGOOD!\n);
break;
case7:
printf(\nOK!\n);
break;
case6:
printf(\nPASS!\n);
break;
default:
printf(\nTRYAGAIN!\n);
break;
}
getch();
}
}while(op!='5');
return0;//return0是正常退出,return非零是异常退出,这是返回给控制台的,不在你编的程序的控制范围内,是给操作系统识别的,对你的程序无影响
}
免责声明:本站发布的教育资讯(图片、视频和文字)以本站原创、转载和分享为主,文章观点不代表本网站立场。
如果本文侵犯了您的权益,请联系底部站长邮箱进行举报反馈,一经查实,我们将在第一时间处理,感谢您对本站的关注!
新励学网教育平台
海量全面 · 详细解读 · 快捷可靠
累积科普文章数:18,862,126篇