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

c语言毫秒计时器

发布时间: 2022-05-01 01:52:23

㈠ 怎么用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语言编程计时器如何工作

秒表计时器的代码
#include

#include

#include

#include

struct
tm
//定义时间结构体,包括时分秒和10毫秒
{
int
hours,minutes,seconds;
int
hscd;
}time,tmp,total;
//time用以计时显示,tmp用以存储上一阶段时间,total记总时间
int
cnt;
file*
fout;
//每次调用update函数,相当于时间过了10ms
void
update(struct
tm
*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
}
void
display(struct
tm
*t)
{
//此处输出计时结果,\r为回车不换行,既一直在同一行更新时间
printf("%d:",(*t).hours);
printf("%d:",(*t).minutes);
printf("%d:",(*t).seconds);
printf("%d\r",(*t).hscd);
//printf("now,
press
‘e’
key
to
stop
the
clock…");
}
void
time_init()
//初始化时间
{
time.hours=time.minutes=time.seconds=time.hscd=0;
}
void
get_total()
//计算总时间
{
total.hscd
=
cnt
%
100;
cnt
/=
100;
total.seconds
=
cnt
%
60;
cnt
/=
60;
total.minutes
=
cnt
%
60;
cnt
/=
60;
total.hours
=
cnt;
}
int
main()
{
char
m;
time_init();
cnt
=
0;
fout
=
fopen("timeout.txt","w");
printf("按回车键开始计时!\n");
while(1)
{
m
=
getch();
if(m
!=
‘\r’)
//读入一个输入,如果是回车,那么跳出次循环
printf("输入错误,仅能输入回车键!\n");
else
break;
}
printf("已经开始计时,但是你可以按回车键以分段计时!\n");
while(1)
{
if(kbhit())
//此处检查是否有键盘输入
{
m=getch();
if(m
==
‘\r’)
//如果等于回车,那么计时结束,跳出循环
break;
else
if(m
==

‘)
//如果等于空格,显示此次计时,初始化计时器
{
tmp
=
time;
//记录上一段计时器结果
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);
//写入文件
time_init();
printf("\n");
}
else
{
printf("输入错误,仅支持输入回车键或者空格键!\n");
}
}
update(&time);
//更新计时器
display(&time);
//显示计时器时间
}
tmp
=
time;
//输出最后一次即使结果,写入文件
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);
get_total();
//计算总的时间,显示,并写入文件
printf("\n总时间:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fprintf(fout,"统计时间:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fclose(fout);
printf("已经保存到当前目录下的timeout.txt文件中按任意键结束!");
getch();
}

㈢ 怎么用C语言编定时器

Windows提供了定时器,帮助我们编写定期发送消息的程序。定时器一般通过一下两中方式通知应用程序间隔时间已到。
⑴ 给指定窗口发送WM_TIMER消息,也就是下面的给出在窗口类中使用的方法。
⑵ 调用一个应用程序定义的回调函数,也就是在非窗口类中使用方法。

4.1 在窗口类中使用定时器
在窗口类中使用定时器比较简单。假如我们想让这个窗口上放置一个电子钟,这样我们必须每1秒或者0.5秒钟去更新显示显见。按照下面的步骤,就可以完成这个电子钟程序,并且知道如何在窗口类中使用定时器:
首先做在我们新建项目的主窗口上添加一个Label控件,用来显示时间。接着
⑴ 用函数SetTimer设置一个定时器,函数格式如下: UINT SetTimer( UINT nIDEvent,
UINT nElapse,
void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD));

这个函数是CWnd类的一个成员函数,其参数意义如下:

nIDEvent: 为设定的定时器指定的定时器标志值,设置多个定时器的时候,每个定时器的值都不同,消息处理函数就是通过这个参数来判断是哪个定时器的。这里我们设定为1。
nElapse: 指定发送消息的时间间隔,单位是毫秒。这里我们设定为1000,也就是一秒。
lpfnTimer: 指定定时器消息由哪个回调函数来执行,如果为空,WM_TIMER将加入到应用程序的消息队列中,并由CWnd类来处理。这里我们设定为NULL。
最后代码如下:SetTimer(1,1000,NULL);
⑵ 通过Class Wizard给主窗口类添加一个WM_TIMER消息的映射函数,默认为OnTimer(UINT nIDEvent)。
⑶ 然后我们就可以在OnTimer(UINT nIDEvent)的函数实现中添加我们的代码了。参数nIDEvent就是我们先前设定定时器时指定的标志值,在这里我们就可以通过它来区别不同的定时器,而作出不同的处理。添加的代码如下:switch(nIDEvent)
{
case 1:
CTime m_SysTime = CTime::GetCurrentTime();
SetDlgItemText(IDC_STATIC_TIME,m_SysTime.Format("%Y年%m月%d日 %H:%M:%S"));
break;
}

代码中的IDC_STATIC_TIME就是我们先前添加的Label控件的ID。
至此,我们的电子钟的程序就完成了。

