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

来源:百度知道 编辑:UC知道 时间:2024/06/08 01:22:39
#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

先讲一下time()和clock()的区别:
time返回从1970年1月1日0时到现在的秒数,即系统时间。
clock是自进程启动后此进程使用cpu的总毫秒数,常用来测试任务执行的速度。
明显可以看到,你所计算的var时间只要大于18.2*25/1000=0.455s都是stupid的,而这个时间还不够你按一个按键的时间呢,
改的话只要把时间调大点就行啦

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);
...

第一、初始选择的时候时候有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"