當前位置:首頁 » 編程語言 » c語言鍾表

c語言鍾表

發布時間: 2023-04-24 02:39:57

c語言編寫鍾表的問題

#include<graphics.h>
#include<math.h>
#include<dos.h>

#define PI 3.1415926
#define x0 320 /*定義鍾表中心坐標*/
#define y0 240

void DrawClock(int x,int y,int color) /*畫表盤*/
{ int r=150; /*表盤的半徑*/
float th;
setcolor(color);
circle(x,y,r);
circle(x,y,2);
}

void DrawHand(int x,int y,float th,int l,int color)
{
int x1,y1;
x1=x l*sin(th);
y1=y-l*cos(th);
setcolor(color);
line(x,y,x1,y1);
}

void main()
{int gdriver=DETECT,gmode;
struct time curtime;
float th_hour,th_min,th_sec;
initgraph(&gdriver,&gmode,"");

setbkcolor(0);

while(! kbhit())
{
DrawClock(x0,y0,14);
gettime(&curtime); /*得到當前系統時間*/

gotoxy(35,20); /*定位輸出位置*/
if((float)curtime.ti_hour<=12) /*午前的處理*/
{printf("AM ");
if((float)curtime.ti_hour<10) printf("0"); /*十點之前在小時數前加零*/
printf("%.0f:",(float)curtime.ti_hour);
}
else /*午後的處理*/
{printf("PM ");
if((float)curtime.ti_hour-12<10) printf("0");
printf("%.0f:",(float)curtime.ti_hour-12);
}

if((float)curtime.ti_min<10) printf("0");
printf("%.0f:",(float)curtime.ti_min);
if((float)curtime.ti_sec<10) printf("0");
printf("%.0f",(float)curtime.ti_sec);

/*以下三行計算表針轉動角度,以豎直向上為起點,順時針為正*/

th_sec=(float)curtime.ti_sec*0.1047197551; /*2π/60=0.1047197551*/
th_min=(float)curtime.ti_min*0.1047197551 th_sec/60.0;
th_hour=(float)curtime.ti_hour*0.523598775 th_min/12.0; /* 2π/12=0.5235987755 */
DrawHand(x0,y0,th_hour,70,2); /*畫時針*/
DrawHand(x0,y0,th_min,110,3); /*分針*/
DrawHand(x0,y0,th_sec,140,12); /*秒針*/

sleep(1); /*延時一秒後刷新*/
cleardevice();
}

closegraph();
}

能正常運行,我測試過
來自網路轉載

⑵ 用C語言做數字時鍾每走一秒響一次,求大神告訴源代碼

「響一次」需要牽涉到圖形編程中的音樂播放問題,需要自己下載圖形編程相關庫文件,具體實現請自己在TODO里添加播放音樂的代碼

