#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#define IDNO_LENGTH 6
#define CALC_KIND 4
char getSignal();
int getResult(int,int,char);
void takeTest();
void falsePrint(int);
void correctPrint(int);
int main()
{
while(1)
{
takeTest();
}
}
char getSignal() //随机运算符
{
char signal[CALC_KIND]={'+','-','*','/'};
srand((unsigned)time(NULL));
return signal[rand()%4];
}
int random(double start, double end) //获取随机数
{
return (int)(start+(end-start)*rand()/(RAND_MAX+ 1.0));
}
int getResult(int num1,int num2,char signal) //进行题目运算
{
int res;
switch(signal)
{
case '+':
res=num1+num2;break;
case '-':
res=num1-num2;break;
case '*':
res=num1*num2;break;
case '/':
res=num1/num2;break;
default:res=0;break;
}
return res;
}
void takeTest()
{
int giveRes,num1,num2,printFlag;
char signal;
srand((unsigned)time(NULL));
signal=getSignal();
num1=random(0,100);
num2=random(1,100);
if(signal=='-') //确保减法出现负数
{
if(num1<num2)
{
int temp;
temp=num1;
num1=num2;
num2=temp;
}
}
if(signal=='/') //防止除法运算出现除数为零
{
if(num2==0)
{
int temp;
temp=num1;
num1=num2;
num2=temp;
}
}
printf("%d%c%d=",num1,signal,num2);
scanf("%d",&giveRes);
fflush(stdin);
if(getResult(num1,num2,signal)==giveRes)
{
printFlag=random(1,1);
correctPrint(printFlag);
}
else
{
printFlag=random(1,1);
falsePrint(printFlag);
printf("正确答案:%d
",getResult(num1,num2,signal));
}
}
void correctPrint(int num) //回答正确提示信息
{
switch(num)
{
case 1:printf("正确!
");break;
default:;break;
}
}
void falsePrint(int num) //回答错误提示信息
{
switch(num)
{
case 1:printf("错误! ");break;
default:break;
}
}