4.2 在非窗口类中使用定时器
在非窗口类中使用定时器就要用到前面我们介绍到的所有知识了。因为是无窗口类,所以我们不能使用在窗口类中用消息映射的方法来设置定时器,这时候就必须要用到回调函数。又因为回调函数是具有一定格式的,它的参数不能由我们自己来决定,所以我们没办法利用参数将this传递进去。可是静态成员函数是可以访问静态成员变量的,因此我们可以把this保存在一个静态成员变量中,在静态成员函数中就可以使用该指针,对于只有一个实例的指针,这种方法还是行的通的,由于在一个类中该静态成员变量只有一个拷贝,对于有多个实例的类,我们就不能用区分了。解决的办法就是把定时器标志值作为关键字,类实例的指针作为项,保存在一个静态映射表中,因为是标志值是唯一的,用它就可以快速检索出映射表中对应的该实例的指针,因为是静态的,所以回调函数是可以访问他们的。
首先介绍一下用于设置定时的函数:

UINT SetTimer(
HWND hWnd, // handle of window for timer messages
UINT nIDEvent, // timer identifier
UINT uElapse, // time-out value
TIMERPROC lpTimerFunc // address of timer procere
);

其中的参数意义如下:
hWnd: 指定与定时器相关联的窗口的句柄。这里我们设为NULL。
nIDEvent: 定时器标志值,如果hWnd参数为NULL,它将会被跳过,所以我们也设定为NULL。
uElapse: 指定发送消息的时间间隔,单位是毫秒。这里我们不指定,用参数传入。
lpTimerFunc: 指定当间隔时间到的时候被统治的函数的地址,也就是这里的回调函数。这个函数的格式必须为以下格式:
VOID CALLBACK TimerProc(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
);

其中的参数意义如下:
hwnd: 与定时器相关联的窗口的句柄。
uMsg: WM_TIMER消息。
idEvent: 定时器标志值。
deTime: 系统启动后所以经过的时间,单位毫秒。
最后设定定时器的代码为:m_nTimerID = SetTimer(NULL,NULL,nElapse,MyTimerProc);
先通过Class Wizard创建一个非窗口类,选择Generic Class类类型,类名称为CMyTimer,该类的作用是每隔一段时间提醒我们做某件事情,然后用这个类创建三个实例,每个实例以不同的时间间隔提醒我们做不同的事情。
MyTimer.h#include

class CMyTimer;
//用模板类中的映射表类定义一种数据类型
typedef CMap CTimerMap;

class CMyTimer
{
public:
//设置定时器,nElapse表示时间间隔,sz表示要提示的内容
void SetMyTimer(UINT nElapse,CString sz);
//销毁该实例的定时器
void KillMyTimer();
//保存该实例的定时器标志值
UINT m_nTimerID;
//静态数据成员要提示的内容
CString szContent;
//声明静态数据成员,映射表类,用于保存所有的定时器信息
static CTimerMap m_sTimeMap;
//静态成员函数,用于处理定时器的消息
static void CALLBACK MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
CMyTimer();
virtual ~CMyTimer();
};

MyTimer.cpp#include "stdafx.h"
#include "MyTimer.h"

//必须要在外部定义一下静态数据成员
CTimerMap CMyTimer::m_sTimeMap;

CMyTimer::CMyTimer()
{
m_nTimerID = 0;
}

CMyTimer::~CMyTimer()
{
}

void CALLBACK CMyTimer::MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
CString sz;
sz.Format("%d号定时器:%s",
idEvent,
m_sTimeMap[idEvent]->szContent);
AfxMessageBox(sz);
}

void CMyTimer::SetMyTimer(UINT nElapse,CString sz)
{
szContent = sz;
m_nTimerID = SetTimer(NULL,NULL,nElapse,MyTimerProc);
m_sTimeMap[m_nTimerID] = this;
}

void CMyTimer::KillMyTimer()
{
KillTimer(NULL,m_nTimerID);
m_sTimeMap.RemoveKey(m_nTimerID);
}

这样就完成了在非窗口类中使用定时器的方法。以上这些代码都在Windwos 2000 Professional 和 Visual C++ 6.0中编译通过。

㈣ C语言,计时器

