/*引用函数库*/
#include<stdio.h>
#include <stdlib.h>
#include<time.h>
/*引用函数库结束*/
/*定义全局变量*/
char operation;/*进入相应模块的运算符*/
int loop;/*每次做题的次数*/
int rightAnswer;/*做题正确次数*/
int flag;/*函数调用出错标记为0表示正确,为1表示出错了*/
/*定义全局变量结束*/
/*定义函数*/
void OutputProblemType()
{
printf("*******************************\n");
printf("请你选择一个类型做题\n");
printf("1\t做加法(+)的题\n");
printf("2\t做减法(-)的题\n");
printf("3\t做乘法(*)的题\n");
printf("*******************************\n");
}
int AcceptUserInputChoice()
{
int userInput;
fflush(stdin);
scanf("%d",&userInput);
return userInput;
}
void CheckChoice(int i)
{
switch(i)
{
case 1: operation='+';break;
case 2: operation='-';break;
case 3: operation='*';break;
default : flag=1;
}
}
void ComeInModule()
{
int i;
i=AcceptUserInputChoice();
flag=0;
CheckChoice(i);
while(flag)
{
printf("*******************************\n\n");
printf("你刚才输入不合要求,请重新输入");
printf("\n\n*******************************\n");
i=AcceptUserInputChoice();
flag=0;
CheckChoice(i);
}
}
int ProduceRandom()
{
srand( (unsigned)time( NULL ) );
return rand();
}
int BringRandom(int seed)
{
srand(seed);
return rand();
}
int HomeRandom(int seed)
{
srand(seed);
return rand();
}
void OutputDoNumber()
{
int number[10]={1,3,5,8,10,15,18,20,30,50};
int i;
printf("*******************************\n");
printf("请你选择你做题次数\n");
for(i=0;i<10;i++)
{
printf("你要做%d次\n",number[i]);
}
printf("*******************************\n");
loop=AcceptUserInputChoice();
flag=1;
for(i=0;i<10;i++)
{
if(loop==number[i])
{
flag=0;
break;
}
}
while(flag)
{
printf("*******************************\n\n");
printf("你刚才输入不合要求,请重新输入");
printf("\n\n*******************************\n");
loop=AcceptUserInputChoice();
flag=1;
for(i=0;i<10;i++)
{
if(loop==number[i])
{
flag=0;
break;
}
}
}
}
void Outputexpression(int first,int second,int third,char oper)
{
printf("*******************************\n\n");
printf("%d%c%d%c%d=",first,oper,second,oper,third);
printf("\n\n*******************************\n");
}
void OutputRightAnswer(int trueAnswer)
{
printf("*******************************\n\n");
printf("正确答案是%d",trueAnswer);
printf("\n\n*******************************\n");
}
void DealUserAnswer(int first,int second,int third,char oper,int answer)
{
int trueAnswer;
switch(oper)
{
case '+':trueAnswer=first+second+third;break;
case '-':trueAnswer=first-second-third;break;
case '*':trueAnswer=first*second*third;break;
}
if(trueAnswer==answer)
{
rightAnswer++;
}
else
{
OutputRightAnswer(trueAnswer);
}
}
void BeginDo()
{
int userAnswer;
int time;
int firstRandomNumber;
int secondRandomNumber;
int thirdRandomNumber;
for(time=0;time<loop;time++)
{
firstRandomNumber=ProduceRandom();
secondRandomNumber=BringRandom(firstRandomNumber);
thirdRandomNumber=HomeRandom(secondRandomNumber);
Outputexpression(firstRandomNumber,secondRandomNumber,thirdRandomNumber,operation);
userAnswer=AcceptUserInputChoice();
DealUserAnswer(firstRandomNumber,secondRandomNumber,thirdRandomNumber,operation,userAnswer);
}
}
void EndDoOutRightNumber()
{
printf("*******************************\n");
printf("你做对了%d题\n",rightAnswer);
}
int IsOver()
{
printf("继续输入其它\n结束输入0\n");
printf("*******************************\n");
return AcceptUserInputChoice();
}
/*定义函数结束*/
/*程序开始处*/
main()
{
do
{
OutputProblemType();/*输出题目类型供用户选择*/
ComeInModule();/*根据用户的输入进入相应的模块*/
OutputDoNumber();/*输出每次做题目的次数供用户选择*/
BeginDo();/*开始做题*/
EndDoOutRightNumber();/*做题目输出做对的个数*/
}while(IsOver());/*是否结束程序*/
}
/*程序结束*/