一个计算小游戏
/*前两天写了一个这样的程序,有很多不足之处。在几位热心朋友的建议与帮助下,新的程序终于诞生了。
它虽然很小,也很简单,但这也是我辛苦的成果。希望诸位能够喜欢。
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char YN[100]={"您错了。请问您是放弃这道题,还是更改答案?您有一次更改的机会\n"
"按N退出这道题,按其它键更改。\n"};
char dui[100]={"恭喜您,答对了!\n"};
char caidan[3][100]={{"***********************************************************************\n"},
{"您好,欢迎您来玩这个小游戏,它将会验证您的计算能力。 \n"},
{"对不起,您输入的字符非法,请重新输入。\n"}};
char jisuanfu[30]={'+','-','*','/','/','/','*','+','+','-','/','*','/','+','-','-','+','*','*','/','/','+','+','-','-','-','*','*','*','*'};
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~菜单*/
char caidan1()
{
char x;
char y[]={"请您用大写字符输入您的选择:\n"};
char caidan_1[100]={"\t\n\tA:初级\n\tB:中级\n\tC:高级\n\tD:游戏说明\n\tE:退出\n\n\n"};
system("cls");
printf("%s\t%s\n\n",caidan[0],caidan[1]);
printf("%s\n",caidan_1);
printf("\n%s\n\n\n\t%s ",caidan[0],y);
do
{
scanf(" %c",&x);
if(x<65||x>69)
printf("\n\t%s\n\t%s",caidan[2],caidan[0]);
}
while(x<65||x>69);
if(x=='E')
exit(0);
if(x=='D')
shuoming();
if(x>=65&&x<=67)
{
printf("\n祝您好运! \n\n");
sleep(2);
return x;
}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~说明*/
shuoming()
{
char x;
system("cls");
printf("\n%s\n\n\n",caidan[0]);
printf("\t您好,欢迎您来玩这个加、减、乘、除的数学小测试。它分为三个等级:\n"
"\t初级: 二位数以下的运算,无小数,无负数,可更改一次答案,\n"
"\t 每题7分,有无错奖20分;\n "
"\t中级: 二位数以下的运算,结果精确到2位小数,有负数,每题8分,\n"
"\t 无错奖20分;\n"
"\t高级: 三位数以下的运算,结果精确到3位小数,有负数,每题20分\n");
printf("%s\n\n",caidan[0]);
printf("请问您是要回到主菜单还是要退出?退出按N,其它键回到主菜单:\n\n");
scanf(" %c",&x);
if(x=='N'||x=='n')
exit(0);
else caidan1();
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~运算*/
float yunsuan( float a, float b,char jisuanfu)
{
float c;
switch(jisuanfu)
{
case '+': c=a+b; break;
case '-': c=a-b; break;
case '*': c=a*b; break;
case '/': c=a/b;
}
return c;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~初级*/
int chuji()
{
int a,b,i,zh,x,X,fen=0,jieguo,j=1,k,m=0;
system("cls");
printf("\t%s\n",caidan[0]);
srand(time(NULL));
i=rand()%20+0;
for(;j<=10;i++,j++)
{
k=0;
srand(time(NULL));
a=rand()%99+5;
srand(time(NULL)+1);
b=rand()%99+5;
if(a<b)
{
zh=a;
a=b;
b=zh;
}
while(a%b!=0)
{
a++;
}
printf("\n第%d题:%d %c %d = ",j,a,jisuanfu[i],b);
x=yunsuan(a,b,jisuanfu[i]); /*~~~~~~~~此处不加int是否能转换过来?*/
while(k==0)
{
scanf(" %d",&jieguo);
puts(" ");
if(x==jieguo)
printf("%s\n您的分数为%d。\n%s \n\n\n",dui,fen+=7,caidan[0]);
else
{
m++;
printf("%s\n%s\n\n",YN,caidan[0]);
scanf(" %c",&X);
if(X=='N'||X=='n')
break;
else
{
printf("%d %c %d = ",a,jisuanfu[i],b);
scanf(" %d",&jieguo);
if(x==jieguo)
{
printf("\n%s\n您的分数为:%d\n%s\n\n",dui,fen+=7,caidan[0]);
break;
}
}
printf("非常遗憾,您错了!不加分。\n");
}
k++;
}
}
if(m==0)
{
printf("您无一题出过错,奖励10分。\n");
fen+=20;
}
printf("任意键继续:");
getch();
return fen;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~中级*/
int zhongji()
{
int i,X,fen=0,j=1,k=0;
float a,b,x,zh,jieguo;
system("cls");
printf("%s\n",caidan[0]);
srand(time(NULL));
i=rand()%20+0;
for(;j<=10;i++,j++)
{
srand(time(NULL));
a=rand()%99+1;
srand(time(NULL)+1);
b=rand()%99+1;
printf("\n第%d题:%f %c %f = ",j,a,jisuanfu[i],b);
scanf(" %f",&jieguo);
puts(" ");
x=yunsuan(a,b,jisuanfu[i]);
if(x<jieguo)
{
zh=x;
x=jieguo;
jieguo=zh;
}
if(x-jieguo<=0.009)
printf("%s\n您的分数为:%d\n\n%s\n\n",dui,fen+=8,caidan[0]);
else
{
k++;
printf("\n非常遗憾,您错了!\n%s\n\n",caidan[0]);
}
}
if(k==0)
{
printf("您无一题出错,奖20分。\n\n");
fen+=20;
}
printf("请按任意键:\n");
return fen;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~高级*/
int gaoji()
{
int i,fen=0,j=1;
float jieguo,zh,a,b,x;
system("cls");
printf("%s\n",caidan[0]);
srand(time(NULL));
i=rand()%20+0;
for(;j<=10;i++,j++)
{
srand(time(NULL));
a=rand()%999+1;
srand(time(NULL)+1);
b=rand()%999+1;
a=a/0.03;
b=b/0.03;
printf("\n第%d题:%f %c %f = ",j,a,jisuanfu[i],b);
scanf(" %f",&jieguo);
puts(" ");
x=yunsuan(a,b,jisuanfu[i]);
if(x<jieguo)
{
zh=x;
x=jieguo;
jieguo=zh;
}
if(x-jieguo<=0.0009)
{
printf("%s\n%s\n\n",dui,caidan[0]);
fen+=10;
}
else
{
printf("\n非常遗憾,您错了!\n");
printf("\n%s\n\n",caidan[0]);
}
}
return fen;
}
/*########################################################主函数*/
int main(void)
{
int fen;
char x;
while(1)
{
x=caidan1();
switch(x)
{
case 'A': fen=chuji();
break;
case 'B': fen=zhongji();
break;
case 'C': fen=gaoji();
}
printf("在您这轮测试中,您得了%d分。\n",fen);
printf("请按任意键:\n");
getch();
}
}