⑴ 用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秒的,供參考