⑴ 用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."。因此,如果是编写服务器端程序,此处一定要万分注意,避免引起意外的状况。
这个是从网络上贴过来的。
⑵ c语言中怎么设置计时器
C语言计时可以用很多方法。
1.
如果是想使用秒级别的技术,可用使用C语言库<time.h>自带的clock()进行计时。如:
#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;
}
以上方法只能用于秒级别的计时。
2.如果想使用毫秒级别的计时可以使用2种方法。一种是使用linux的系统库<sys/time.h>,一种是使用CUDA的cutil的库。
A.
如果使用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;
/*
微秒数
*/
};
上述方法可以进行微妙级别的计时,当然也可以进行毫秒和秒的计时。
B.
如果可以使用CUDA的话,则可以使用CUDA的sdk里面的cutil库里面的函数。
#include
<cutil.h>
int
main()
{
unsigned
int
timer
=
0;
cutCreatTimer(&timer);//创建计时器
cutStartTimer(&timer);//开始计时
//
do
some
process
here
cutStopTimer(&timer);//停止计时
cout<<"time
is
"<<cutGetTimerValue(&timer)<<endl;//打印时间
}
⑶ C语言编写的计时器源代码怎么编写
#include
<time.h>
clock_t
start,end;
在开始计时的地方写:start
=
clock();
在结束的地方写:end
=
clock();
时间等于:t=(end
-
start)/CLOCKS_PER_SEC;
单位是秒
⑷ 怎么用c语言编写一个秒表,这个秒表按1停止,按0清零,求大神教!!
用到time.h文件
里面有个clock();函数,返回一个clock_t类型的数字,表示从程序运行开始,CPU的"滴答"数
而在time.h里有个常量CLOCKS_PER_SEC表示每秒钟有多少个"滴答".
这样,(((float)clock())/CLOCKS_PER_SEC)*1000这样的表达式就能得到从程序运行开始到现在的经过的时间.
程序的大致思路是这样的,程序按下1的时候记下当时的程序运行时间.
从这时起,每时刻捡取程序运行时间,然后减去先前的值,就可以得到已经计时的时间了.
只要让用户按下0结束计时就好了
在conio.h文件里,有个函数kbhit()是个非阻塞函数,用来检查键盘缓冲里有没有按键按下,若有,则返回1,若没有,则返回0,以此来作为判断,若返回1,则捡取按键,测试它是不是0或者1,若返回0,则表示用户没有动作,继续原来的工作,即继续计时或等待命令.
⑸ 如何用c#做一个秒表
由于目前用到了C#的有关知识,但之前没有C#的基础,所以趁着机会正好学习学习。
本篇博文,记录下利用C#实现一个简单的秒表计时器,基本界面如下图。
功能说明:点击“开始”开始计时,点击“暂停”暂停计时,点击“”停止“”停止计时,再点击“开始”,重新开始计时。
下一步工作:在左下方添加一个label,实验多次暂停的功能,即能保存秒表的多个中间结果,如记录多名同学的长跑成绩的时候,暂停按钮只是记录到达终点的同学的成绩,计时还在继续,这个功能不难实现,给自己也给各位一个动手的余地。
-------------------------------------------------------------------------------------------------------
以下是窗体设计器自动生成的代码,辅助参考。
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(204, 78);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(38, 23);
this.button1.TabIndex = 0;
this.button1.Text = "开始";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(205, 163);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(37, 23);
this.button3.TabIndex = 2;
this.button3.Text = "停止";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label1
//
this.label1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.label1.Font = new System.Drawing.Font("宋体", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(30, 33);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(202, 42);
this.label1.TabIndex = 3;
this.label1.Text = "0:0:0:0";
//
// button2
//
this.button2.Location = new System.Drawing.Point(205, 122);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(38, 23);
this.button2.TabIndex = 4;
this.button2.Text = "暂停";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.button2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.RightToLeftLayout = true;
this.Text = "秒表";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
⑹ 用51单片机C语言版设计秒表倒计时
K1 EQU P1.0
K2 EQU P1.1
K3 EQU P1.2
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP T0ISR
ORG 0030H
MAIN:
MOV TMOD,#01H
MOV TH0,#HIGH(65536-10000)
MOV TL0,#LOW(65536-10000)
SETB ET0
SETB TR0
SETB EA
MOV R0,#9
MOV R1,#9
MOV R2,#0
MOV R3,#99
CLR 00H
CLR 01H
LOOP:
JB K1,LP01
JNB K1,$
CLR 01H
INC R3
CJNE R3,#100,LOOP0
MOV R3,#0
SJMP LOOP0
LP01:
JB K2,LP02
JNB K2,$
CLR 01H
CJNE R3,#0,LP011
MOV R3,#99
SJMP LOOP0
LP011:
DEC R3
SJMP LOOP0
LP02:
JB K3,LOOP
JNB K3,$
SETB 01H
SJMP LOOP
LOOP0:
MOV A,R3
MOV B,#10
DIV AB
MOV R0,B
MOV R1,A
SJMP LOOP
T0ISR:
CLR TR0
MOV TH0,#HIGH(65536-10000)
MOV TL0,#LOW(65536-10000)
SETB TR0
MOV DPTR,#TABLE
T0C:
CPL 00H
JB 00H,T001
MOV P3,#0FDH
MOV A,R1
MOVC A,@A+DPTR
MOV P2,A
SJMP T002
T001:
MOV P3,#0FEH
MOV A,R0
MOVC A,@A+DPTR
MOV P2,A
T002:
JNB 01H,T0E
INC R2
MOV A,R2
CJNE A,#100,T0E
MOV R2,#0
MOV A,R0
JZ T003
DEC R0
SJMP T0E
T003:
MOV A,R1
JZ T004
DEC R1
MOV R0,#9
T0E:
RETI
T004:
CLR 01H
MOV R4,#16
T005:
CPL P0.0
LCALL DELAY
DJNZ R4,T005
RETI
DELAY:
MOV R6,#200
DLY:
MOV R7,#250
DJNZ R7,$
DJNZ R6,DLY
RET
TABLE: ; 共阴极数码管显示代码表
DB 3FH,06H,5BH,4FH,66H ;01234
DB 6DH,7DH,07H,7FH,6FH ;56789
END
⑺ c语言怎么样编写一个时钟程序
c语言时钟程序代码如下:
#include<windows.h>
#include<math.h>
#define ID_TIMER 1//计时器ID
#define TWOPI (2*3.14159)
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("Clock");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows
T"),szAppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,TEXT("Analog Clock"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void Setsotropic(HDC hdc,int cxClient,int cyClient)
{
SetMapMode(hdc,MM_ISOTROPIC);
SetWindowExtEx(hdc,1000,1000,NULL);
SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);
SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
}
void RotatePoint(POINT pt[],int iNum,int iAngle)
{
int i;
POINT ptTemp;
for(i=0;i<iNum;i++)
{
ptTemp.x=(int)(pt[i].x*cos(TWOPI*iAngle/360)+pt[i].y*sin(TWOPI*iAngle/360));
ptTemp.y=(int)(pt[i].y*cos(TWOPI*iAngle/360)+pt[i].x*sin(TWOPI*iAngle/360));
pt[i]=ptTemp;
}
}
void DrawClock(HDC hdc)
{
int iAngle;
POINT pt[3];
for(iAngle=0;iAngle<360;iAngle+=6)
{
pt[0].x=0;
pt[0].y=900;
RotatePoint(pt,1,iAngle);
pt[2].x=pt[2].y=iAngle%5?33:100;
pt[0].x-=pt[2].x/2;
pt[0].y-=pt[2].y/2;
pt[1].x=pt[0].x+pt[2].x;
pt[1].y=pt[0].y+pt[2].y;
SelectObject(hdc,GetStockObject(BLACK_BRUSH));
Ellipse(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y );
}
}
void DrawHands(HDC hdc,SYSTEMTIME *pst,BOOL fChange)
{
static POINT pt[3][5]={0,-150,100,0,0,600,-100,0,0,-150, 0,-200,50,0,0,800,-50,0,0,-200, 0,0,0,0,0,0,0,0,0,800 };
int i,iAngle[3];
POINT ptTemp[3][5];
iAngle[0]=(pst->wHour*30)%360+pst->wMinute/2;
iAngle[1]=pst->wMinute*6;
iAngle[2]=pst->wSecond*6;
memcpy(ptTemp,pt,sizeof(pt));
for(i=fChange?0:2;i<3;i++)
{
RotatePoint(ptTemp[i],5,iAngle[i]);
Polyline(hdc,ptTemp[i],5);
}
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static int cxClient,cyClient;
static SYSTEMTIME stPrevious;
BOOL fChange;
HDC hdc;
PAINTSTRUCT ps;
SYSTEMTIME st;
switch(message)
{
case WM_CREATE:
SetTimer(hwnd,ID_TIMER,1000,NULL);
GetLocalTime(&st);
stPrevious=st;
return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
case WM_TIMER:
GetLocalTime(&st);
fChange=st.wHour!=stPrevious.wHour||st.wMinute!=stPrevious.wMinute;
hdc=GetDC(hwnd);
Setsotropic(hdc,cxClient,cyClient);
SelectObject(hdc,GetStockObject(WHITE_PEN));
DrawHands(hdc,&stPrevious,fChange);
SelectObject(hdc,GetStockObject(BLACK_PEN));
DrawHands(hdc,&st,TRUE);
stPrevious=st;
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
Setsotropic(hdc,cxClient,cyClient);
DrawClock(hdc);
DrawHands(hdc,&stPrevious,TRUE);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
KillTimer(hwnd,ID_TIMER);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
⑻ 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语言中怎么设置计时器
#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;
}
(9)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语言怎么写秒计时器
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar tt,num;
uchar shi,ge,temp;
uchar code table[]=
{0x3f,0x06,0x5b,0x4f,<br>0x66,0x6d,0x7d,0x07,<br>0x7f,0x6f};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar shi,uchar ge)
{
shi=temp/10;
ge=temp%10; P2=table[shi];
P3=0xfe;
delay(1);
P2=0x00; P2=table[ge];
P3=0xfd;
delay(1);
P2=0x00;
}
void timer0() interrupt 1
{
TH0=-50000/256;
TL0=-50000%256;
tt++;
if(tt>=20)
{
tt=0;
temp++;
if(temp==60)
temp=0;
}
}
void init()
{
TMOD=0x01;
TH0=-50000/256;
TL0=-50000%256;
EA=1;
ET0=1;
TR0=1;
tt=0;
temp=0;
}
void main()
{
init();
while(1)
{
display(shi,ge);
}
} 这个是60秒的,供参考