使用rand生成两个正的一位整数,显示如下问题:How much is 6 times 7?
对正确答案的回应信息:
Very good!
Excellent!
Nice work!
Keep up the good work!
对错误答案的回应信息:
NO.Please try again.
Wrong.Try once more.
Don't give up!
NO.Keep trying.
使用随即数生成程序来选择从1~4之间的数字,以选择每个答案的回应信息。
使用带有printf语句的switch结构来发出这些回应信息。
计算出输入的正确答案的百分比,若百分比低于75%,程序输出“Please ask your instructor for extra help”,
然后结束程序运行。
注:以前发过但对回答不满意,有用goto语句解答的,但这里要求用函数解答。我作过这题但没有预期效果,大家帮忙看看:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int question(void);
float answer(int result,int rand1,int rand2);
void rAnswer(void);
void wAnswer(void);
void rateFunction(float);
static int rand1,rand2,result;
static int wcount=0,rcount=0;
static float rate;
int main()
{
srand((int)time(0));
do{
question();
answer(result,rand1,rand2);
rateFunction(rate);
}while(rcount+wcount==15);
return 0;
}
int question()
{
rand1=1+(int)(rand()%10);
rand2=1+(int)(rand()%10);
printf("How much is %d times %d",rand1,rand2);
scanf("%d",&result);
printf("%d",result);
return (result);
}
float answer(int result,int rand1,int rand2)
{
if (result==rand1*rand2)
{void rAnswer();
rcount++;
}
else
{void wAnswer();
wcount++;
}
rate=(float)rcount/(rcount+wcount);
return rate;
}
void rAnswer(void)
{ int rRand;
rRand=1+rand()%4;
switch(rRand)
{
case 1:printf("Very good!\n");break;
case 2:printf("Excellent!\n");break;
case 3:printf("Nice work!\n");break;
case 4:printf("Keep up the good work!\n");break;
}
}
void wAnswer(void)
{ int wRand;
wRand=1+rand()%4;
switch(wRand)
{
case 1:printf("No.pelase try again.\n");break;
case 2:printf("Wrong.Try once more.\n");break;
case 3:printf("Don't giue up!\n");break;
case 4:printf("No,keep trying.\n");break;
}
}
void rateFunction(float rate)
{ if (rate<=0.75)
{
printf("Please ask your instruyctor for extra help");
exit(1);
}
}