请C语言高手帮我修改一下这个程序··

#include "time.h"
#include "stdlib.h"
#include "stdio.h"
main()
{char c;
clock_t start,end;
time_t a,b;
double var;
int i,guess;
printf("************************************************************************\n");

srand(time(NULL));
printf("do you want to play it.('y' or 'n') \n");
loop:
while((c=getchar())=='y')
{
i=rand()%100;
printf("\nplease input number you guess:\n");
start=clock();
a=time(NULL);
scanf("%d",&guess);
while(guess!=i)
{if(guess>i)
{printf("please input a little smaller.\n");
scanf("%d",&guess);}
else
{printf("please input a little bigger.\n");
scanf("%d",&guess);}
}
end=clock();
b=time(NULL);
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
if(var<15)
printf("\1\1 You are very clever! \1\1\n\n");
else if(var<25)
printf("\1\1 you are normal! \1\1\n\n");
else
printf("\1\1 you are stupid! \1\1\n\n");
printf("\1\1 Congradulations \1\1\n\n");
printf("The number you guess is %d",i);
}
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
if((c=getchar())=='y')
goto loop;

system("pause");
return 0;
}

这个程序运行的时候不轮我多快··最后都是出现“you are stupid!”不知道怎么办··高手帮我修改一下··谢谢大家

第一、初始选择的时候时候有bug,如果选择n,还会输出一遍:
我给你稍微改了下,下面我会把大妈发上来
第二、var定义成int就可以。。再笨的人也不会超过int范围的。。。
var=(end-start)/CLOCKS_PER_SEC);这里用CLOCKS_PER_SEC比较好,更具有通用性,别人的cpu不一定是18.2 。
同时printf("\1: It took you %6.3f seconds\n",var=(end-start)/CLOCKS_PER_SEC);这句里:6.3f改成%d。
第三、printf("\1: it took you %6.3f seconds\n",difftime(b,a));
这句什么意思??没必要吧

下面是代码,去掉的东西,我注释起来了,你对比看下
#include "time.h"
#include "stdlib.h"
#include "stdio.h"

void main()
{
char c;
clock_t start,end;
//time_t a,b;
int var;
int i,guess;
printf("************************************************************************\n");

//srand(time(NULL));
printf("do you want to play it.('y' or 'n') \n");
bool choice=false;
if((c=getchar())=='y')
choice = true;

while(choice)
{
i=rand()%100;
printf("\nplease input number you guess:\n");
start=clock();
//a=time(NULL);
scanf("%d",&guess);
while(guess!=i)
{
if(guess>i)
{
printf("please input a little smaller.\n");
scanf("%d",&guess);}
else
{
printf("please input a little bigger.\n");
scanf("%d",&guess);}
}
end=clock();
//b=time(NULL);
printf("\1: It took you %d seconds\n",var=(end-start)/CLOCKS_PER_SEC);
//printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
if(var<15)
printf("\1\1 You are very clever! \1\1\n\n");
else if(var<25)
printf("\1\1 you are normal! \1\1\n\n");
else
printf("\1\1 you are stupid! \1\1\n\n");
printf("\1\1 Congradulations \1\1\n\n");
printf("The number you guess is %d\n",i);
/********************************************************/
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
getchar();//从缓存里把之前的“回车”拿走
if((c=getchar())=='y')
choice=true;
else
choice=false;
}

system("pause");

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-03-31
先讲一下time()和clock()的区别:
time返回从1970年1月1日0时到现在的秒数,即系统时间。
clock是自进程启动后此进程使用cpu的总毫秒数,常用来测试任务执行的速度。
明显可以看到,你所计算的var时间只要大于18.2*25/1000=0.455s都是stupid的,而这个时间还不够你按一个按键的时间呢,
改的话只要把时间调大点就行啦本回答被提问者采纳
第2个回答  2009-03-31
var没有初始化,而且每次重新游戏时,var没有置为0
你需要在
i=rand()%100;
前加上一句,var=0;

这么改:
...
loop:
while((c=getchar())=='y')
{
var=0; //加在这里!!!
i=rand()%100;
printf("\nplease input number you guess:\n");
start=clock();
a=time(NULL);
...
第3个回答  2009-03-31
你想要达到什么目的呀?
相似回答