當前位置:首頁 » 編程軟體 » 電腦編程時鍾

電腦編程時鍾

發布時間: 2024-05-30 18:24:35

A. 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);

}

B. c++編程定義一時鍾類Clock

#include <iostream> using namespace std; //時間類 class Time{ private: int hour; int minute; int second; public: //設置時間 void set(int h,int m,int s){ hour = h; minute = m; second = s; } //時間走一秒,時分秒的變化情況 void next(){ if(second<59) second++; else if(minute<59){ second=0; minute++;} else if(hour<23){ minute=0; hour++;} else hour=0; } //得到時間 int get(){ return hour*10000+minute*100+second; } }; //時鍾類 class Clock{ private: Time now; Time ring_time; public: //對表,設定初始時間 void adjust_now(int h,int m,int s){ now.set(h,m,s); cout<<"現在的時間是:"<<h<<"時"<<m<<"分"<<s<<"秒"<<endl; } //設定鬧鈴時間 void adjust_ring(int h,int m,int s){ ring_time.set(h,m,s); cout<<"鬧鈴時間是:"<<h<<"時"<<m<<"分"<<s<<"秒"<<endl; } //時間過一秒 void tick(){ long int old=time(0); while(time(0)==old) ; now.next(); } //顯示當前時間 void showtime(){ cout<<now.get()<<endl; } //時鍾開始走時,等到了鬧鈴時間,開始響 void run(){ do{ tick(); showtime(); if(now.get()>=ring_time.get()) cout<<'\a'; }while(1); } }; int main(){ Clock c; c.adjust_now(18,35,40); //起始時間 c.adjust_ring(18,35,45); //鬧鈴時間 c.run(); }

C. 編程中的時鍾時間是如何得到

簡單的說,有幾種時鍾,可部分參見clock_gettime函數描述。(1)掛鍾,返回的是系統內核裡面的一個變數。這個變數是從主板上的CMOS晶元里讀出的。CMOS晶元是獨立供電的,在電腦斷電後也有小電池供電,就像電子表一樣計時。(2)CPU的計時,是CPU有一個寄存器不斷自動累加,比如2GHz的CPU每0.5ns就+1。(3)可編程時鍾,有低精度的和高精度的兩種,其實都是主板上的晶元,受到CPU的控制。
這個描述非常簡化,系統在其中做了很多事情,其實linux時鍾系統非常復雜,詳情請參閱有關書籍。

D. 鐢╲b鍒朵綔灝忕▼搴忊斺旀椂閽

鐢╲b鍒朵綔綆鍗曠殑瀹炵敤宸ュ叿錛屽皬鏃墮挓銆
鎵撳紑浣犵殑vb紼嬪簭錛屾坊鍔犱竴涓鏍囧噯宸ョ▼錛屽傚浘錛屽崟鏈烘墦寮

寰呬綘榪涜岀紪紼嬬殑鐣岄潰

娣誨姞涓変釜line鎺т歡錛屼竴涓猼imer鎺т歡錛屼竴涓猻hape鎺т歡錛岋紙鍙屽嚮鎺т歡鍗沖彲娣誨姞涓涓錛

淇鏀箃imer鐨勫睘鎬э紝鍦ㄥ彸渚х殑灞炴х獥鍙e唴銆備篃鍙浠ュ崟鏈哄皬鏃墮挓錛屾ゆ椂鍙抽潰灞炴ф爮鏄劇ず涓哄傚浘紺虹殑欏圭洰銆傝繘琛岀紪杈戝氨鍙浠ヤ簡銆傛妸ennabled淇鏀逛負true錛宨nterval淇鏀逛負1000

鍙屽嚮紿椾綋絀虹櫧澶勶紝涔熷氨鏄娌℃湁鎺т歡鐨勫湴鏂癸紝緙栬緫浠g爜錛屽備笅鍥

鍐嶆潵緙栬緫鍑鴻繖涓浠g爜錛

緇х畫緙栬緫錛岃繖嬈℃槸timer錛屽氨鏄灝忔椂閽熺殑鏃墮棿銆傛瘡1000ms錛堜慨鏀圭殑interval涓1000錛夊嵆涓縐掗挓錛屽畠鐨勬椂闂村搷搴斾竴嬈°

