四则运算
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int a,n,nright=0,nwrong=0,flag5,flag3=1,flag6=0,mark,etime=0,etime1,etime2;
char op,choice;
void award()
{
char c;
if(mark==100)
{
c=rand()%2+1;
printf("%28c %21c\n",c,c);
c=rand()%2+1;
printf("%26c %3c %17c %3c\n",c,c,c,c);
c=rand()%2+1;
printf("%24c %7c %13c %7c\n",c,c,c,c);
c=rand()%2+1;
printf("\n\n");
printf("\n\n");
printf("%35c %7c\n",c,c);
c=rand()%2+1;
printf("%37c %3c\n",c,c);
c=rand()%2+1;
printf("%39c\n",c);
}
else if(mark>=90)
{
c=rand()%2+1;
printf("%28c %21c\n",c,c);
c=rand()%2+1;
printf("%26c %3c %17c %3c\n",c,c,c,c);
c=rand()%2+1;
printf("%24c %7c %13c %7c\n",c,c,c,c);
c=rand()%2+1;
printf("\n\n");
printf("\n\n");
printf("%35c %c %c %c %c\n",c,c,c,c,c);
}
else if(mark>=80)
{
c=rand()%2+1;
printf("\n");
printf("%28c %c %c %c %c %12c %c %c %c %c\n",c,c,c,c,c,c,c,c,c,c);
printf("\n");
printf("\n");
printf("\n");
c=rand()%2+1;
printf("%38c %7c\n",c,c);
c=rand()%2+1;
printf("%40c %3c\n",c,c);
c=rand()%2+1;
printf("%42c\n",c);
}
else if(mark>=60)
{
c=22;
printf("\n");
printf("%28c %c %c %c %c %12c %c %c %c %c\n",c,c,c,c,c,c,c,c,c,c);
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("%38c %c %c %c %c\n",c,c,c,c,c);
}
else
{
c=22;
printf("\n");
printf("%28c %c %c %c %c %12c %c %c %c %c\n",c,c,c,c,c,c,c,c,c,c);
c=25;
printf("%32c %20c\n",c,c);
printf("%32c %20c\n",c,c);
printf("%32c %20c\n",c,c);
printf("%32c %20c\n",c,c);
}
}
void score(int seconds,int i)
{
int seconds0;
seconds0=i*n;
if(etime==0)
printf("\n");
else
{
if(seconds<=seconds0)
{
mark=100*nright/(nright+nwrong);
}
else if(seconds<=(seconds0*1.2))
{
mark=95*nright/(nright+nwrong);
}
else if(seconds<=(seconds0*1.5))
{
mark=90*nright/(nright+nwrong);
}
else if(seconds<=(seconds0*2.0))
{
mark=85*nright/(nright+nwrong);
}
else
{
mark=85*nright/(nright+nwrong);
}
printf("总分数%d\n",mark);
}
}
int judge(int n1)
{
if(n1==a)
{
printf("正确\n");
nright++;
}
else
{
printf("错误\n");
nwrong++;
}
return nwrong;
}
int hunhe()
{
int flag2=1,flag1=0,inumber;
int b=0,e,input;
int i2;
char op2;
a=flag5==0?rand()%10+1:rand()%100+1;
etime=flag5==0?7:9;
op2=rand()%6+42;
while((op2!='*')&&(op2!='+')&&(op2!='-')&&(op2!='/')) //*随机出加减乘除符号*//
op2=rand()%6+42;
inumber=rand()%4+2; //*随机输出混合式长度*//
printf("%d%c",a,op2);
etime1=time(0);
for(i2=1;i2<=inumber;i2++)
{
if(op2=='+'||op2=='-')
{
if(op2=='-')
flag2=0;
if(op2=='+')
flag2=1;
b=flag5==0?rand()%10+1:rand()%100+1;
switch(op2)
{
case '+':
a+=b;
break;
case '-':
while(b>a) //*控制不输出负数*//
{
b=rand()%100+0;
}
a-=b;
break;
}
printf("%d",b);
op2=rand()%6+42;
while((op2!='*')&&(op2!='+')&&(op2!='-')&&(op2!='/'))
op2=rand()%6+42;
printf("%c",op2);
flag1++;
}
if(op2=='*'||op2=='/')
{
if(flag1!=0)
{
if(flag2==1)
a-=b;
if(flag2==0)
a+=b;
}
if(flag1==0)
{
b=a;
}
e=flag5==0?rand()%10+1:rand()%100+1;
switch(op2)
{
case '*':
if(flag2==0)
{
while((b*e)>a) //*控制不输出负数*//
{
e=flag5==0?rand()%10+1:rand()%100+1;
}
b*=e;
}
if(flag2==1)
{
b*=e;
}
break;
case '/':
if(flag2==0)
{
while((b/e)>a||((b%e)!=0)) //*控制不输出负数及小数*//
{
e=flag5==0?rand()%10+1:rand()%100+1;
}
b/=e;
}
if(flag2==1)
{
while(((b%e)!=0))
{
e=flag5==0?rand()%10+1:rand()%100+1;
}
b/=e;
}
break;
}
printf("%d",e);
op2=rand()%6+42;
while((op2!='+')&&(op2!='-'))
op2=rand()%6+42;
printf("%c",op2);
if(flag1!=0) //*根据前一个运算符的加或减来进行后期运算*//
{
if(flag2==1)
a+=b;
if(flag2==0)
a-=b;
}
if(flag1==0)
a=b;
}
}
printf("\b=");
scanf("%d",&input);
judge(input);
etime2=time(0);
}
int jiajian()
{
int flag4,b,input,d;
flag4=rand()%2+1;
etime=flag5==0?2:4; //*输出每道题所需时间*//
switch(flag4)
{
case 1:
d=flag5==0?rand()%10+1:rand()%100+1; //*随机输出混合式长度*//
b=flag5==0?rand()%10+1:rand()%100+1;
a=d+b;
printf("%d+%d=",d,b);
scanf("%d",&input);
break;
case 2:
do
{
d=flag5==0?rand()%10+1:rand()%100+1; //*随机输出混合式长度*//
b=flag5==0?rand()%10+1:rand()%100+1;
}while(d-b<0);
a=d-b;
printf("%d-%d=",d,b);
scanf("%d",&input);
break;
}
judge(input);
}
int chengchu()
{
int flag4,input,b,d;
etime=flag5==0?2:4;
flag4=rand()%2+1;
switch(flag4)
{
case 1:
d=flag5==0?rand()%10+1:rand()%100+1;
b=flag5==0?rand()%10+1:rand()%100+1;
a=d*b;
printf("%d*%d=",d,b);
scanf("%d",&input);
break;
case 2:
do
{
d=flag5==0?rand()%10+1:rand()%100+1;
b=flag5==0?rand()%10+1:rand()%100+1;
}while(d%b!=0);
a=d/b;
printf("%d/%d=",d,b);
scanf("%d",&input);
break;
}
judge(input);
}
int lianjiajian()
{
int b,i,inumber,input;
char op2;
etime=5;
srand(time(0));
a=rand()%10+1;
op2=rand()%6+42;
while((op2!='+')&&(op2!='-'))
op2=rand()%6+42;
inumber=rand()%4+2;
printf("%d%c",a,op2);
for(i=1;i<=inumber;i++)
{
b=rand()%10+0;
switch(op2)
{
case '+':
a+=b;
break;
case '-':
while(a<b)
b=rand()%10+0;
a-=b;
break;
}
printf("%d",b);
op2=rand()%6+42;
while((op2!='+')&&(op2!='-'))
op2=rand()%6+42;
printf("%c",op2);
}
printf("\b=");
scanf("%d",&input);
judge(input);
}
int lianchengchu()
{
int b,i,inumber,input;
char op2;
etime=5;
srand(time(0));
a=rand()%10+0;
op2=rand()%6+42;
while((op2!='*')&&(op2!='/'))
op2=rand()%6+42;
inumber=rand()%4+2;
printf("%d%c",a,op2);
for(i=1;i<=inumber;i++)
{
b=rand()%10+1;
switch(op2)
{
case '*':
a*=b;
break;
case '/':
while(a%b!=0)
b=rand()%10+1;
a/=b;
break;
}
printf("%d",b);
op2=rand()%6+42;
while((op2!='*')&&(op2!='/'))
op2=rand()%6+42;
printf("%c",op2);
}
printf("\b=");
scanf("%d",&input);
judge(input);
}
int main()
{
int i1,i2,i3,n,second1=0,second2=0,second3=0;
int flag2=1,flag1=0;
char mode,esc;
char f[3][51]={"##################################################",
"#提示:若要退出按除数字回车外的任意键进入选择菜单#",
"##################################################"};
char a[5][36]={"###################################",
"# Math Game #",
"# #",
"#03 低吟w夏伤 #",
"###################################"};
for(i2=0;i2<5;i2++)
{
printf("%57s\n",a[i2]);
}
srand(time(0));
do
{
printf(" 请选择难度\n");
printf(" 1、新手\n");
printf(" 2、入门\n");
printf(" 3、高手\n");
printf(" 4、大师\n");
for(i3=0;i3<3;i3++)
{
printf("%64s\n",f[i3]);
}
printf(" \n");
scanf("%c",&mode);
second1=time(0);
if(mode<49||mode>52) //*任意键退出*//
{
printf("A、继续\nB、退出游戏\n");
op=getchar();
op=getchar();
if(op=='a'||op=='A')
{
op='a';
flag6=1;
}
else if(op=='b'||op=='B')
return 0;
else
{
op='a';
printf("请重新选择\n");
flag6=1;
}
}
do
{
if(flag6==0)
op='m';
switch(mode)
{
case '1':
choice=getchar();
printf("经典模式\n请选择游戏难度\n");
printf("1、十以内加减\n2、十以内乘除\n3、一百以内加减\n4、一百以内乘除\n5、十以内连加连减\n6、十以内连乘连除\n7、十以内混合运算\n8、一百以内混合运算\n");
choice=getchar();
if(choice<49||choice>56) //*任意键退出*//
{
printf("A、回到主菜单\nB、退出游戏\nC、继续\n");
op=getchar();
op=getchar();
if(op=='b'||op=='B')
return 0;
else if(op=='a'||op=='A')
{
flag6=1;
break;
}
else if(op=='c'||op=='C')
{
op='c';
flag3=0;
}
else
{
op='c';
printf("请重新选择\n");
flag3=0;
}
}
if(flag3!=0)
{
printf("请输入答题数\n");
scanf("%d",&n);
esc=getchar();
}
second1=time(0);
for(i1=1;i1<=n;i1++) //*控制答题数进行循环*//
{
switch(choice)
{
case '1':
flag5=0;
jiajian();
break;
case '2':
flag5=0;
chengchu();
break;
case '3':
flag5=1;
jiajian();
break;
case '4':
flag5=1;
chengchu();
break;
case '5':
lianjiajian();
break;
case '6':
lianchengchu();
break;
case '7':
flag5=0;
hunhe();
break;
case '8':
flag5=1;
hunhe();
break;
}
flag2=1;
flag1=0;
}
if(flag3!=0)
choice=getchar();
break;
case '2':
second1=time(0);
choice=getchar();
printf("升级模式\n您必须答对所有题才能进入下一关\n按回车开始游戏\n");
choice=getchar();
if((choice<49||choice>56)&&choice!='\n')
{
printf("A、回到主菜单\nB、退出游戏\nC、继续\n");
op=getchar();
op=getchar();
if(op=='b'||op=='B')
return 0;
else if(op=='a'||op=='A')
{
flag6=1;
break;
}
else if(op=='c'||op=='C')
{
op='c';
flag3=0;
}
else
{
op='c';
printf("请重新选择\n");
flag3=0;
}
}
printf("第一关\n");
for(i2=1;i2<=5;i2++)
{
flag5=rand()%1+0;
jiajian();
}
if(nwrong==0)
{
printf("第二关\n");
for(i2=1;i2<=8;i2++)
{
flag5=rand()%1+0;
chengchu();
}
}
if(nwrong==0)
{
printf("第三关\n");
for(i2=1;i2<=10;i2++)
{
lianjiajian();
}
}
if(nwrong==0)
{
printf("第四关\n");
for(i2=1;i2<=8;i2++)
lianchengchu();
}
if(nwrong==0)
{
printf("第五关\n");
for(i2=1;i2<=8;i2++)
{
flag5=rand()%1+0;
hunhe();
}
}
else
printf("闯关失败\n");
break;
case '3':
second1=time(0);
choice=getchar();
printf("无限模式\n你只有三次犯错的机会\n按回车开始游戏\n");
choice=getchar();
if((choice<49||choice>56)&&choice!='\n')
{
printf("A、回到主菜单\nB、退出游戏\nC、继续\n");
op=getchar();
op=getchar();
if(op=='b'||op=='B')
return 0;
else if(op=='a'||op=='A')
{
flag6=1;
break;
}
else if(op=='c'||op=='C')
{
op='c';
flag3=0;
}
else
{
op='c';
printf("请重新选择\n");
flag3=0;
}
}
while(nwrong<=2) //*逐渐增加难度*//
{
flag5=rand()%1+0;
if(nright<=3)
jiajian();
if(nright>3&&nright<=6)
chengchu();
if(nright>6&&nright<=10)
lianjiajian();
if(nright>10&&nright<=14)
lianchengchu();
if(nright>14)
hunhe();
}
break;
case '4':
srand(time(0));
choice=getchar();
printf("时间模式\n你有60秒左右的时间来进行游戏\n按回车开始游戏\n");
choice=getchar();
if((choice<49||choice>56)&&choice!='\n')
{
printf("A、回到主菜单\nB、退出游戏\nC、继续\n");
op=getchar();
op=getchar();
if(op=='b'||op=='B')
return 0;
else if(op=='a'||op=='A')
{
flag6=1;
break;
}
else if(op=='c'||op=='C')
{
op='c';
flag3=0;
}
else
{
op='c';
printf("请重新选择\n");
flag3=0;
}
}
second1=time(0);
do
{
flag5=rand()%1+0;
hunhe();
second3+=(etime2-etime1);
}while(second3<60);
break;
}
flag3++;
second2=time(0);
}while(op=='c');
if(flag6==0)
{
printf("共答对%d\n",nright);
printf("用时%d\n",second2-second1);
score(second2-second1,etime);
award();
nright=0;
nwrong=0;
printf("按回车继续\n"); //*控制循环继续*//
op=getchar();
printf("你想继续吗? Y|N\n");
op=getchar();
if(op=='y'||op=='Y')
op='a';
if(op=='n'||op=='N')
return 0;
}
mode=getchar();
flag6=0;
}while(op=='a'||op=='A');
return 0;
}