本文目錄一覽:
怎麼用C語言編程數字時鐘
1、以下例程實現時鐘的實時顯示基本要求: 1) 自行設計界面,模擬錶盤式時鐘。要求界面美觀,清晰。2)數字同步顯示時間信息。
2、例程:
#includegraphics.h
#includemath.h
#includedos.h
#define PI 3.1415926
//屏幕中心的坐標(640X480模式下)
#define mid_x 320
#define mid_y 240
int main()
{ int graphdriver=DETECT,graphmode;
int end_x,end_y;
struct time curtime;
float th_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屏幕,即返迴文本方式
return 0;
}
圖形時鐘用C語言怎麼編?
#include graphics.h
#include conio.h
#include math.h
void Draw(int hour, int minute, int second)
{
double a_hour, a_min, a_sec; // 時、分、秒針的弧度值
int x_hour, y_hour, x_min, y_min, x_sec, y_sec; // 時、分、秒針的末端位置
int x_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);
}
void main()
{
initgraph(640, 480); // 初始化 640 x 480 的繪圖窗口
// 繪製一個簡單的錶盤
circle(320, 240, 2);
circle(320, 240, 60);
circle(320, 240, 160);
outtextxy(296, 330, ” 竹斌”);
int x,y;
for(int i=0;i12;i++)
{
x=320+(int)(140*sin(30*i*2*PI/360));
y=240-(int)(140*cos(30*i*2*PI/360));
switch(i)
{
case 0:outtextxy(x-5,y-5,”12″);break;
case 1:outtextxy(x-5,y-5,”1″);break;
case 2:outtextxy(x-5,y-5,”2″);break;
case 3:outtextxy(x-5,y-5,”3″);break;
case 4:outtextxy(x-5,y-5,”4″);break;
case 5:outtextxy(x-5,y-5,”5″);break;
case 6:outtextxy(x-5,y-5,”6″);break;
case 7:outtextxy(x-5,y-5,”7″);break;
case 8:outtextxy(x-5,y-5,”8″);break;
case 9:outtextxy(x-5,y-5,”9″);break;
case 10:outtextxy(x-5,y-5,”10″);break;
case 11:outtextxy(x-5,y-5,”11″);break;
}
}
// 設置 XOR 繪圖模式
setwritemode(R2_XORPEN); // 設置 XOR 繪圖模式
//畫刻度
int a,b,a1,b1,n=0;
for(n=0;n60;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);
}
// 繪製錶針
SYSTEMTIME ti; // 定義變量保存當前時間
while(!kbhit()) // 按任意鍵退出鐘錶程序
{
GetLocalTime(ti); // 獲取當前時間
Draw(ti.wHour, ti.wMinute, ti.wSecond); // 畫錶針
Sleep(1000); // 延時 1 秒
Draw(ti.wHour, ti.wMinute, ti.wSecond); // 擦錶針(擦錶針和畫錶針的過程是一樣的)
}
closegraph(); // 關閉繪圖窗口
}
怎麼使用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;i60;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();
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/238373.html