C语言编图形时钟

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言编图形时钟相关的知识,希望对你有一定的参考价值。

帮忙编个时钟,外表尽量美观
用TC图形编来编,谢谢啦,编好了还加分,不要COPY,要原创的
函数尽量简单的
郁闷,都是网上考的,除了6楼,不过没注释啊,看不懂
写的人顺便注释下

给你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();
参考技术A #include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define PI 3.1415926

int main()

int X=300,Y=250,R=100,RR=130;
int X_HOUR,X_MINUTE,X_SECOND;
int Y_HOUR,Y_MINUTE,Y_SECOND;
int X_DRAW=0,Y_DRAW=0,R_DRAW=120;
int NUM=0;
struct time T;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"D:\\Program Files\\TC\\BGI");
setlinestyle(0,1,3);
setcolor(LIGHTGRAY);
circle(X,Y,RR);
setlinestyle(0,1,1);
for(NUM=0;NUM<12;NUM++)

setcolor(11);
X_DRAW=X+R_DRAW*cos(NUM*2*PI/12);
Y_DRAW=Y+R_DRAW*sin(NUM*2*PI/12);
circle(X_DRAW,Y_DRAW,5);
setfillstyle(1,11);
if((NUM%3)==0)floodfill(X_DRAW,Y_DRAW,11);


while(!kbhit())

gettime(&T);
X_HOUR=X+(R/2)*cos(T.ti_hour*2*PI/12+T.ti_min*2*PI/(60*12)-PI/2);
X_MINUTE=X+R*cos(T.ti_min*2*PI/60-PI/2)*3/4;
X_SECOND=X+R*cos(T.ti_sec*2*PI/60-PI/2);
Y_HOUR=X+(R/2)*sin(T.ti_hour*2*PI/12+T.ti_min*2*PI/(60*12)-PI/2);
Y_MINUTE=Y+R*sin(T.ti_min*2*PI/60-PI/2)*3/4;
Y_SECOND=Y+R*sin(T.ti_sec*2*PI/60-PI/2);
setcolor(YELLOW);
line(X,Y,X_HOUR,Y_HOUR);
setcolor(RED);
line(X,Y,X_MINUTE,Y_MINUTE);
setcolor(BLUE);
line(X,Y,X_SECOND,Y_SECOND);
delay(10000);
setcolor(BLACK);
line(X,Y,X_HOUR,Y_HOUR);
line(X,Y,X_MINUTE,Y_MINUTE);
line(X,Y,X_SECOND,Y_SECOND);

getch();
closegraph();
return 0;


在使用时请把initgraph中的第三个参数改成你的tc下BGI文件夹所在的目录,注意:目录中不要有空格,\要用\\代替。本回答被提问者采纳
参考技术B 试试这个
#include"graphics.h"
#define PI 3.1416
#include"math.h"
#include"dos.h"
main()

int x0=320,y0=240,r0=150;
void init_sceen();
void sec();
init_sceen(x0,y0,r0);
sec();
closegraph();

void init_sceen(int x0,int y0,int r0)/********************************************/

int i,x,y,graphdriver,graphmode;
char s[10];
float alpha,a0=90;

graphdriver=DETECT;
initgraph(&graphdriver,&graphmode,"");
setbkcolor(3);
setcolor(2);
circle(x0,y0,r0);
circle(x0,y0,r0+30);
setfillstyle(SOLID_FILL,10);
floodfill(x0-r0-10,y0,2);
/*please input the time*/
for(i=12;i>=1;i--)

alpha=(a0+30*(11-i)*PI/180);
x=x0+cos(alpha)*r0-16;
y=y0-sin(alpha)*r0;
sprintf(s,"%2d",i);
setcolor(4);
settextstyle(0,0,2);
outtextxy(x,y,s);

/*input second*/
for(i=60;i>=1;i--)

alpha=(a0+6*(60-i)*PI/180);
x=x0+cos(alpha)*(r0-20);
y=y0-sin(alpha)*(r0-20);
setcolor(14);
if(i%5==0)
circle(x,y,5);
else circle(x,y,2);
floodfill(x,y,14);

setlinestyle(0,0,3);

void sec(void) /******************************************************************/

