当前位置:首页 » 编程语言 » c语言实现计时
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言实现计时

发布时间: 2023-01-05 09:40:11

c语言倒计时怎么编

定位要倒计时的坐标!然后再需要倒计时的地方添加gotoxy(所定位的坐标),当然这只是十以下的倒计时,超过了十则会清除不了十位数上的数字,要解决这个办法就是输出%2d

⑵ C语言程序运行计时

可以使用time.h里面的clock函数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
long i = 10000000L;
clock_t start, finish;
double ration; /* 测量一个事件持续的时间*/
printf( "Time to do %ld empty loops is ", i) ;
start = clock();
while(i--);
finish = clock();
ration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", ration );
system("pause");
return 0;
}

⑶ 怎么用c语言编写一个计时器!!!

调用win的api函数ExitWindowsEx();

#include <stdio.h>
#include <time.h>

main()
{
clock_t start,end;
int n;
printf("How many seconds do you want to count? ");
scanf("%d",&n);
getchar();
clrscr();
start=end=clock();
while((n-(int)(end-start)/19)>=0&!kbhit())
{
printf("the time is: %d",n-(int)(end-start)/19);
sleep(1);
end=clock();
clrscr();
}
ExitWindowsEx();
}

循环结束就可以了。
网上帮你找了下。
头文件: 在Winuser.h中定义;需要包含 Windows.h.
静态库: User32.lib.

【以上转贴】
【以下原创】
#include "ctime" //不要直接编,可能过不了,为C++,只是告知你大概方法

using namespace std;
//我写的一个类,调用API函数:
// gameTime类,用于时间控制
class gameTime
{
public:
gameTime();
~gameTime();
public:
DWORD tNow; //当前时间
DWORD tPre; //开始时间
private:
bool key; //运行控制
public:
bool getKey() { return key; }
void setKey( bool temp ) { key = temp; }

DWORD getTimeDelay(){ return (tNow - tPre);}
};

/**-----------------------------------------------------------------------------
* gameTime类实现段
*------------------------------------------------------------------------------
*/
gameTime::gameTime():tNow(0),tPre(0),key(false)
{

}
gameTime::~gameTime()
{

}
//原理是开始计时时:
tPre = GetTickCount();
///....执行。
gameStartTime.tNow = GetTickCount();
if(gameStartTime.getTimeDelay()>= 72000)
............

//在72S内做什么什么。。。

这个是控制时间间隔的。

⑷ 秒表计时程序要求(需要用C语言实现)

这里的分段计时,我使用空格键实现的,F2比较麻烦。程序开始,输入回车开始计时,中途输入空格可以开始新的计时,最后输入回车完成计时。

文件存在程序目录下的timeout.txt

真麻烦,下次这种求助才给10分,绝对不做。。。

//////////////////////////

我的代码就是在VS2010下写的。。。怎么会无法编译。。。你要建一个空工程,然后加入C++源文件。

/////////////////////////////

请新建一个空工程,不要新建Win32Console那个工程!

#include<stdio.h>

#include<conio.h>

#include<windows.h>

#include<stdlib.h>

structtm//定义时间结构体,包括时分秒和10毫秒

{

inthours,minutes,seconds;

inthscd;

}time,tmp,total;//time用以计时显示,tmp用以存储上一阶段时间,total记总时间

intcnt;

FILE*fout;

//每次调用update函数,相当于时间过了10ms

voipdate(structtm*t)

{

(*t).hscd++;//10ms单位时间加1

cnt++;

if((*t).hscd==100)//计时满1s,进位

{

(*t).hscd=0;

(*t).seconds++;

}

if((*t).seconds==60)//计时满一分,进位

{

(*t).seconds=0;

(*t).minutes++;

}

if((*t).minutes==60)//计时满一小时,进位

{

(*t).minutes=0;

(*t).hours++;

}

if((*t).hours==24)(*t).hours=0;

//delay();

Sleep(10);//Sleep是windows提供的函数,作用是暂停程序,单位毫秒,所以此处暂停10ms

}

