c语言时钟代码

c语言时钟代码,第1张

#include<graphicsh> / 引入graphich /

#include<mathh> / 引入mathh /

#include<dosh> / 引入dosh /

#define pi 31415926 /定义pi=314159/

#define X(a,b,c) x=acos(bcpi/180-pi/2)+300;

#define Y(a,b,c) y=asin(bcpi/180-pi/2)+240;

#define d(a,b,c) X(a,b,c);Y(a,b,c);line(300,240,x,y) /定义……/

void init() /初始化程序/

{int i,l,x1,x2,y1,y2; /定义……/

setbkcolor(1); /设置颜色/

circle(300,240,200); /作园/

circle(300,240,205);

circle(300,240,5);

for(i=0;i<60;i++) /循环(算时间)/

{if(i%5==0) l=15;

else l=5;

x1=200cos(i6pi/180)+300;

y1=200sin(i6pi/180)+240;

x2=(200-l)cos(i6pi/180)+300;

y2=(200-l)sin(i6pi/180)+240;

line(x1,y1,x2,y2);

}

}

main()

{

int x,y;

int gd=VGA,gm=2;

unsigned char h,m,s; /定义/

struct time t[1];

initgraph(&gd,&gm,"d:\\tc");

init();

setwritemode(1);

gettime(t);

h=t[0]ti_hour;

m=t[0]ti_min;

s=t[0]ti_sec; /定义时分秒/

setcolor(7); /设置颜色/

d(150,h,30);

setcolor(14);

d(170,m,6);

setcolor(4);

d(190,s,6);

while(!kbhit()) /获取键盘相应/

{while(t[0]ti_sec==s)

gettime(t); /C语言中得到时间的函数/

sound(400); /计算时间……/

delay(70);

sound(200);

delay(30);

nosound();

setcolor(4);

d(190,s,6);

s=t[0]ti_sec;

d(190,s,6);

if (t[0]ti_min!=m)

{

setcolor(14);

d(170,m,6);

m=t[0]ti_min;

d(170,m,6);

}

if (t[0]ti_hour!=h)

{ setcolor(7);

d(150,h,30);

h=t[0]ti_hour;

d(150,h,30);

sound(1000);

delay(240);

nosound();

delay(140);

sound(2000);

delay(240);

nosound();

}

}

getch(); /设置空格后退出/

closegraph();

}

具体的。。就是套用用几个函数算时间。。

不要对这种很长的东西害怕,其实大部分都是在画这个钟~

加油哦~

/实验名称:定时中断0

功能:利用定时器,产生秒计数器,使数码管显示时间和年月日,还有跑表。

05-02-01

/

#include <reg51h>

//LCD的口线

sbit E=P3^5;

sbit RW=P3^6;

sbit RS=P3^7;

//8字LED的连接方法

//P07,P06,P05,P04,P03,P02,P01,P00

//G, F A E B H C D

sbit l1=P2^7;

sbit l2=P2^6;

sbit l3=P2^5;

sbit l4=P2^4;

sbit l5=P2^3;

sbit l6=P2^2;

sbit l7=P2^1;

sbit l8=P2^0;

sbit j1=P1^1;

sbit j2=P1^2;

unsigned char code ledcode[]={0x84,0xf5,0x46,0x54,0x35,0x1c,0x0c,0xd5,

0x04,0x14,0x05,0x2c,0x8e,0x64,0x0e,0x0f}; //数码管段码表

unsigned char ms,s,min,hour,t0100ms,t0101ms,t0sm,s0,s1,mi0,mi1,h0,h1,dsm,dsh,dsm0,dsm1,dsh0,dsh1;

unsigned int year,mouch,date,y3,y2,y1,y0,m1,m0,d1,d0,leap,bz,bz1,bz0,pb0,pb1,pb2,pb00,pb10,pb20,pb,cs;

void time0(void);

void delay();

void main(void)