數字時鍾的實現很簡單,運用time.h相關函數即可

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
time_toldt=-1;
structtm*p;
boolPrintdate()
{
time_tt=time(NULL);
if(t!=oldt)
{
oldt=t;
p=localtime(&t);
system("cls");
printf("%d/%d/%d周",1900+p->tm_year,1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
switch(p->tm_wday)
{
case1:printf("一");break;
case2:printf("二");break;
case3:printf("三");break;
case4:printf("四");break;
case5:printf("五");break;
case6:printf("六");break;
case7:printf("日");break;
}
printf("%d:%02d:%02d",p->tm_hour,p->tm_min,p->tm_sec);
return1;
}
return0;
}
main()
{
while(1)
if(Printdate())
{
/*************TODO*************/
/**Forexample:printf("a");**/
}
return0;
}

⑶ c語言 時鍾模擬

#include<math.h>
#include<dos.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#define PI 3.141592653589793
int h,m,s,i,l,mon,y,d;
struct time t;
struct date data;
draw()
{
gettime(&t); //取得時間信息到t
s=t.ti_sec; //秒
h=t.ti_hour; //時
m=t.ti_min; //分
getdate(&data); //取得日期信息到data
y=data.da_year; //年
mon=data.da_mon; //月
d=data.da_day; //日

//畫出鍾的外圓(即是輪廓)
setcolor(11);
circle(300,200,152);
setcolor(3);
circle(300,200,157);

//畫出60個分鍾刻度
for(i=0;i<60;i+=1)
{
if(i%5==0) l=140;
else l=145;
line(300+150*sin(i*PI/30),200-150*cos(i*PI/30),
300+l*sin(i*PI/30),200-l*cos(i*PI/30));
}

//畫秒針
setcolor(19);
line(300,200,300+140*sin(s*PI/30),200-140*cos(s*PI/30));
//畫分針
setcolor(3);
line(300,200,300+110*sin(m*PI/30),200-110*cos(m*PI/30));
//畫時針
setcolor(11);
line(300,200,300+90*sin(((float)h+(float)m/60)*PI/6),200-90*cos(((float)h+(float)m/60)*PI/6));

//標注鍾盤上的"3"、"6"、"9"、"12"
settextstyle(3,0,2);
outtextxy(430,190,"3");
outtextxy(295,320,"6");
outtextxy(160,190,"9");
outtextxy(293,60,"12");
}
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,""); //初始化屏幕模式
setbkcolor(8);
while(!kbhit()) //若有鍵盤輸入則跳出(結束程序)
{
draw(); //繪制鍾
settextstyle(3,0,5);
setcolor(9);
outtextxy(60,170,"my clock");
gotoxy(35,17);
//列印出數字形式的時間(hh:mm:ss)
if(h<10) printf("0");printf("%d:",h);
if(m<10) printf("0");printf("%d:",m);
if(s<10) printf("0");printf("%d",s);
gotoxy(33,18);
printf("%d:",y);
//列印出日期(mm:dd)
if(mon<10) printf("0");printf("%d:",mon);
if(d<10) printf("0");printf("%d",d);
sound(200); //讓喇叭以200HZ叫一聲
delay(70); //延時0.07秒,即是聲音延續0.07秒
nosound(); //停止聲音
sleep(1); //停止一秒
cleardevice(); //清屏
}
}

⑷ 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<graphics.h>
#include<conio.h>
#include<math.h>

voidDraw(inthour,intminute,intsecond)
{
doublea_hour,a_min,a_sec;//時、分、秒針的弧度值
intx_hour,y_hour,x_min,y_min,x_sec,y_sec;//時、分、秒針的末端位置
intx_hour1,y_hour1,x_min1,y_min1,x_sec1,y_sec1;
//計算時、分、秒針的弧度值
a_sec=second*2*PI/60;
a_min=minute*2*PI/60;
a_hour=hour*2*PI/12+a_min/12;;

//計算時、分、秒針的首末端位置
x_sec=320+(int)(120*sin(a_sec));
y_sec=240-(int)(120*cos(a_sec));
x_min=320+(int)(100*sin(a_min));
y_min=240-(int)(100*cos(a_min));
x_hour=320+(int)(70*sin(a_hour));
y_hour=240-(int)(70*cos(a_hour));
x_sec1=320-(int)(15*sin(a_sec));
y_sec1=240+(int)(15*cos(a_sec));
x_min1=320-(int)(10*sin(a_min));
y_min1=240+(int)(10*cos(a_min));
x_hour1=320-(int)(5*sin(a_hour));
y_hour1=240+(int)(5*cos(a_hour));//畫時針
setlinestyle(PS_SOLID,NULL,7);
setcolor(WHITE);
line(x_hour1,y_hour1,x_hour,y_hour);

//畫分針
setlinestyle(PS_SOLID,NULL,4);
setcolor(LIGHTGRAY);
line(x_min1,y_min1,x_min,y_min);

//畫秒針
setlinestyle(PS_SOLID,NULL,2);
setcolor(RED);
line(x_sec1,y_sec1,x_sec,y_sec);
}