鎸塅5鎴栧崟鏈鴻繍琛岃皟璇曞浘鏍囪繍琛

榪愯屾垚鍔熻繕鍙浠ワ紝鐐瑰嚮鏂囦歡-鐢熸垚宸ョ▼form.exe錛屾斁鍒頒綘鎯寵佺殑鍦版柟錛屾瘮濡傛岄潰錛屽拰浣犱笅杞界殑搴旂敤宸涓嶅氾紝鏈鍚庣偣鍑諱繚瀛樺嵆鍙閫鍑恆

E. VB程序設計怎麼做數字時鍾

1、添加一個「label控制項」命名為label
2、添加一個「timer控制項」命名為timer1
3、設置「timer1」的「Interval屬性」為1000
使用到的代碼:

DimHourAsInteger'小時
DimMinAsInteger'分鍾
DimSecAsInteger'秒
PrivateSubForm_Load()
Hour=0
Min=0
Sec=0
Label1.Caption="00:00:00"
EndSub

PrivateSubTimer1_Timer()
DimstrHourAsString
DimstrMinAsString
DimstrSecAsString
Sec=Sec+1
IfSec>=60Then
Sec=0
Min=Min+1
IfMin>=60Then
Min=0
Hour=Hour+1
IfHour>=24Then
Hour=0
EndIf
EndIf
EndIf
IfHour<10Then
strHour="0"&Hour
Else
strHour=Hour
EndIf
IfMin<10Then
strMin="0"&Min
Else
strMin=Min
EndIf
IfSec<10Then
strSec="0"&Sec
Else
strSec=Sec
EndIf
Label1.Caption=strHour&":"&strMin&":"&strSec
EndSub

F. 怎麼用C語言編程數字時鍾

1、以下常式實現時鍾的實時顯示基本要求: 1) 自行設計界面,模擬表盤式時鍾。要求界面美觀,清晰。2)數字同步顯示時間信息。


2、常式:

#include<graphics.h>
#include<math.h>
#include<dos.h>
#definePI3.1415926
//屏幕中心的坐標(640X480模式下)
#definemid_x320
#definemid_y240
intmain()
{intgraphdriver=DETECT,graphmode;
intend_x,end_y;
structtimecurtime;
floatth_hour,th_min,th_sec;
initgraph(&graphdriver,&graphmode,"C:\TC2");//初始化VGA屏幕模式
setbkcolor(BLACK);//使用黑色的背景色
while(!kbhit(0))//若有鍵盤輸入,則跳出,即是結束程序
{setcolor(GREEN);//把畫筆設為綠色
circle(mid_x,mid_y,180);//鍾的外圓
circle(mid_x,mid_y,150);//鍾的內圓
circle(mid_x,mid_y,1);//畫出鍾的圓心
gettime(&curtime);//取得系統當前時間
th_sec=(float)curtime.ti_sec*0.1047197551;//把秒針的角度化為弧度,為以後繪制時方便,下同
th_min=(float)curtime.ti_min*0.1047197551+th_sec/60.0;//分針的弧度
th_hour=(float)curtime.ti_hour*0.5235987755+th_min/12.0;//時度的弧度,注意整時是12等分的,所時乘的是3.14/180*5
//計算出時針的尾的坐標(時針長70)
end_x=mid_x+70*sin(th_hour);
end_y=mid_y-70*cos(th_hour);
setcolor(RED);
line(mid_x,mid_y,end_x,end_y);//用紅色線畫出時針
//計算出分針坐標(分針長110)
end_x=mid_x+110*sin(th_min);
end_y=mid_y-110*cos(th_min);
setcolor(RED);
line(mid_x,mid_y,end_x,end_y);//用紅色畫出分針
end_x=mid_x+140*sin(th_sec);
end_y=mid_y-140*cos(th_sec);
setcolor(RED);
line(mid_x,mid_y,end_x,end_y);//同上,畫出秒針,長為140
//畫出鍾盤上的刻度,刻度長20
line(140,240,160,240);//9點對應的大刻度
line(320,60,320,80);//12點對應的大刻度
line(500,240,480,240);//3點的刻度
line(320,420,320,400);//6點的刻度
line(410,395.7,400,378.4);//5點
line(475.7,330,458.4,320);//4點
line(475.7,150,458.4,160);//2點
line(410,84.3,400,101.6);//1點
line(230,84.3,240,101.6);//11點
line(164.3,150,181.6,160);//10點
line(164.3,330,181.6,320);//8點
line(230,395.7,240,378.4);//7點
sleep(BLUE);//這里應該是打錯,停止一秒,應為sleep(1000)
cleardevice();//清除屏幕上的顯示
}
closegraph();//關閉VGA屏幕,即返迴文本方式
return0;
}