{

E=0; //取消LCD对LED的影响

RW=0;

RS=1;

year=2009, mouch=10, date=18,leap=0;

y3=0, y2=0, y1=0, y0=0;

m1=0, m0=0, d1=0, d0=0;

bz=0,bz1=0,bz0=0;

pb0=0,pb1=0,pb2=0,cs=1;

ms=0,min=30,hour=12,s=50;

s0=0x00,s1=0x00;

mi0=0,mi1=0,h0=0,h1=0;

t0100ms=0x14;

t0101ms=0x14;

t0sm=0x01; //从第一个LED开始显示

TH0=0xee;

TL0=0x00;

TMOD=0x01;

TR0=1;

IE=0x82;

while(1)

{ if((year%4==0&&year%100!=0)||(year%400==0)) //判断是平年还是闰年

{leap=1;}

else

{leap=2;}

do

{

if(j1==0){delay(), bz++;}

if(bz==8){bz=0;}

switch(bz)

{ case 0:{ bz1=0;} break;

case 1:{ bz1=0;if(j2==0){delay(), min++;}} break;

case 2:{ bz1=0;if(j2==0){delay(), hour++;}} break;

case 3:{ bz1=1;if(j2==0){delay(), date++;}} break;

case 4:{ bz1=1;if(j2==0){delay(),mouch++;}} break;

case 5:{ bz1=1;if(j2==0){delay(), year++;}} break;

case 6:{ bz1=2;if(j2==0){delay(), dsm++;}} break;

case 7:{ bz1=2;if(j2==0){delay(), dsh++;}} break;

default:break; }

}

while(bz);

do

{

if(j2==0) {delay(),bz0++;}

if(bz0==5){bz0=0;}

switch(bz0)

{ case 0:{ bz1=0;} break;

case 1:{ bz1=1;if(j1==0){delay(),bz0=0;}} break;

case 2:{ bz1=3,pb=0,pb0=0,pb1=0,pb2=0;} break;

case 3:{ bz1=3;if(j1==0){cs=2,pb00=pb0,pb10=pb1,pb20=pb2;}} break;

case 4:{ bz1=3;if(j1==0){pb=1;}} break;

// case 2:{ bz1=0;if(j2==0){delay(), hour++;}} break;

// case 3:{ bz1=1;if(j2==0){delay(), date++;}} break;

// case 4:{ bz1=1;if(j2==0){delay(),mouch++;}} break;

// case 5:{ bz1=1;if(j2==0){delay(), year++;}} break;

default:break; }

}

while(bz0);

}

}

////////////////////////////////////

void delay()

{

unsigned int i,j,k;

for( i=10; i>0;i--)

for( j=20; j>0;j--)

for(k=100; k>0;k--);

}

///////////////////////////////////////

void time0(void) interrupt 1

