想用c设计一记时程序,能计算出程序运行时间的那种,怎么弄啊?
2个回答
展开全部
给你个完整的实例,《c语言学习经典100例》上面的,win-tc下运行通过。
/* 时间函数举例,一个猜数游戏,判断一个人反应快慢。*/
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
#include "time.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:(0-99)\n");
scanf("%d",&guess);
start=clock(); /* 计时开始 */
a=time(NULL);
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 Congratulations \1\1\n\n");
printf("The number you guess is %d",i);
}
printf("\nDo you want to try it again?('y'.or.'n')\n");
if((c=getch())=='y')
goto loop;
}
/* 时间函数举例,一个猜数游戏,判断一个人反应快慢。*/
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
#include "time.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:(0-99)\n");
scanf("%d",&guess);
start=clock(); /* 计时开始 */
a=time(NULL);
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 Congratulations \1\1\n\n");
printf("The number you guess is %d",i);
}
printf("\nDo you want to try it again?('y'.or.'n')\n");
if((c=getch())=='y')
goto loop;
}
展开全部
http://baike.baidu.com/view/1542771.htm
大致就是 1 获得当前时间
2 运行程序
3.获得当前时间
然后3 减去1 就是运行时间
下面有些函数你肯定能用得到
http://zhidao.baidu.com/question/18553054.html
以上 ,祝好
大致就是 1 获得当前时间
2 运行程序
3.获得当前时间
然后3 减去1 就是运行时间
下面有些函数你肯定能用得到
http://zhidao.baidu.com/question/18553054.html
以上 ,祝好
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询