voidmain()
{
initgraph(640,480);//初始化640x480的繪圖窗口

//繪制一個簡單的表盤
circle(320,240,2);
circle(320,240,60);
circle(320,240,160);
outtextxy(296,330,"竹斌");
intx,y;
for(inti=0;i<12;i++)
{
x=320+(int)(140*sin(30*i*2*PI/360));
y=240-(int)(140*cos(30*i*2*PI/360));
switch(i)
{
case0:outtextxy(x-5,y-5,"12");break;
case1:outtextxy(x-5,y-5,"1");break;
case2:outtextxy(x-5,y-5,"2");break;
case3:outtextxy(x-5,y-5,"3");break;
case4:outtextxy(x-5,y-5,"4");break;
case5:outtextxy(x-5,y-5,"5");break;
case6:outtextxy(x-5,y-5,"6");break;
case7:outtextxy(x-5,y-5,"7");break;
case8:outtextxy(x-5,y-5,"8");break;
case9:outtextxy(x-5,y-5,"9");break;
case10:outtextxy(x-5,y-5,"10");break;
case11:outtextxy(x-5,y-5,"11");break;
}
}

//設置XOR繪圖模式
setwritemode(R2_XORPEN);//設置XOR繪圖模式


//畫刻度
inta,b,a1,b1,n=0;
for(n=0;n<60;n++)
{
a=320+(int)(160*sin(n*2*PI/60));
b=240-(int)(160*cos(n*2*PI/60));
a1=320+(int)(150*sin(n*2*PI/60));
b1=240-(int)(150*cos(n*2*PI/60));
if(n%5==0)
setlinestyle(PS_SOLID,NULL,5);
else
setlinestyle(PS_SOLID,NULL,1);
line(a1,b1,a,b);
}


//繪製表針
SYSTEMTIMEti;//定義變數保存當前時間
while(!kbhit())//按任意鍵退出鍾表程序
{
GetLocalTime(&ti);//獲取當前時間
Draw(ti.wHour,ti.wMinute,ti.wSecond);//畫表針
Sleep(1000);//延時1秒
Draw(ti.wHour,ti.wMinute,ti.wSecond);//擦表針(擦表針和畫表針的過程是一樣的)
}

closegraph();//關閉繪圖窗口
}

⑹ c語言時鍾代碼

#include<graphics.h> /* 引入graphic.h */
#include<math.h> /* 引入math.h */
#include<dos.h> /* 引入dos.h */
#define pi 3.1415926 /*定義pi=3.14159*/
#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300;
#define Y(a,b,c) y=a*sin(b*c*pi/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=200*cos(i*6*pi/180)+300;
y1=200*sin(i*6*pi/180)+240;
x2=(200-l)*cos(i*6*pi/180)+300;
y2=(200-l)*sin(i*6*pi/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();
}

具體的。。就是套用用幾個函數算時間。。
不要對這種很長的東西害怕,其實大部分都是在畫這個鍾~
加油哦~

熱點內容
jrtplib編譯 發布:2024-11-01 18:06:01 瀏覽:226
java代碼中if 發布:2024-11-01 18:02:40 瀏覽:377
android定時刷新 發布:2024-11-01 17:59:43 瀏覽:999
炎黃解說我的世界伺服器生存 發布:2024-11-01 17:59:42 瀏覽:542
如何清楚網頁緩存 發布:2024-11-01 17:53:58 瀏覽:552
linux文件許可權不夠 發布:2024-11-01 17:53:19 瀏覽:917
c語言中10是什麼意思 發布:2024-11-01 17:45:08 瀏覽:892
裝棉衣壓縮袋 發布:2024-11-01 17:37:18 瀏覽:297
android40ble藍牙 發布:2024-11-01 17:36:58 瀏覽:712
資料庫表對比 發布:2024-11-01 17:18:42 瀏覽:985