{

TH0=0xee,TL0=0x00;

t0100ms--;

if(t0100ms==0x00){ t0100ms=20,ms++;

if(ms==10) { ms=0,s++;

if(s==60) { s=0,min++,m0=6;

if(min==60) { min=0,hour++;

if(hour==24) { hour=0,date++;

//

switch(mouch)

{ case 1:{if(date==32){date=0,mouch++;}}break;

case 2:{if(leap==1&&date==30){date=0,mouch++;}else{if(leap==2&&date==29){date=0,mouch++;}}}break;

case 3:{if(date==32){date=0,mouch++;}}break;

case 4:{if(date==31){date=0,mouch++;}}break;

case 5:{if(date==32){date=0,mouch++;}}break;

case 6:{if(date==31){date=0,mouch++;}}break;

case 7:{if(date==32){date=0,mouch++;}}break;

case 8:{if(date==32){date=0,mouch++;}}break;

case 9:{if(date==31){date=0,mouch++;}}break;

case 10:{if(date==32){date=0,mouch++;}}break;

case 11:{if(date==31){date=0,mouch++;}}break;

case 12:{if(date==32){date=0,mouch++;}}break;

case 13:{mouch=1,year++;}break;

default: break; }

}

}

}

}

}

if(min==60) {min=0;}

if(hour==24){hour=0;}

if((mouch==1||mouch==3||mouch==5||mouch==7||mouch==8||mouch==10||mouch==12)&&date==32){date=0;}

if((mouch==4||mouch==6||mouch==9||mouch==11)&&date==31){date=0;}

if(mouch==2&&leap==1&&date==30){date=0;}

if(mouch==2&&leap==2&&date==29){date=0;}

if(mouch==13){mouch=1;}

s0=s%10,s1=s/10%10;

mi0=min%10,mi1=min/10%10;

h0=hour%10,h1=hour/10%10;

d0=date%10,d1=date/10%10;

m0=mouch%10,m1=mouch/10%10;

y0=year%10,y1=year/10%10,y2=year/100%10,y3=year/1000;

dsm0=dsm%10,dsm1=dsm/10%10;

dsh0=dsh%10,dsh1=dsh/10%10;

if(dsm==60){dsm=0;}

if(dsh==24){dsh=0;}

//

if(bz0==4)

{}

else

if(bz0==3)

{ TH0=0xee,TL0=0x00;

t0101ms--;

if(t0101ms==0x00){ t0101ms=20,pb0++;

if( pb0==10) {pb0=0,pb1++;

if(pb1==10) {pb1=0,pb2++;

if(pb2==10) {pb2=0;}

}

}

}

}

/////////////////////////////////////////////////////////////////////////

t0sm++;

if(t0sm==9){t0sm=1; }

P0=0xff;

if(bz1==0)

{switch(t0sm)

{ case 1:{P2=0xff,l1=0;P0=ledcode[ms];} break;

case 2:{P2=0xff,l2=0;P0=ledcode[s0]&0xfb;} break;

case 3:{P2=0xff,l3=0;P0=ledcode[s1];} break;

case 4:{P2=0xff,l4=0;P0=ledcode[mi0]&0xfb;}break;

case 5:{if(bz==1){P2=0xff,l5=0;P0=ledcode[mi1]&0xfb;}else{P2=0xff,l5=0;P0=ledcode[mi1];}}break;

case 6:{P2=0xff,l6=0;P0=ledcode[h0]&0xfb;} break;

case 7:{if(bz==2){P2=0xff,l7=0;P0=ledcode[h1]&0xfb;}else{P2=0xff,l7=0;P0=ledcode[h1];}}break;

case 8:{P2=0xff,l8=0;P0=ledcode[bz]&0xfb;} break;

default:break; }

}

if(bz1==1)

{switch(t0sm)

{ case 1:{if(bz==3){P2=0xff,l1=0;P0=ledcode[d0]&0xfb;}else{P2=0xff,l1=0;P0=ledcode[d0];}} break;

case 2:{if(bz==3){P2=0xff,l2=0;P0=ledcode[d1]&0xfb;}else{P2=0xff,l2=0;P0=ledcode[d1];}} break;

case 3:{P2=0xff,l3=0;P0=ledcode[m0]&0xfb;} break;

case 4:{if(bz==4){P2=0xff,l4=0;P0=ledcode[m1]&0xfb;}else{P2=0xff,l4=0;P0=ledcode[m1];}} break;

case 5:{P2=0xff,l5=0;P0=ledcode[y0]&0xfb;} break;

case 6:{if(bz==5){P2=0xff,l6=0;P0=ledcode[y1]&0xfb;}else{P2=0xff,l6=0;P0=ledcode[y1];}} break;

case 7:{if(bz==5){P2=0xff,l7=0;P0=ledcode[y2]&0xfb;}else{P2=0xff,l7=0;P0=ledcode[y2];}} break;

case 8:{if(bz==5){P2=0xff,l8=0;P0=ledcode[y3]&0xfb;}else{P2=0xff,l8=0;P0=ledcode[y3];}} break;

default:break; }

}

if(bz1==2)

{switch(t0sm)

{ case 1:{P2=0xff;} break;

case 2:{P2=0xff;} break;

case 3:{P2=0xff;} break;

case 4:{if(bz!=6){P2=0xff,l4=0;P0=ledcode[dsm0];}else{P2=0xff,l4=0;P0=ledcode[dsm0]&0xfb;}}break;

case 5:{if(bz!=6){P2=0xff,l5=0;P0=ledcode[dsm1];}else{P2=0xff,l5=0;P0=ledcode[dsm1]&0xfb;}}break;

case 6:{P2=0xff,l6=0;P0=ledcode[dsh0]&0xfb;} break;

case 7:{if(bz!=7){P2=0xff,l7=0;P0=ledcode[dsh1];}else{P2=0xff,l7=0;P0=ledcode[dsh1]&0xfb;}}break;

case 8:{P2=0xff;} break;

default:break; }

}

if(bz1==3)

{switch(t0sm)

{ case 1:{if(pb==1){P2=0xff,l1=0;P0=ledcode[pb00];}else{P2=0xff,l1=0;P0=ledcode[pb0];}}break;

case 2:{if(pb==1){P2=0xff,l2=0;P0=ledcode[pb10]&0xfb;}else{P2=0xff,l2=0;P0=ledcode[pb1]&0xfb;}} break;

case 3:{if(pb==1){P2=0xff,l3=0;P0=ledcode[pb2];}else{P2=0xff,l3=0;P0=ledcode[pb2];}} break;

case 4:{P2=0xff;}break;

case 5:{P2=0xff,l5=0;P0=ledcode[cs];}break;

case 6:{P2=0xff;}break;

case 7:{P2=0xff;}break;

case 8:{P2=0xff;}break;

default:break; }

}

}