int x,y,i,j,k,xj,yj,xk,yk,xi,yi,x0=320,y0=240,r0=150;
union REGS r;

unsigned char *shijie="";
unsigned char *daa="";
struct time tim;
struct date dat;
float alphai,alphak,alphaj,a0=90;
xi=x0;yi=y0;xj=x0;yj=y0;xk=x0;yk=y0;
do

/*intput the time*/
x=38;y=12;
gettime(&tim);
sprintf(shijie,"%02d:%02d:%02d",tim.ti_hour,tim.ti_min,tim.ti_sec);
setfillstyle(SOLID_FILL,0);
bar(245,190,375,210);
setcolor(15);
outtextxy(245,190,shijie);
/*input the date*/ /*****************************************************/
getdate(&dat);
sprintf(daa,"%02d--%02d--%02d",dat.da_year,dat.da_mon,dat.da_day);
/*setfillstyle(SOLID_FILL,3);*/
bar(225,290,395,310);
setcolor(RED);
outtextxy(225,290,daa);
x=190;y=430;
setcolor(RED);
outtextxy(x-26,y,"Designed by GuoLiuTa0");
setcolor(LIGHTRED);
outtextxy(x+76,y0+20,"NBA GAME");
setlinestyle(0,0,3);
k=tim.ti_hour;
j=tim.ti_min;
i=tim.ti_sec;
alphak=(a0+30*(12-k)-j*5/60.*6)*PI/180;
alphaj=(a0-6*j)*PI/180;
/*write second hand*/
alphai=(a0+6*(60-i))*PI/180;
x=x0+cos(alphai)*(r0-32);
y=y0-sin(alphai)*(r0-32);
setcolor(BLACK);
line(x0,y0,xi,yi);
setcolor(YELLOW);
line(x0,y0,x,y);
xi=x;
yi=y;
/*write minute hand*/
x=x0+cos(alphaj)*(r0-60);
y=y0-sin(alphaj)*(r0-60);
setcolor(BLACK);
line(x0,y0,xj,yj);
setcolor(BLUE);
line(x0,y0,x,y);
xj=x;
yj=y;
/*write hour hand*/
x=x0+cos(alphak)*(r0-99);
y=y0-sin(alphak)*(r0-99);
setcolor(BLACK);
line(x0,y0,xk,yk);
setcolor(RED);
line(x0,y0,x,y);
xk=x;
yk=y;
delay(10000);

while(!kbhit());


或者这个/*全屏幕模拟时钟的c源程序*/
#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(0);
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()) /*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(); /*关闭图形系统*/

注意是两个
参考技术C 一个完整的c程序如下,程序在win-tc和tc.0下都调试通过,代码简练。
#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(BLACK);
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();
参考技术D 又是某课程设计不想自己做吧..

linux高编信号-------setitimer()getitimer()

/*************************
 *功能:获取时钟间隔
 *参数:which:设置哪个时钟
 *             ITIMER_REAL:实时递减,超时发送SIGALRM信号
 *             ITIMER_VIRTURL:虚拟时钟,超时发送SIGVALRM时钟
 *             ITIMER_PROF: 
 * **********************/
int getitimer(int which, struct itimerval *curr_value);


/*************************
 *功能:设置时钟间隔
 *参数:which:设置哪个时钟
 *             ITIMER_REAL:实时递减,超时发送SIGALRM信号
 *             ITIMER_VIRTURL:虚拟时钟,超时发送SIGVALRM时钟
 *             ITIMER_PROF: 
 *      new_value:新的时钟
 *      old_value:旧时间
 * **********************/
int setitimer(int which, const struct itimerval *new_value,struct itimerval *old_value);

//时间赋值是原子操作
struct itimerval { struct timeval it_interval; /* next value */ struct timeval it_value; /* current value */ }; struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };

 

以上是关于C语言编图形时钟的主要内容,如果未能解决你的问题,请参考以下文章

如何用c语言作出有图形界面的小程序?

怎样用c语言写一个图形界面

如何用c语言编写图形操作的软件呢??

c语言怎么样编写一个时钟程序

怎么用C语言编程数字时钟

深入学习C语言的具体步骤