跪求C语言源程序注释 急急急 3号之前解决 多谢多谢
注释详细点题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)1.程序分析:2.程序源代码:#include"time.h"#include"std...
注释详细点
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)
1.程序分析:
2.程序源代码:
#include "time.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
main()
{
char c;
clock_t start,end;
time_t a,b;
double var;
int i,guess;
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=getch())=='y')
goto loop;
} 展开
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)
1.程序分析:
2.程序源代码:
#include "time.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
main()
{
char c;
clock_t start,end;
time_t a,b;
double var;
int i,guess;
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=getch())=='y')
goto loop;
} 展开
1个回答
展开全部
//定义头文件
#include "time.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
//主程序入口
main()
{
//变量定义
char c; //用户输入变量
clock_t start,end; //时间变量
time_t a,b; //时间变量
double var; //绝对时间
int i,guess; //答案变量以及用户猜的数据
srand(time(NULL)); //用当前的时间为初始化随机种子,srand函数用于初始化随机种子
printf("do you want to play it.('y' or 'n') \n"); //给出用户输入的提示信息,printf函数用于打印信息
loop: //goto语句的入口,这里用于循环
while((c=getchar())=='y') //如果用户输入为y,那么进入循环
{
/*游戏开始*/
i=rand()%100; /*随机产生需要猜测的数据i,范围100以内,rand函数用于产生随机数*/
printf("\nplease input number you guess:\n"); //给出用户输入的提示信息,printf函数用于打印信息
start=clock(); /*计时开始,clock函数获得当前时间*/
a=time(NULL); /*计时开始,timeh函数获得当前时间*/
scanf("%d",&guess); /*用户输入猜测的数据,scanf函数用于请求用户输入*/
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) /*小于15,优秀评级*/
printf("\1\1 You are very clever! \1\1\n\n");
else if(var<25) /*小于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=getch())=='y') //如果用户输入为y,那么继续刚才的循环
goto loop; //goto语句跳到loop的位置,程序继续
}
说明:
1.clock()函数和time函数在程序中的作用是相同的,clock函数的精确度相对来说要高点.
2.printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);有问题,程序员希望得到秒级的时间,但windows中不应该/18.2 而是:
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/CLOCKS_PER_SEC);
#include "time.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
//主程序入口
main()
{
//变量定义
char c; //用户输入变量
clock_t start,end; //时间变量
time_t a,b; //时间变量
double var; //绝对时间
int i,guess; //答案变量以及用户猜的数据
srand(time(NULL)); //用当前的时间为初始化随机种子,srand函数用于初始化随机种子
printf("do you want to play it.('y' or 'n') \n"); //给出用户输入的提示信息,printf函数用于打印信息
loop: //goto语句的入口,这里用于循环
while((c=getchar())=='y') //如果用户输入为y,那么进入循环
{
/*游戏开始*/
i=rand()%100; /*随机产生需要猜测的数据i,范围100以内,rand函数用于产生随机数*/
printf("\nplease input number you guess:\n"); //给出用户输入的提示信息,printf函数用于打印信息
start=clock(); /*计时开始,clock函数获得当前时间*/
a=time(NULL); /*计时开始,timeh函数获得当前时间*/
scanf("%d",&guess); /*用户输入猜测的数据,scanf函数用于请求用户输入*/
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) /*小于15,优秀评级*/
printf("\1\1 You are very clever! \1\1\n\n");
else if(var<25) /*小于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=getch())=='y') //如果用户输入为y,那么继续刚才的循环
goto loop; //goto语句跳到loop的位置,程序继续
}
说明:
1.clock()函数和time函数在程序中的作用是相同的,clock函数的精确度相对来说要高点.
2.printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);有问题,程序员希望得到秒级的时间,但windows中不应该/18.2 而是:
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/CLOCKS_PER_SEC);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询