voiddisplay(structtm*t)

{

//此处输出计时结果, 为回车不换行,既一直在同一行更新时间

printf("%d:",(*t).hours);

printf("%d:",(*t).minutes);

printf("%d:",(*t).seconds);

printf("%d ",(*t).hscd);

//printf("Now,press'e'keytostoptheclock...");

}

voidtime_init()//初始化时间

{

time.hours=time.minutes=time.seconds=time.hscd=0;

}

voidget_total()//计算总时间

{

total.hscd=cnt%100;

cnt/=100;

total.seconds=cnt%60;

cnt/=60;

total.minutes=cnt%60;

cnt/=60;

total.hours=cnt;

}

intmain()

{

charm;

time_init();

cnt=0;

fout=fopen("timeout.txt","w");

printf("Now,pressEnterkeytobegintheclock... ");

while(1)

{

m=getch();

if(m!=' ')//读入一个输入,如果是回车,那么跳出次循环

printf("InputError! ");

else

break;

}

printf("Whilecounting,! ");

while(1)

{

if(kbhit())//此处检查是否有键盘输入

{

m=getch();

if(m==' ')//如果等于回车,那么计时结束,跳出循环

break;

elseif(m=='')//如果等于空格,显示此次计时,初始化计时器

{

tmp=time;//记录上一段计时器结果

fprintf(fout,"%d:%d:%d:%d ",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);//写入文件

time_init();

printf(" ");

}

else

{

printf("InputError! ");

}

}

update(&time);//更新计时器

display(&time);//显示计时器时间

}

tmp=time;//输出最后一次即使结果,写入文件

fprintf(fout,"%d:%d:%d:%d ",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);

get_total();//计算总的时间,显示,并写入文件

printf(" totaltime:%d:%d:%d:%d ",total.hours,total.minutes,total.seconds,total.hscd);

fprintf(fout,"totaltime:%d:%d:%d:%d ",total.hours,total.minutes,total.seconds,total.hscd);

fclose(fout);

getch();

}

⑸ 在C语言中如何实现精确计时

  1. time()
    头文件:time.h
    函数原型:time_t time(time_t * timer)
    功能:返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。

2.clock()
头文件:time.h
函数原型:clock_t clock(void);
功能:该函数返回值是硬件滴答数,要换算成秒,需要除以CLK_TCK或者 CLK_TCKCLOCKS_PER_SEC。比如,在VC++6.0下,这两个量的值都是1000。

3. timeGetTime()

头文件:Mmsystem.h 引用库: Winmm.lib
函数原型:DWORD timeGetTime(VOID);
功能:返回系统时间,以毫秒为单位。系统时间是从系统启动到调用函数时所经过的毫秒数。注意,这个值是32位的,会在0到2^32之间循环,约49.71天。

⑹ C语言写出计时器

boolsetTime1(inth,intm,ints)//倒计时
{
if((h<0)||(m<0||m>=60)||(s<0||s>=60))
{
printf("Timeseterror! ");
returnfalse;
}

while(h>0||m>0||s>0)
{
printf("%02d:%02d:%02d",h,m,s);

--s;
if(s<0)
{
s=59;
--m;
if(m<0)
{
m=59;
--h;
}
}
Sleep(1000);
system("cls");
}
returntrue;
}

boolsetTime2(inth,intm,ints)//正计时
{
inthour=0,min=0,sec=0;

if((h<0)||(m<0||m>=60)||(s<0||s>=60))
{
printf("Timeseterror! ");
returnfalse;
}

while(h!=hour||m!=min||s!=sec)
{
printf("%02d:%02d:%02d",hour,min,sec);

++sec;
if(sec==60)
{
sec=0;
++min;
if(min==60)
{
min=0;
++hour;
}
}
Sleep(1000);
system("cls");
}
returntrue;
}