时间函数举例.docx
《时间函数举例.docx》由会员分享,可在线阅读,更多相关《时间函数举例.docx(4页珍藏版)》请在冰豆网上搜索。
时间函数举例
题目:
时间函数举例
1.程序分析:
2.程序源代码:
#include"stdio.h"
#include"time.h"
voidmain()
{
time_tlt;/*definealonginttimevarible*/
lt=time(NULL);/*systemtimeanddate*/
printf(ctime(<));/*englishformatoutput*/
printf(asctime(localtime(<)));/*tranfertotm*/
printf(asctime(gmtime(<)));/*tranfertoGreenwichtime*/
}
【程序92】
题目:
时间函数举例2
1.程序分析:
2.程序源代码:
/*calculatetime*/
#include"time.h"
#include"stdio.h"
main()
{
time_tstart,end;
inti;
start=time(NULL);
for(i=0;i<3000;i++)
{
printf("\1\1\1\1\1\1\1\1\1\1\n");
}
end=time(NULL);
printf("\1:
Thedifferentis%6.3f\n",difftime(end,start));
}
【程序93】
题目:
时间函数举例3
1.程序分析:
2.程序源代码:
/*calculatetime*/
#include"time.h"
#include"stdio.h"
main()
{
clock_tstart,end;
inti;
doublevar;
start=clock();
for(i=0;i<10000;i++)
{
printf("\1\1\1\1\1\1\1\1\1\1\n");
}
end=clock();
printf("\1:
Thedifferentis%6.3f\n",(double)(end-start));
}
【程序94】
题目:
时间函数举例4,一个猜数游戏,判断一个人反应快慢。
(版主初学时编的)
1.程序分析:
2.程序源代码:
#include"time.h"
#include"stdlib.h"
#include"stdio.h"
main()
{
charc;
clock_tstart,end;
time_ta,b;
doublevar;
inti,guess;
srand(time(NULL));
printf("doyouwanttoplayit.('y'or'n')\n");
loop:
while((c=getchar())=='y')
{
i=rand()%100;
printf("\npleaseinputnumberyouguess:
\n");
start=clock();
a=time(NULL);
scanf("%d",&guess);
while(guess!
=i)
{
if(guess>i)
{
printf("pleaseinputalittlesmaller.\n");
scanf("%d",&guess);
}
else
{
printf("pleaseinputalittlebigger.\n");
scanf("%d",&guess);
}
}
end=clock();
b=time(NULL);
printf("\1:
Ittookyou%6.3fseconds\n",var=(double)(end-start)/18.2);
printf("\1:
ittookyou%6.3fseconds\n\n",difftime(b,a));
if(var<15)
printf("\1\1Youareveryclever!
\1\1\n\n");
elseif(var<25)
printf("\1\1youarenormal!
\1\1\n\n");
else
printf("\1\1youarestupid!
\1\1\n\n");
printf("\1\1Congradulations\1\1\n\n");
printf("Thenumberyouguessis%d",i);
}
printf("\ndoyouwanttotryitagain?
(\"yy\".or.\"n\")\n");
if((c=getch())=='y')
gotoloop;