秒表计时器的代码
#include
<stdio.h>
#include
<conio.h>
#include
<windows.h>
#include
<stdlib.h>
struct
tm
//定义时间结构体,包括时分秒和10毫秒
{
int
hours,minutes,seconds;
int
hscd;
}time,tmp,total;
//time用以计时显示,tmp用以存储上一阶段时间,total记总时间
int
cnt;
FILE*
fout;
//每次调用update函数,相当于时间过了10ms
void
update(struct
tm
*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
}
void
display(struct
tm
*t)
{
//此处输出计时结果,\r为回车不换行,既一直在同一行更新时间
printf("%d:",(*t).hours);
printf("%d:",(*t).minutes);
printf("%d:",(*t).seconds);
printf("%d\r",(*t).hscd);
//printf("Now,
press
‘e’
key
to
stop
the
clock…");
}
void
time_init()
//初始化时间
{
time.hours=time.minutes=time.seconds=time.hscd=0;
}
void
get_total()
//计算总时间
{
total.hscd
=
cnt
%
100;
cnt
/=
100;
total.seconds
=
cnt
%
60;
cnt
/=
60;
total.minutes
=
cnt
%
60;
cnt
/=
60;
total.hours
=
cnt;
}
int
main()
{
char
m;
time_init();
cnt
=
0;
fout
=
fopen("timeout.txt","w");
printf("按回车键开始计时!\n");
while(1)
{
m
=
getch();
if(m
!=
‘\r’)
//读入一个输入,如果是回车,那么跳出次循环
printf("输入错误,仅能输入回车键!\n");
else
break;
}
printf("已经开始计时,但是你可以按回车键以分段计时!\n");
while(1)
{
if(kbhit())
//此处检查是否有键盘输入
{
m=getch();
if(m
==
‘\r’)
//如果等于回车,那么计时结束,跳出循环
break;
else
if(m
==

‘)
//如果等于空格,显示此次计时,初始化计时器
{
tmp
=
time;
//记录上一段计时器结果
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);
//写入文件
time_init();
printf("\n");
}
else
{
printf("输入错误,仅支持输入回车键或者空格键!\n");
}
}
update(&time);
//更新计时器
display(&time);
//显示计时器时间
}
tmp
=
time;
//输出最后一次即使结果,写入文件
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);
get_total();
//计算总的时间,显示,并写入文件
printf("\n总时间:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fprintf(fout,"统计时间:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fclose(fout);
printf("已经保存到当前目录下的timeout.txt文件中按任意键结束!");
getch();
}

㈤ 我想用C语言做一个计时器,也就是秒表,按下一个键开始计时,可不可以显示读秒过程都行,可以的话最好,

如果对时间的精度要求是s的话,采用time()即可,该函数的返回值是自1970年1月1日0点0分之后的秒数。
采用getchar函数获取输入字符,然后获取一个time值,等到在输入一个字符后,在获取一次time值,两者相减就是经过的秒数。

㈥ 用C语言编写一个分秒计时器 大侠们 帮帮我!!

这什么东西啊,也太乱了吧!用一个GetTickCount这个API函数;
GetTickCount函数
函数功能:GetTickCount返回(retrieve)从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD。
函数原型: DWORD GetTickCount(void);
C/C++头文件:winbase.h
windows程序设计中可以使用头文件windows.h
程序示例
//代替time函数来初始化随机数生成器
#include<stdio.h>
#include<windows.h>
int main()
{
int i,k,r;
for(i=0;i<10;i++)
{
srand(GetTickCount());
printf("\n");
for(k=0;k<5;k++)
{
r=rand();
printf("%d ",r);
}
}
return 0;
}
注意事项
GetTickcount函数:它返回从操作系统启动到当前所经过的毫秒数,常常用来判断某个方法执行的时间,其函数原型是DWORD GetTickCount(void),返回值以32位的双字类型DWORD存储,因此可以存储的最大值是2-1 ms约为49.71天,因此若系统运行时间超过49.71天时,这个数就会归0,MSDN中也明确的提到了:"Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days."。因此,如果是编写服务器端程序,此处一定要万分注意,避免引起意外的状况。
这个是从网络上贴过来的。

㈦ 51单片机C语言设置一个精确到毫秒的倒计时

51单片机 12MHz
汇编:DELAY: ;误差 0us
MOV R7,#13H
DL1:
MOV R6,#14H
DL0:
MOV R5,#82H
DJNZ R5,$
DJNZ R6,DL0
DJNZ R7,DL1
RET
C:
void delay(void) //误差 0us
{
unsigned char a,b,c;
for(c=19;c>0;c--)
for(b=20;b>0;b--)
for(a=130;a>0;a--);
}。

㈧ c语言中怎么设置计时器

#include <iostream>

#include <time.h>

using namespace std;

int main()

{

clock_t start = clock();

//do some process here

clock_t end = (clock() - start)/CLOCKS_PER_SEC;

cout<<"time comsumption is "<<end<<endl;

}

(8)c语言毫秒计时器扩展阅读

使用linux的系统设置计时器

#include <sys/time.h>

int main()

{

timeval starttime,endtime;

gettimeofday(&starttime,0);

//do some process here

gettimeofday(&endtime,0);

double timeuse = 1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - startime.tv_usec;

timeuse /=1000;//除以1000则进行毫秒计时,如果除以1000000则进行秒级别计时,如果除以1则进行微妙级别计时

}

timeval的结构如下:

strut timeval

{

long tv_sec; /* 秒数 */

long tv_usec; /* 微秒数 */

};

㈨ 秒表计时程序要求(需要用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语言电子毫秒表计时程序

使用time()函数。它在头文件time.h中
具体使用方法如下:
time_t
a,b;//time_t是表示时间的结构体,你可以在time.h中找到它的原型。
a=time(null);//表示获取当前的机器时间。
代码段
b=time(null);//表示获取当前的机器时间。
a是代码段执行前的时间,b是代码段执行后的时间(单位是秒),那么b-a当然是代码段的执行时间了。输出时,以长整型输出时间。
希望这个解答可以帮到你。