///////////////////////////////////////////////////////////////

// Clockcpp文件

#include <windowsh>

#include "resourceh"

#include <mathh>LRESULT __stdcall WndProc(HWND, UINT, WPARAM, LPARAM);

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

char szWindowClass[] = "Clock"; // 注册窗口类

WNDCLASSEX wcex; wcexcbSize = sizeof(WNDCLASSEX);

wcexstyle = CS_HREDRAW | CS_VREDRAW;

wcexlpfnWndProc = (WNDPROC)WndProc;

wcexcbClsExtra = 0;

wcexcbWndExtra = 0;

wcexhInstance = hInstance;

wcexhIcon = ::LoadIcon(hInstance, (LPCTSTR)IDI_MAIN);

wcexhCursor = LoadCursor(NULL, IDC_ARROW);

wcexhbrBackground = (HBRUSH)(COLOR_3DFACE + 1); // 此域可以是一个系统定义的颜色值

wcexlpszMenuName = NULL;

wcexlpszClassName = szWindowClass;

wcexhIconSm = NULL; ::RegisterClassEx(&wcex);

// 创建并显示主窗口

HWND hWnd = ::CreateWindowEx(NULL, szWindowClass, "时钟",

WS_POPUP|WS_SYSMENU|WS_SIZEBOX, 100, 100, 150, 150, NULL, NULL, hInstance, NULL);

::ShowWindow(hWnd, nShowCmd);

::UpdateWindow(hWnd); // 进入消息循环

MSG msg;

while(::GetMessage(&msg, NULL, 0, 0))

{

::TranslateMessage(&msg);

::DispatchMessage(&msg);

} return 1;

} //////////////////////////////////////////////////////////////////////////////////////

// 消息处理代码#define IDT_CLOCK 1

const int IDM_HELP = 100;

const int IDM_TOPMOST = 101;// 实现函数

void SetIsotropic(HDC hdc, int cxClient, int cyClient);

void DrawClockFace(HDC hdc);

void DrawHand(HDC hdc, int nLength, int nWidth, int nDegrees, COLORREF clrColor);// 上一次Windows通知时的时间

static int s_nPreHour; // 小时

static int s_nPreMinute; // 分钟

static int s_nPreSecond; // 秒

// 窗口客户区的大小

static int s_cxClient;

static int s_cyClient;

// 是否位于最顶层

static BOOL s_bTopMost;LRESULT __stdcall WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)

