c語言圖形時鍾
1. 圖形時鍾用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();//關閉繪圖窗口
}
2. C語言編輯一個程序。要求在屏幕上顯示一個圖形時鍾(用程序繪制一個與時鍾樣式相似即可)用的什麼函數
用 MFC 吧 下面是C語言里的函數 時間函數 #include #include void main () { time_t rawtime; struct tm * timeinfo; char timE [80]; time ( &rawtime ); timeinfo = localtime ( &rawtime ); strftime ( timE,80,"Data:\n%Y-%m-%d \nTime:\n%I...
3. C語言程序設計:圖形時鍾
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "graphics.h"
#include "math.h"
#include "time.h"
#include "dos.h"
#include "stdlib.h"
#include "conio.h"
#define R 200
#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(void);
void draw_bp(void);
void close(void);
void draw_kt(void);
int main(void)
{
int x,y,hc=0;
int bx,by,ax,ay;
int h,m,s;
struct time t[1];
init();
draw_bp();
draw_kt();
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,h,6);
setcolor(4);
d(190,s,6);
while(!kbhit())
{
while(t[0].ti_sec==s)
gettime(t);
sound(400);
delay(70);
sound(200);
delay(30);
nosound();
setcolor(4);
d(190,s,6);
s=t[0].ti_sec;
setfillstyle(SOLID_FILL,13);
floodfill(1,380,14);
setcolor(4);
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();
}
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(300,180,0,180,23,23);
ellipse(380,180,0,180,23,23);
arc(150,80,20,250,59);
}
close();
return 0;
}
void init(void)
{
int driver,mode;
driver=DETECT;
initgraph(&driver,&mode,"c:\\tc");
cleardevice();
}
void draw_bp(void)
{
int x=300,y=240,i,n,l,ax,ay,bx,by;
for(n=0;n<27;n++)
printf("\n");
for(n=0;n<29;n++)
printf(" ");
setbkcolor(0);
setcolor(14);
circle(x,y,R);
setcolor(12);
circle(x,y,190);
setfillstyle(SOLID_FILL,14);
floodfill(x,y,12);
setfillstyle(SOLID_FILL,13);
floodfill(1,1,14);
setcolor(2);
circle(x,y,2);
circle(x,y,5);
for(i=0;i<60;i++)
{
if(i%5==0)
l=15;
else
l=5;
ax=200*cos(i*6*PI/180)+300;
ay=200*sin(i*6*PI/180)+240;
bx=(200-l)*cos(i*6*PI/180)+300;
by=(200-l)*sin(i*6*PI/180)+240;
line(ax,ay,bx,by);
}
settextstyle(3,0,5);
outtextxy(260,380,"MADEIN CHINA");
}
void draw_kt(void)
{
int x=300,y=240;
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);
setcolor(5);
circle(150,80,60);
setfillstyle(SOLID_FILL,14);
floodfill(150,80,5);
circle(450,80,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);
setfillstyle(SOLID_FILL,12);
floodfill(x+120,y+10,0);
setcolor(14);
circle(x+120,y+10,23);
setcolor(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);
}
void close(void)
{
getch();
closegraph();
}
4. C語言編圖形時鍾
給你2個選吧,都是原創:
第1個:
#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*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);
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();
}
第2個:
#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();
}
5. 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(); }
6. 怎麼使用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',' ',' ',' ',' ','L','o','v','e',' ',' ',' ',' ','Y','o','u'};
int driver=VGA,mode=VGAHI; /*定義圖形驅動器*/
int h,m,s; /*定義時針hour 分針minute 秒針second*/
struct time t[1]; /*定義一個結構體名為time的數組,只有一個成員t[0]*/
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;
else l=5;
ax=200*cos(i*6*pi/180)+300;
ay=200*sin(i*6*pi/180)+240;
bx=(200-l)*cos(i*6*pi/180)+300;
by=(200-l)*sin(i*6*pi/180)+240;
line(ax,ay,bx,by);
}
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();
}
7. 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(); //清屏
}
}
8. 如何用C語言在圖形界面里畫時鍾時針
這是我用C語言寫的
需要採用EasyX圖形庫,繪制方法很簡單,只需要根據時間,算出指針的偏轉角度,然後從中心點向外畫長度不等的線即可
9. C語言 用Devc++做一個圖形時鍾,要能在devc上運行的,簡簡單單的就行,謝謝。
#include<iostream>
#include<windows.h>
using namespace std;
class Clock{
public:
Clock(short h=0,short m=0,short s=0):h(h),m(m),s(s){
}
void displayTime();
private:
short h;
short m;
short s;
};void Clock::displayTime(){
while(true){
cout<<h<<':'<<m<<':'<<s<<" ";
Sleep(1000);//一秒更新一次
cout<<'\r';
if(!(s=++s%60))
if(!(m=++m%60))
h=++h%24;
}
} int main()
{
Clock A(12,20,30);//初始時間
A.displayTime();//更新函數
return 0;
}
/*
12:20:3
10. C語言 用Devc++編一個圖形時鍾,要求能在devc++上運行的,簡簡單單的就可以了。
//圖形庫是EGE,下載後配置一下
#include<iostream>
#include"graphics.h"
#include<math.h>
#include<time.h>
#include<windows.h>
#definex0210.0
#definey0210.0
#definer200.0
POINTpt[60],pt_s[60],pt_m[60],pt_h[60],pt1[60];
usingnamespacestd;
intmain()
{
initgraph(420,420);
setfillcolor(0xffccff);
setcolor(0xffccff);
fillellipse(x0,y0,10,10);
ints=45,m,h,n=0;
while(n<60)
{
pt_s[s].x=x0+(int)((r-20)*cos((n-90)*3.1415926/30.0));
pt_s[s].y=y0+(int)((r-20)*sin((n-90)*3.1415926/30.0));
pt_m[s].x=x0+(int)((r-50)*cos((n-90)*3.1415926/30.0));
pt_m[s].y=y0+(int)((r-50)*sin((n-90)*3.1415926/30.0));
pt_h[s].x=x0+(int)((r-80)*cos((n-90)*3.1415926/30.0));
pt_h[s].y=y0+(int)((r-80)*sin((n-90)*3.1415926/30.0));
pt[s].x=x0+(int)(r*cos((n-90)*3.1415926/30.0));
pt[s].y=y0+(int)(r*sin((n-90)*3.1415926/30.0));
fillellipse(pt[s].x,pt[s].y,2,2);
n++;
s++;
if(s>=60)
{
s=0;
}
}
for(inti=0;i<12;i++)
{
fillellipse(pt[i*5].x,pt[i*5].y,5,5);
}
intxs,ys,xm=-1,ym=-1,xh=-1,yh=-1;
while(1)
{
intn,m;
SYSTEMTIMEst={0};
GetLocalTime(&st);
setcolor(0xffccff);
line(x0,y0,pt_s[st.wSecond].x,pt_s[st.wSecond].y);
xs=pt_s[st.wSecond].x;
ys=pt_s[st.wSecond].y;
if(pt_m[st.wMinute].x!=xm&&pt_m[st.wMinute].y!=ym)
{
setcolor(0);
line(x0,y0,xm,ym);
xm=pt_m[st.wMinute].x;
ym=pt_m[st.wMinute].y;
setcolor(0xffccff);
}
line(x0,y0,pt_m[st.wMinute].x,pt_m[st.wMinute].y);
if(st.wHour>12)
{
n=st.wHour-12;
}
else
{
n=st.wHour;
}
m=(n*60+st.wMinute)/12;
line(x0,y0,pt_h[m].x,pt_h[m].y);
Sleep(1000);
setcolor(0x0);
line(x0,y0,xs,ys);
fillellipse(x0,y0,10,10);
}
closegraph();
return0;
}