本文目錄一覽:
c語言怎麼樣編寫一個時鐘程序
c語言時鐘程序代碼如下:
#includewindows.h
#includemath.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;iiNum;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;iAngle360;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;i3;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語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。儘管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標準規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
C語言是由UNIX的研製者丹尼斯·里奇(Dennis Ritchie)於1970年 由 肯·湯普遜(Ken Thompson)所研製出的B語言的基礎上發展和完善起來的。目前,C語言編譯器普遍存在於各種不同的操作系統中,例如UNIX、MS-DOS、Microsoft Windows及Linux等。C語言的設計影響了許多後來的編程語言,例如C++、Objective-C、Java、C#等。
C語言如何調用系統時間
方法一,#includetime.h
int main()
{
time_t timep;
struct tm *p;
time (timep);
p=gmtime(timep);
printf(“%d\n”,p-tm_sec); /*獲取當前秒*/
printf(“%d\n”,p-tm_min); /*獲取當前分*/
printf(“%d\n”,8+p-tm_hour);/*獲取當前時,這裡獲取西方的時間,剛好相差八個小時*/
printf(“%d\n”,p-tm_mday);/*獲取當前月份日數,範圍是1-31*/
printf(“%d\n”,1+p-tm_mon);/*獲取當前月份,範圍是0-11,所以要加1*/
printf(“%d\n”,1900+p-tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
printf(“%d\n”,p-tm_yday); /*從今年1月1日算起至今的天數,範圍為0-365*/
}
方法二.#include stdio.h
#include time.h
int main ()
{
time_t t
struct tm * lt; time (t);//獲取Unix時間戳。
lt = localtime (t);//轉為時間結構。
printf ( “%d/%d/%d %d:%d:%d\n”,lt-tm_year+1900, lt-tm_mon, lt-tm_mday,
lt-tm_hour, lt-tm_min, lt-tm_sec);//輸出結果
return 0;}
擴展資料
1、CTimeSpan類
如果想計算兩段時間的差值,可以使用CTimeSpan類,具體使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //計算當前系統時間與時間t1的間隔
int iDay=span.GetDays(); //獲取這段時間間隔共有多少天
int iHour=span.GetTotalHours(); //獲取總共有多少小時
int iMin=span.GetTotalMinutes();//獲取總共有多少分鐘
int iSec=span.GetTotalSeconds();//獲取總共有多少秒
2、timeb()函數
_timeb定義在SYS\TIMEB.H,有四個fields
dstflag
millitm
time
timezone
void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
_ftime( timebuffer );
參考資料來源:百度百科:time函數
c語言 更改系統時間(settime)程序
函數名: settime
功 能: 設置系統時間
用 法: void settime(struct time *timep);
程序例:
#include stdio.h
#include dos.h
int main(void)
{
struct time t;
gettime(t);
printf(“The current minute is: %d\n”, t.ti_min);
printf(“The current hour is: %d\n”, t.ti_hour);
printf(“The current hundredth of a second is: %d\n”, t.ti_hund);
printf(“The current second is: %d\n”, t.ti_sec);
/* Add one to the minutes struct element and then call settime */
t.ti_min++; //在這個地方就可以控制你要改的時間的分,以此同!
settime(t);
return 0;
}
C語言編寫由鍵盤輸入設定計算機時鐘當前時間的程序
我同意樓上說的 計算機時鐘和當前時間不是一個概念
但是我猜你應該是想修改當前時間,代碼如下
#includestdio.h
#includestdlib.h
#includestring.h
#define time_len 20
int main ()
{
char set_time[time_len], cmd[100];
printf(“請輸入想設置的時間(格式XX:xx):”);
scanf(“%s”, set_time); //輸入將時間修改為XX:XX,其實可以不設置分,直接XX,則默認XX:00
strcpy(cmd, “time “);
strcat(cmd, set_time);
system(cmd);
}
C語言中調用系統時間
#include stdio.h
#include time.h
int main()
{
time_t rawtime;
struct tm * timeinfo;
time ( rawtime );
timeinfo = localtime ( rawtime );
printf ( “當前系統時間: %s”, asctime (timeinfo) );
return 0;
}
說明:
time_t // 時間類型(time.h 定義)
struct tm { // 時間結構,time.h 定義如下:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}
time ( rawtime ); // 獲取時間,以秒計,從1970年1月一日起算,存於rawtime
localtime ( rawtime ); //轉為當地時間,tm 時間結構
asctime() // 轉為標準ASCII時間格式:
//就是直接列印tm,tm_year 從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/200262.html