实验1

#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#define C 4

char getSignal();
int answer(int,int,char);

void falsePrint(int);
void rightPrint(int);

//main函数
void main()
{
while(1)
{
int getResult=-1;
int num1;
int num2;
char signal;
int p;

srand((unsigned)time(NULL));//清空
signal=getSignal();
num1=rand()%10;
num2=rand()%10;//获取随机数
printf("num1=%d,num2=%d\n",num1,num2);

if(signal=='/')
{
if(num2==0)
{
int temp;
temp=num1;
num1=num2;
num2=temp;//被除数不为0
}
}

printf("%d%c%d=",num1,signal,num2);
scanf("%d",&getResult);
fflush(stdin);
if(answer(num1,num2,signal)==getResult)
{
p=rand()%6;
rightPrint(p);
}
else
{
p=rand()%5;
falsePrint(p);
printf("正确答案:%d\n",answer(num1,num2,signal));
}
}

}

//获取随机运算符
char getSignal()
{
char signal[C]={'+','-','*','/'};
// srand((unsigned)time(NULL));
return signal[rand()%4];
}

//运算
int answer(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;
printf("运算符不符合规则\n");
}
return res;
}

//回答正确打印信息
void rightPrint(int num)
{
switch(num)
{
case 1:
printf("Powerful!\n");break;
case 2:
printf("It's OK!\n");break;
case 3:
printf("Right!\n");break;
case 4:
printf("Perfect!\n");break;
case 5:
printf("Good!\n");break;
default:
printf("o(≧v≦)o~~好棒!\n");break;
}
}

//回答错误打印信息
void falsePrint(int num)
{
switch(num)
{
case 1:
printf("You're lost!");break;
case 2:
printf("Failure!");break;
case 3:
printf("Come on!");break;
case 4:
printf("It's wrong!");break;
default:
printf("╮(╯▽╰)╭哎!");break;
}
}

你可能感兴趣的:(实验1)