四则运算

#include<stdio.h>
#include<malloc.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

char fuhao()
{
int xuanzhe;
time_t t;
srand((unsigned)time(&t));
xuanzhe=rand()%4+1;
switch(xuanzhe)
{
case 1:return'+';
case 2:return'-';
case 3:return'*';
case 4:return'/';
}
}
void main()
{
float shuru3,a8=0.2,a7=0.8,a6=1.4;
int a1=49,a10=3,a9=2,a5=21,a4=3,a3=2,a2=4;
time_t t;
int geshu,fanwei,shuliang,shu1,shu2,defen,shuru,xuanzhe,shu3,shuliang1,xuanzhe2,fujiati;
float b;
char c,c1;
printf("请输入四则数目的个数");
printf("1:2个数");
printf("2:3个数");
printf("数值个数:");
scanf("%d",&geshu);
while(geshu!=1&&geshu!=2)
{
printf("输入错误,请从新输入");
printf("1:2个数");
printf("2:3个数");
}
printf("请输入四则运算最大的数目(10-100):");
scanf("%d",&fanwei);
printf("请输入题目的数量:");
scanf("%d",&shuliang);
if(geshu==1)
{

for(int i=1;i<=shuliang;i++)
{
c=fuhao();
srand((unsigned)time(&t));
shu1=rand()%fanwei+1;
shu2=rand()%fanwei+1;
if(c=='+')
{defen=shu1+shu2;
}
else if(c=='-')
{defen=shu1-shu2;
}
else if(c=='*')
{defen=shu1*shu2;
}
else if(c=='/')
{defen=shu1/shu2;
}
printf("第%d题:%d%c%d=",i,shu1,c,shu2);
scanf("%d",&shuru);
if(shuru==defen)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
}
}

if(geshu==2)
{
for(int ii=1;ii<=shuliang;ii++)
{
srand((unsigned)time(&t));
shu1=rand()%fanwei+1;
shu2=rand()%fanwei+1;
shu3=rand()%fanwei+1;
c1=fuhao();
c=fuhao();
if(c1=='*'&&c=='-')
{
shuliang1=shu1*shu2-shu3;
}
else if(c1=='*'&&c=='+')
{
shuliang1=shu1*shu2+shu3;
}
else if(c1=='/'&&c=='-')
{
shuliang1=shu1/shu2-shu3;
}
else if(c1=='/'&&c=='+')
{
shuliang1=shu1/shu2+shu3;
}
else if(c1=='/'&&c=='/')
{
shuliang1=shu1/shu2/shu3;
}
else if(c1=='/'&&c=='*')
{
shuliang1=shu1/shu2*shu3;
}
else if(c1=='*'&&c=='+')
{
shuliang1=shu1*shu2+shu3;
}
else if(c1=='*'&&c=='*')
{
shuliang1=shu1*shu2*shu3;
}
else if(c1=='-'&&c=='+')
{
shuliang1=shu1-shu2+shu3;
}
else if(c1=='-'&&c=='-')
{
shuliang1=shu1-shu2-shu3;
}
else if(c1=='-'&&c=='*')
{
shuliang1=shu1-shu2*shu3;
}
else if(c1=='-'&&c=='/')
{
shuliang1=shu1-shu2/shu3;
}
else if(c1=='+'&&c=='-')
{
shuliang1=shu1+shu2-shu3;
}
else if(c1=='+'&&c=='+')
{
shuliang1=shu1+shu2+shu3;
}
else if(c1=='+'&&c=='*')
{
shuliang1=shu1+shu2*shu3;
}
else if(c1=='+'&&c=='/')
{
shuliang1=shu1+shu2/shu3;
}
printf("第%d题:%d%c%d%c%d=",ii,shu1,c1,shu2,c,shu3);
scanf("%d",&shuru);
printf("%d",shuliang1);
if(shuru==shuliang1)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
}
printf("是否做附加题(1:是。2:否。):");
scanf("%d",&fujiati);
if(fujiati==1)
{
srand((unsigned)time(&t));

xuanzhe2=rand()%10+1;
switch(xuanzhe2)
{
case 1:printf("7*(8-1)=");

scanf("%d",&shuru);
if(shuru==a1)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
break;
case 2:printf("4/(2-1)=");
scanf("%d",&shuru);
if(shuru==a2)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
break;
case 3:printf("8-(4+2)=");
scanf("%d",&shuru);
if(shuru==a3)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
break;
case 4:printf("(4-3)*3=");
scanf("%d",&shuru);
if(shuru==a4)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
break;
case 5:printf("(4+3)*3=");
scanf("%d",&shuru);
if(shuru==a5)
{
printf("正确! ");
}
else
{
printf("错误! ");
}

break;
case 6:printf("1.2/3+1=");
scanf("%f",&shuru3);
if(shuru3==a6)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
break;
case 7:printf("(1.4+1)/3=");
scanf("%f",&shuru3);

if(shuru3==a7)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
break;
case 8:printf("2.4/4/3=");
scanf("%f",&shuru3);
if(shuru3==a8)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
break;
case 9:printf("1%6+1=");
scanf("%d",&shuru);
if(shuru==a9)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
break;
case 10:printf("3+4%2=");
scanf("%d",&shuru);
if(shuru==a10)
{
printf("正确! ");
}
else
{
printf("错误! ");
}
break;
}
}
else
{
printf("程序结束!");
}

printf("程序结束!");

}
}

//运行结果

个人感想:

本程序编写过程经过多方面的考虑,首先在所计算数字的个数可选择为2个或者是3个,控制一下难度。

然后可以控制答题的数目,最后还有附加题,可以选做,人性化设计。

原文地址:https://www.cnblogs.com/shaojiawen/p/4368488.html