#include
#include
#include
#define NUMBER 10/*定义数组项目的个数为一个符号常量*/
int Num1[10]= {0},Num2[10]= {0},Pos1=0,Pos2=0;
/*定义两个数组来保存已经出现的情况*/
int main()
{
int putquestion();/*声明出题函数*/
int questionnum=10;/*声明变量*/
int i=1;
printf("Please input number of question:");
scanf("%d",&questionnum);
int answer[NUMBER];/*声明一个数组,用于储存学生的答案*/
int rightanswer[NUMBER];/*声明一个数组,用于储存正确答案*/
for(i=1; i<=questionnum; i++)
{
rightanswer[i-1]=putquestion();
scanf("%d",&answer[i-1]);
}
for(i=1; i<=NUMBER; i++)
printf("\n%d%5d",answer[i-1],rightanswer[i-1]);
}
bool exist(int goal,int *Num)
{/*检查数字goal是否已经出现在Num数组里面*/
int i;
for(i=0; i
return true;
return false;
}
int putquestion()/*此函数用于出题*/
{
int randValue;
int num1;
int num2;
int correctanswer;
srand((int)time(NULL));
randValue=1+rand()%4;
num1=1+rand()%99;
num2=1+rand()%99;
switch(randValue)
{
case 1:/*加法作业*/
while(num1+num2>=100||exist(num1,Num1)&&exist(num2,Num2))
{/*当满足该条件(满足题目要求且两个数同时出现过)时继续循环*/
num1=1+rand()%99;
num2=1+rand()%99;
}
Num1[Pos1++]=num1;
Num2[Pos2++]=num2;
printf("\n%d+%d=",num1,num2);
correctanswer=num1+num2;
break;
case 2:/*减法作业*/
while(num1-num2<0||exist(num1,Num1)&&exist(num2,Num2))
{
num1=1+rand()%99;
num2=1+rand()%99;
}
Num1[Pos1++]=num1;
Num2[Pos2++]=num2;
printf("\n%d-%d=",num1,num2);
correctanswer=num1-num2;
break;
case 3:/*乘法作业*/
while(num1*num2>=100||exist(num1,Num1)&&exist(num2,Num2))
{
num1=1+rand()%99;
num2=1+rand()%99;
}
Num1[Pos1++]=num1;
Num2[Pos2++]=num2;
printf("\n%d*%d=",num1,num2);
correctanswer=num1*num2;
break;
case 4:/*除法作业*/
while(num1%num2!=0||exist(num1,Num1)&&exist(num2,Num2))
{
num1=1+rand()%99;
num2=1+rand()%99;
}
Num1[Pos1++]=num1;
Num2[Pos2++]=num2;
printf("\n%d/%d=",num1,num2);
correctanswer=num1/num2;
break;
}
return(correctanswer);
}