⑴ 圖形時鍾用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>
#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語言時鍾設計
#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(); }
⑷ 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> /* 引入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();
}
具體的。。就是套用用幾個函數算時間。。
不要對這種很長的東西害怕,其實大部分都是在畫這個鍾~
加油哦~
⑹ 時鍾的C語言程序代碼,要沒錯誤,能運行的。最好重要的語句後面要有注釋,急求~~高手求解~~~
給你一個指示吧 1.繪圖函數 2.時間從主板上面取 程序如下(運行環境是在TC下面的)/***********簡單的時鍾程序,界面不是很美觀,您可以根據自己的愛好加以修改,如給表盤加上刻度,將指針改為其它外形等*/
#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;<br> struct time curtime;<br> float th_hour,th_min,th_sec;<br> initgraph(&gdriver,&gmode,"");<br><br> setbkcolor(0);<br><br> while(! kbhit())<br> {<br> DrawClock(x0,y0,14);<br> gettime(&curtime); /*得到當前系統時間*/<br><br> gotoxy(35,20); /*定位輸出位置*/<br> if((float)curtime.ti_hour<=12) /*午前的處理*/<br> {printf("AM ");<br> if((float)curtime.ti_hour<10) printf("0"); /*十點之前在小時數前加零*/<br> printf("%.0f:",(float)curtime.ti_hour);<br> }
else /*午後的處理*/
{printf("PM ");<br> if((float)curtime.ti_hour-12<10) printf("0");<br> printf("%.0f:",(float)curtime.ti_hour-12);<br> }
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語言模擬時鍾中的表針轉動是怎麼寫的
/*開發環境:turbo c 2.0模擬時鍾轉動程序代碼*/ #include"graphics.h" #include"math.h" #include"dos.h" #define pi 3.1415926 #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*sin(i*6*pi/180)+300; y1=200*cos(i*6*pi/180)+240; x2=(200-l)*sin(i*6*pi/180)+300; y2=(200-l)*cos(i*6*pi/180)+240; line(x1,y1,x2,y2); } } main() { int x,y,i,k=1; int gdriver=9,gmode=2; unsigned char h,m,s; int o,p,q; float n; struct time t[1]; struct date d[1]; initgraph(&gdriver,&gmode,"c:\\tc"); initgraph(&gdriver,&gmode,"c:\\tc"); for(i=0;i<=6;i++) { settextstyle(TRIPLEX_FONT,HORIZ_DIR,i); /*控制輸出字元的字體,方向,大小*/ cleardevice(); settextjustify(1,1); /*在指定坐標上輸出字元串*/ outtextxy(300,80,"12") ; outtextxy(300,390,"6"); outtextxy(140,230,"9"); outtextxy(460,230,"3"); outtextxy(380,100,"1"); outtextxy(220,100,"11"); outtextxy(430,160,"2"); outtextxy(430,310,"4"); outtextxy(380,370,"5"); outtextxy(220,370,"7"); outtextxy(160,160,"10"); outtextxy(160,310,"8"); } init(); setwritemode(1); /*設置畫線的輸出模式*/ if(k!=0) { getdate(d); /*獲得系統日期函數*/ o=d[0].da_year; p=d[0].da_mon; q=d[0].da_day; gettime(t); /*獲得系統時間函數*/ h=t[0].ti_hour; m=t[0].ti_min; s=t[0].ti_sec; } setcolor(7); /*設置時針顏色*/ n=(float)h+(float)m/60; d(150,n,30); /*畫出時針*/ setcolor(14); /*設置分針顏色*/ d(170,m,6); /*畫出分針*/ setcolor(4); /*設置秒針顏色*/ d(190,s,6); /*畫出秒針*/ while(!kbhit()) /*控製程序按下任意鍵退出*/ { while(t[0].ti_sec==s) gettime(t); gotoxy(44,18); /*使游標移動到指定坐標*/ printf("\b\b\b\b\b\b\b\b\b"); /*退格,使表示時間的字元串不斷變化*/ sound(400); /*按給定的頻率打開PC揚聲器*/ delay(70); /*中斷程序的執行,時間為70毫秒*/ sound(200); delay(30); nosound(); /*按給定的頻率關閉PC揚聲器*/ 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(); } if(s<10) /*用字元的形式輸出時間*/ { if(m<10) printf("%u:0%u:0%u",h,m,s); else printf("%u:%u:0%u",h,m,s); } else { if(m<10) printf("%u:0%u:%u",h,m,s); else printf("%u:%u:%u",h,m,s); } gotoxy(34,19); /*在指定坐標上輸出日期*/ printf("%d年%d月%d日",o,p,q); printf("\b\b\b\b\b\b\b\b\b"); } getch(); closegraph(); }
⑻ 誰能幫我用c語言編寫桌面鍾表啊!
#include<math.h>
#include<dos.h>
#include<graphics.h>
#define
CENTERX
320
/*表盤中心位置*/
#define
CENTERY
175
#define
CLICK
100
/*喀嗒聲頻率*/
#define
CLICKDELAY
30
/*喀嗒聲延時*/
#define
HEBEEP
10000
/*高聲頻率*/
#define
LOWBEEP
500
/*低聲頻率*/
#define
BEEPDELAY
200
/*報時聲延時*/
/*表盤刻度形狀*/
int
Mrk_1[8]={-5,-160,5,-160,5,-130,-5,-130,
};
int
Mrk_2[8]={-5,-160,5,-160,2,-130,-2-130,
};
/*時針形狀*/
int
HourHand[8]={-3,-100,3,-120,4,
10,-4,10};
/*分針形狀*/
int
MiHand[8]={-3,-120,3,-120,4,
10,-4,10};
/*秒針形狀*/
int
SecHand[8]={-2,-150,2,-150,3,
10,-3,10};
/*發出喀嗒聲*/
void
Click()
{
sound(CLICK);
delay(CLICKDELAY);
nosound();
}
/*高聲報時*/
void
HighBeep()
{
sound(HEBEEP);
delay(BEEPDELAY);
nosound;
}
/*低聲報時*/
void
LowBeep()
{
sound(LOWBEEP);
}
/*按任意角度畫多邊形*/
void
DrawPoly(int
*data,int
angle,int
color)
{
int
usedata[8];
float
sinang,cosang;
int
i;
sinang=sin((float)angle/180*3.14);
cosang=cos((float)angle/180*3.14);
for(i=0;i<8;i+=2)
{
usedata[i]
=CENTERX+
cosang*data[i]-sinang*data[i+1]+.5;
usedata[i+1]=CENTERY+sinang*data[i]+cosang*data[i+1]+.5;
}
setfillstyle(SOLID_FILL,color);
fillpoly(4,usedata);
}
/*畫表盤*/
void
DrawClock(struct
time
*cutime)
{
int
ang;
float
hourrate,minrate,secrate;
setbkcolor(BLUE);
cleardevice();
setcolor(WHITE);
/*
畫刻度*/
for(ang=0;ang<360;ang+=90)
{
DrawPoly(Mrk_1,ang,WHITE);
DrawPoly(Mrk_2,ang+30,WHITE);
DrawPoly(Mrk_2,ang+60,WHITE);
}
secrate=(float)cutime->ti_sec/60;
minrate=((float)cutime->ti_min+secrate)/60;
hourrate=(((float)cutime->ti_hour/12)+minrate)/12;
ang=hourrate*360;
DrawPoly(HourHand,ang,YELLOW);/*畫時針*/
ang=minrate*360;
DrawPoly(MiHand,ang,
GREEN);/*畫分針*/
ang=secrate*360;
DrawPoly(SecHand,ang,
RED);/*畫秒針*/
}
main()
{
int
gdriver=EGA,
gmode=EGAHI;
int
curpage;
struct
time
curtime
,newtime
;
initgraph(&gdriver,&gmode,"c:\\tc");
setbkcolor(BLUE);
cleardevice();
gettime(&curtime);
curpage=0;
DrawClock(&curtime);
while(1)
{
if(kbhit())
break;
/*按任意鍵退出*/
gettime(&newtime);
/*檢測系統時間*/
if(newtime.ti_sec!=curtime.ti_sec)/*每1秒更新一次時間*/
{
if(curpage==0)
curpage=1;
else
curpage=0;
curtime=newtime;
/*設置繪圖頁*/
setactivepage(curpage);
/*在圖頁上畫表盤*/
DrawClock(&curtime);
/*設置繪圖頁為當前可見頁*/
setvisualpage(curpage);
/*0分0秒高聲報時*/
if(newtime.ti_min==0&&newtime.ti_sec==0)
HighBeep();
/*
59分55至秒時低聲報時*/
else
if(newtime.ti_min==59&&
newtime.ti_sec<=59)
LowBeep();/*其他時間只發出喀嗒聲*/
else
Click();
}
}
closegraph();
}
⑼ C語言 時鍾 編程 源代碼 越繁復越好
幫你找了一個!希望對你有幫助……(個人覺得在C語言中比較花哨了)#include "stdio.h"
#include "graphics.h"
#include "math.h"
#include "time.h"
#include "dos.h"
#define r 200
#define pi 3.1415926
#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300; /*遇到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; /*遇到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) /*宏定義不是說明或語句,在行末不必加分號,如加上分號則連分號也一起置換*/
/*
那麼就是當執行d(200,12,6)時 相當於寫了3句話
首先X(a,b,c) 也就是X(200,12,6) 這時計算x=200*cos(12*6*pi/180-pi/2)+300
也就是計算出了 以半徑為200,度數為12 步長為6 (實際上就是72度 但是這里又減去了pi/2 也就是72-45=27度)
的那點的橫坐標 也就是x的數值
然後執行Y(a,b,c)也就是Y(200,12,6)這時計算y=200*sin(12*6*pi/180-pi/2)+240
也就是計算出了 以半徑為200,度數為12 步長為6 (實際上就是72度 但是這里又減去了pi/2 也就是72-45=27度)
的那點的縱坐標 也就是y的數值
最後是 line(300,240,x,y) 也就是 畫線 這樣指針就出來了
那麼這里第一個參數是設置半徑長度 第二個參數是設置度數 第三個參數是設置步長(度數)
*/
main()
{
int x=300,y=240,bx,by,bx1,by1,bx2,by2,bx3,by3,jd,i,j,k,h1,m1,hc=0,l,ax,ay,n,p=0;
char b[]={'I',' ',' ',' ',' ','T','e','l','l',' ',' ',' ',' ','Y','o','u'};
int driver=VGA,mode=VGAHI; /*定義圖形驅動器*/
int h,m,s; /*定義時針hour 分針minute 秒針second*/
struct time t[1]; /*定義一個結構體名為time的數組,只有一個成員t[0]*/
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,""); /*設置圖形驅動器*/
setbkcolor(0); /*設置背景色為黑色*/
for(n=0;n<=27;n++) printf("\n");
for(n=0;n<=29;n++)
printf(" ");
setcolor(14); /*設置前景色為黃色*/
circle(x,y,r); /*以300,240,半徑為200畫大圓*/
setcolor(12); /*設置前景色為洋紅色*/
circle(x,y,190); /* 內部小圓*/
/*設置填充樣式及顏色*/
setfillstyle(SOLID_FILL,14); /*實心填充*/
floodfill(x,y,12); /* 填充表盤 填充色為洋紅(12) */
setfillstyle(SOLID_FILL,13); /*實心填充*/
floodfill(1,1,14);
setcolor(2); /*設置為綠色*/
/*畫表心*/
circle(x,y,2); /*表心小圓*/
circle(x,y,5); /*表心大圓*/
/*眼睛*/
ellipse(x-80,y-70,0,360,23,65); /*左 外面的橢圓*/
ellipse(x+80,y-70,0,360,23,65); /*右 外面的橢圓*/
ellipse(x-80,y-60,0,180,23,23); /*左 裡面的橢圓*/
setfillstyle(SOLID_FILL,0); /*實型填充 填充顏色為黑色*/
floodfill(x-80,y-60,2); /*填充顏色 綠色*/
ellipse(x+80,y-60,0,180,23,23); /*右 裡面的橢圓*/
setfillstyle(SOLID_FILL,0); /*實型填充 填充顏色為黑色*/
floodfill(x+80,y-70,2); /*填充顏色 綠色*/
/* outtextxy(225,380,"EmBEdDed 06241 ShiwU");
outtextxy(245,400,"MaDE In HuaXiA"); *//*六個鬍子*/
setcolor(5); /* 粉紅色 */
line(x-120,y+70,x-250,y+90);
line(x-120,y+90,x-250,y+110);
line(x-120,y+110,x-250,y+130);
line(x+120,y+70,x+250,y+90);
line(x+120,y+90,x+250,y+110);
line(x+120,y+110,x+250,y+130);
/*畫耳朵*/
arc(150,80,0,360,60); /*畫圓弧(此處完全可以用圓來代替)*/
setfillstyle(SOLID_FILL,14); /*填充色黃色*/
floodfill(150,80,5);
arc(450,80,0,360,60);
setfillstyle(SOLID_FILL,14);
floodfill(450,80,5);
setcolor(14); /*擦除內部圓弧痕跡*/
arc(150,80,0,360,60);
arc(450,80,0,360,60);
/*畫嘴*/
setcolor(0);
ellipse(x,y+60,160,340,23,23); /*用圓弧畫嘴*/
/*畫側臉*/
circle(x+120,y+10,23); /*前景色為0 側面的圓圈*/
setfillstyle( SOLID_FILL,12); /*實型填充 顏色為12 淡洋紅*/
floodfill(x+120,y+10,0); /*以側面的圓心為中心 邊緣顏色為0 白色 進行對封閉圖形的填充*/
setcolor(14); /*設置前景色為黃色 清除內部圓弧痕跡*/
circle(x+120,y+10,23); /*重新畫小圈*/
setcolor(0); /*前景色為0 側面的圓圈*/
circle(x-120,y+10,23); /*以下同上*/
setfillstyle( SOLID_FILL,12);
floodfill(x-120,y+10,0);
setcolor(14);
circle(x-120,y+10,23);ellipse(x,y+60,0,180,23,23);
for(i=0;i<60;i++)
{if(i%5==0) l=15;<br> else l=5;<br> ax=200*cos(i*6*pi/180)+300;<br> ay=200*sin(i*6*pi/180)+240;<br> bx=(200-l)*cos(i*6*pi/180)+300;<br> by=(200-l)*sin(i*6*pi/180)+240;<br> line(ax,ay,bx,by);</p><p> }
setwritemode(1); /*這句無敵了*/
gettime(t); /*得到系統時間給數組t實際上就是給了t[0]*/
h=t[0].ti_hour; /*時針數值給h*/
m=t[0].ti_min; /*分針數值給m*/
s=t[0].ti_sec;
/*秒針數值給s*/
setcolor(7); /*設置前景色為7 淡灰色*/
/*第一個參數是設置半徑長度 第二個參數是設置起始度數 第三個參數是設置步長(度數)*/
d(150,h,30); /*以半徑為150,h為起始度數,步長為30度,時針總共一圈才走12下*/
setcolor(14); /*設置分針顏色 1 藍色*/
d(170,m,6); /*以半徑170,m為起始度數,步長為6度,分針一圈走60下*/
setcolor(4); /*設置秒針顏色 4 紅色*/
d(190,s,6); /*以半徑190,s為起始度數,步長為6度,秒針一圈走60下*/
while(!kbhit()) /*當不敲擊鍵盤時候一直循環*/
{ while(t[0].ti_sec==s) /*判斷當前系統時間是否與剛才得到的秒時間相等,一定相等,沒有刷新系統時間*/
gettime(t);
setcolor(4);
d(190,s,6); /*所以重新刷新系統時間*/
s=t[0].ti_sec;
setfillstyle(SOLID_FILL,13); /*實心填充*/
floodfill(1,380,14);
if(p<=15)
{ setbkcolor(hc); printf("%c",b[p]); }
else
setbkcolor(0);
p++;
setcolor(4); /*設置秒針顏色為2 綠色*/
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);
}
setcolor(hc++);
if(hc==12)
hc=0;
ellipse(300,300,160,340,23,23); /*眼睛*/
ellipse(220,170,0,360,23,65);
ellipse(380,170,0,360,23,65);
ellipse(220,180,0,180,23,23);
ellipse(380,180,0,180,23,23); arc(150,80,20,250,59);
arc(450,80,-70,165,59);
} getch();
getch();
}