G. c語言編程,怎麼計算時間

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

void main()

{

unsigned char time1[] = {10, 8, 31, 9, 26 };

unsigned char time2[] = { 10, 8, 31, 9, 50 };

struct tm t1 = {0};

struct tm t2 = {0};

time_t _t1;

time_t _t2;

double diff;

t1.tm_year = time1[0] + 100;

t1.tm_mon = time1[1];

t1.tm_mday = time1[2];

t1.tm_hour = time1[3];

t1.tm_min = time1[4];

t2.tm_year = time2[0] + 100;

t2.tm_mon = time2[1];

t2.tm_mday = time2[2];

t2.tm_hour = time2[3];

t2.tm_min = time2[4];

_t1 = _mkgmtime( &t1 );

_t2 = _mkgmtime( &t2 );

diff = difftime(_t2, _t1 );

printf( "相差 %.0f 分鍾 ", diff / 60 );

}

(7)電腦編程時鍾擴展閱讀:

C語言中有兩個相關的函數用來計算時間差,分別是:

time_t time( time_t *t) 與 clock_t clock(void)

頭文件: time.h

計算的時間單位分別為: s , ms

time_t 和 clock_t 是函數庫time.h 中定義的用來保存時間的數據結構

返回值:

1、time : 返回從公元1970年1月1號的UTC時間從0時0分0秒算起到現在所經過的秒數。如果參數 t 非空指針的話,返回的時間會保存在 t 所指向的內存。

2、clock:返回從「開啟這個程序進程」到「程序中調用clock()函數」時之間的CPU時鍾計時單元(clock tick)數。 1單元 = 1 ms。

所以我們可以根據具體情況需求,判斷採用哪一個函數。

具體用法如下例子:

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

int main()

{

time_t c_start, t_start, c_end, t_end;

c_start = clock(); //!< 單位為ms

t_start = time(NULL); //!< 單位為s

system("pause");

c_end = clock();

t_end = time(NULL);

//!<difftime(time_t, time_t)返回兩個time_t變數間的時間間隔,即時間差

printf("The pause used %f ms by clock() ",difftime(c_end,c_start));

printf("The pause used %f s by time() ",difftime(t_end,t_start));

system("pause");

return 0;

}

因此,要計算某一函數塊的佔用時間時,只需要在執行該函數塊之前和執行完該函數塊之後調用同一個時間計算函數。再調用函數difftime()計算兩者的差,即可得到耗費時間。

熱點內容
數據模型於資料庫系統 發布:2024-07-27 09:59:56 瀏覽:849
網站ip訪問量 發布:2024-07-27 09:59:55 瀏覽:286
java字元串字元數組 發布:2024-07-27 09:59:13 瀏覽:593
如何利用無線網路搭建伺服器 發布:2024-07-27 09:59:05 瀏覽:721
it天空解壓密碼 發布:2024-07-27 09:50:39 瀏覽:549
軟體腳本買賣 發布:2024-07-27 09:50:38 瀏覽:917
android對象轉json 發布:2024-07-27 09:50:15 瀏覽:183
安卓平板有什麼可以畫對稱的 發布:2024-07-27 09:36:03 瀏覽:133
羊創意腳本 發布:2024-07-27 09:29:30 瀏覽:895
榮耀v20升級存儲 發布:2024-07-27 09:20:19 瀏覽:486