{

switch(nMsg)

{

case WM_CREATE:

{

// 向系统菜单中添加自定义的项

HMENU hSysMenu;

hSysMenu = ::GetSystemMenu(hWnd, FALSE);

::AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);

::AppendMenu(hSysMenu, MF_STRING, IDM_TOPMOST, "总在最前");

::AppendMenu(hSysMenu, MF_STRING, IDM_HELP, "帮助");

SetWindowLong( hWnd, GWL_EXSTYLE, GetWindowLong( hWnd, GWL_EXSTYLE ) ^ 0x80000 ); HINSTANCE hInst = LoadLibrary( "User32dll ");

//_ASSERT( hInst ); typedef BOOL ( WINAPI SetWindowLayeredFunc )( HWND, COLORREF, BYTE, DWORD );

SetWindowLayeredFunc Func = NULL; Func = ( SetWindowLayeredFunc ) GetProcAddress( hInst, "SetLayeredWindowAttributes" );

//_ASSERT( Func ); Func( hWnd, 0, 128, 2 );

FreeLibrary( hInst ); // 设置时间

SYSTEMTIME time;

::GetLocalTime(&time);

s_nPreHour = timewHour%12;

s_nPreMinute = timewMinute;

s_nPreSecond = timewSecond;

// 创建定时器

::SetTimer(hWnd, IDT_CLOCK, 1000, NULL);

return 0;

}

case WM_SIZE:

{

s_cxClient = LOWORD(lParam);

s_cyClient = HIWORD(lParam);

return 0;

}

case WM_PAINT:

{

PAINTSTRUCT ps;

HDC hdc = ::BeginPaint(hWnd, &ps);

// 设置坐标系

SetIsotropic(hdc, s_cxClient, s_cyClient);

// 绘制时钟外观

DrawClockFace(hdc);

// 绘制指针

// 经过1个小时时针走30度(360/12),经过1分钟时针走05度(30/60)

DrawHand(hdc, 200, 8, s_nPreHour30 + s_nPreMinute/2, RGB(255, 0, 0));

// 经过1分钟分针走6度(360/60)

DrawHand(hdc, 400, 6, s_nPreMinute6, RGB(0, 255, 0));

// 经过1秒钟秒针走6度(360/60)

DrawHand(hdc, 400, 1, s_nPreSecond6, RGB(0, 0, 255));

::EndPaint(hWnd, &ps);

return 0;

}

case WM_TIMER:

{

// 如果窗口处于最小化状态就什么也不做

if(::IsIconic(hWnd)) // IsIconic函数用来判断窗口是否处于最小化状态

return 0;

// 取得系统时间

SYSTEMTIME time;

::GetLocalTime(&time);

// 建立坐标系

HDC hdc = ::GetDC(hWnd);

SetIsotropic(hdc, s_cxClient, s_cyClient);

// 以COLOR_3DFACE为背景色就可以擦除指针了(因为窗口的背景色也是COLOR_3DFACE)

COLORREF crfColor = ::GetSysColor(COLOR_3DFACE);

// 如果分钟改变的话就擦除时针和分针

if(timewMinute != s_nPreMinute)

{

// 擦除时针和分针

DrawHand(hdc, 200, 8, s_nPreHour30 + s_nPreMinute/2, crfColor);

DrawHand(hdc, 400, 6, s_nPreMinute6, crfColor);

s_nPreHour = timewHour;

s_nPreMinute = timewMinute;

}

// 如果秒改变的话就擦除秒针,然后重画所有指针

if(timewSecond != s_nPreSecond)

{

// 擦除秒针

DrawHand(hdc, 400, 1, s_nPreSecond6, crfColor);

// 重画所有指针

DrawHand(hdc, 400, 1, timewSecond6, RGB(255, 0, 0));

DrawHand(hdc, 200, 8, timewHour30 + timewMinute/2, RGB(0, 255, 0));

DrawHand(hdc, 400, 6, timewMinute6, RGB(0, 0, 255));

s_nPreSecond = timewSecond;

}

return 0;

}

case WM_CONTEXTMENU:

POINT pt; ptx = LOWORD(lParam); pty = HIWORD(lParam);

{

// 取得系统菜单的句柄

HMENU hSysMenu = ::GetSystemMenu(hWnd, FALSE);

// d出系统菜单

int nID = ::TrackPopupMenu(hSysMenu,TPM_LEFTALIGN|TPM_RETURNCMD,

ptx, pty, 0, hWnd, NULL);

if(nID > 0)

::SendMessage(hWnd, WM_SYSCOMMAND, nID, 0);

return 0;

}

case WM_CLOSE:

{

::KillTimer(hWnd, IDT_CLOCK);

::DestroyWindow(hWnd);

return 0;

}

case WM_DESTROY:

{

::PostQuitMessage(0);

return 0;

}

case WM_NCHITTEST:

{

UINT nHitTest;

nHitTest = ::DefWindowProc(hWnd, WM_NCHITTEST, wParam, lParam);

if (nHitTest == HTCLIENT &&

::GetAsyncKeyState(MK_LBUTTON) < 0) // 如果鼠标左键按下,GetAsyncKeyState函数的返回值小于0

nHitTest = HTCAPTION;

return nHitTest;

}

case WM_SYSCOMMAND:

int nID = wParam;

{

if(nID == IDM_HELP)

{

::MessageBox(hWnd, "一个时钟的例子", "时钟", 0);

}

else if(nID == IDM_TOPMOST)

{

HMENU hSysMenu = ::GetSystemMenu(hWnd, FALSE);

if(s_bTopMost)

{

::CheckMenuItem(hSysMenu, IDM_TOPMOST, MF_UNCHECKED);

::SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0,

SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE);

s_bTopMost = FALSE;

}

else

{

::CheckMenuItem(hSysMenu, IDM_TOPMOST, MF_CHECKED);

::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0,

SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE);

s_bTopMost = TRUE;

}

}

