| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 412 人关注过本帖
标题:[讨论] 一道貌似很简单的一道题(再发)
只看楼主 加入收藏
enchk
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2006-9-23
收藏
 问题点数:0 回复次数:1 
[讨论] 一道貌似很简单的一道题(再发)

使用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);
}
}

2007-04-18 21:39
Javal
Rank: 1
等 级:新手上路
威 望:1
帖 子:108
专家分:0
注 册:2006-5-7
收藏
得分:0 

在你的程序上稍微修改了一下,你看能不能符合要求

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define DEGREE 10 // 提问的次数

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(void)
{
srand((int)time(0));
do{
question();
answer(result,rand1,rand2);
}while(rcount+wcount!=DEGREE);
rateFunction(rate);

return 0;
}

int question(void)
{
rand1=1+(int)(rand()%10);
rand2=1+(int)(rand()%10);
printf("How much is %d times %d\n",rand1,rand2);
scanf("%d",&result);
//printf("%d",result);
return (result);
}

float answer(int result,int rand1,int rand2)
{
if (result==rand1*rand2)
{
rAnswer();
rcount++;
}
else
{
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!\n");
exit(1);
}
}


猝然临之而不惊,无故加之而不怒 /?spaced" target="_blank">Linux C资料
2007-04-19 09:21
快速回复:[讨论] 一道貌似很简单的一道题(再发)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037591 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved