当前位置:首页 » 编程软件 » 电脑编程时钟

电脑编程时钟

发布时间: 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()计算两者的差,即可得到耗费时间。

热点内容
linux下ntp服务器搭建 发布:2024-09-08 08:26:46 浏览:742
db2新建数据库 发布:2024-09-08 08:10:19 浏览:171
频率计源码 发布:2024-09-08 07:40:26 浏览:778
奥迪a6哪个配置带后排加热 发布:2024-09-08 07:06:32 浏览:101
linux修改apache端口 发布:2024-09-08 07:05:49 浏览:209
有多少个不同的密码子 发布:2024-09-08 07:00:46 浏览:566
linux搭建mysql服务器配置 发布:2024-09-08 06:50:02 浏览:995
加上www不能访问 发布:2024-09-08 06:39:52 浏览:811
银行支付密码器怎么用 发布:2024-09-08 06:39:52 浏览:513
苹果手机清理浏览器缓存怎么清理缓存 发布:2024-09-08 06:31:32 浏览:554