return ::DefWindowProc(hWnd, WM_SYSCOMMAND, nID, 0);

}

} return ::DefWindowProc(hWnd, nMsg, wParam, lParam);

}

void SetIsotropic(HDC hdc, int cx, int cy)

{

::SetMapMode(hdc, MM_ISOTROPIC);

::SetWindowExtEx(hdc, 1000, 1000, NULL);

::SetViewportExtEx(hdc, cx, -cy, NULL);

::SetViewportOrgEx(hdc, cx/2, cy/2, NULL);

}// 绘制时钟的外观

void DrawClockFace(HDC hdc)

{

const int SQUARESIZE = 20;

static POINT pt[] =

{

0, 450, // 12点

225, 390, // 1点

390, 225, // 2点

450, 0, // 3点

390, -225, // 下面的坐标是上面的点的对称点(以X轴、Y轴或原点对称)

225, -390,

0, -450,

-225, -390,

-390, -225,

-450, 0,

-390, 225,

-225, 390

}; // 选择一个黑色的画刷

::SelectObject(hdc, ::GetStockObject(BLACK_BRUSH)); // 画12个圆

for(int i=0; i<12; i++)

{

::Ellipse(hdc, pt[i]x - SQUARESIZE, pt[i]y + SQUARESIZE,

pt[i]x + SQUARESIZE, pt[i]y - SQUARESIZE);

}

} // 指针的长度、宽度、相对于0点偏移的角度、颜色分别由参数nLength、nWidth、nDegrees、clrColor指定

void DrawHand(HDC hdc, int nLength, int nWidth, int nDegrees, COLORREF clrColor)

{

// 将角度nDegrees转化成弧度 231415926/360 == 00174533

double nRadians = (double)nDegrees00174533; // 计算坐标

POINT pt[2];

pt[0]x = (int)(nLengthsin(nRadians));

pt[0]y = (int)(nLengthcos(nRadians));

pt[1]x = -pt[0]x/5;

pt[1]y = -pt[0]y/5; // 创建画笔,并选如DC结构中

HPEN hPen = ::CreatePen(PS_SOLID, nWidth, clrColor);

HPEN hOldPen = (HPEN)::SelectObject(hdc, hPen); // 画线

::MoveToEx(hdc, pt[0]x, pt[0]y, NULL);

::LineTo(hdc, pt[1]x, pt[1]y); ::SelectObject(hdc, hOldPen);

::DeleteObject(hPen);

}

以上就是关于c语言时钟代码全部的内容,包括:c语言时钟代码、用C语言编数码管时钟程序、怎样做一个C语言制成的数字时钟的界面等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10058008.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存