四位数字时钟设计 C语言程序

四位数字时钟设计 C语言程序,第1张

两个参看啊:

第一个:

算法:将当前时间显示到屏幕,当时间发生变化时,清屏,显示新的时间(当有键盘 *** 作时退出程序)。

显示时间格式:小时:分钟:秒

/ DEV C++ Win XP/

#include <stdioh>

#include <stdlibh>

#include <conioh>

#include <timeh>

typedef struct

{

int x;

int y;

}Point;

time_t now;

struct tm pt,t1,t2;

int printpoint(Point p)

{

Point p1;

p1x=px+2; p1y=py+4;

gotoxy(p1x,p1y); printf("%c%c",2,2);

gotoxy(p1x, p1y+1); printf("%c%c",2,2);

p1y+=4;

gotoxy(p1x,p1y); printf("%c%c",2,2);

gotoxy(p1x,p1y+1); printf("%c%c",2,2);

return 0;

}

int print0(Point p)

{

int i=0;

for(;i<13;i++)

{

gotoxy(px+1, py+i);

if(i==0||i==12)

printf("%c%c%c%c%c%c",2,2,2,2,2,2);

else

printf("%c%4s%c",2," ",2);

}

return 0;

}

int print1(Point p)

{

int i=0;

for(;i<13;i++)

{

gotoxy(px+1, py+i);

printf("%5s%c"," ",2);

}

return 0;

}

int print2(Point p)

{

int i=0;

for(;i<13;i++)

{

gotoxy(px+1, py+i);

if(i==0||i==6||i==12)

printf("%c%c%c%c%c%c",2,2,2,2,2,2);

else if(i>0&&i<6)

printf("%5s%c"," ",2);

else

printf("%c",2);

}

return 0;

}

int print3(Point p)

{

int i=0;

for(;i<13;i++)

{

gotoxy(px+1, py+i);

if(i==0||i==6||i==12)

printf("%c%c%c%c%c%c",2,2,2,2,2,2);

else

printf("%5s%c"," ",2);

}

return 0;

}

int print4(Point p)

{

int i=0;

for(;i<13;i++)

{

gotoxy(px+1, py+i);

if(i<6) printf("%c%4s%c",2," ",2);

else if(i==6)

printf("%c%c%c%c%c%c",2,2,2,2,2,2);

else printf("%5s%c"," ",2);

}

return 0;

}

int print5(Point p)

{

int i=0;

for(;i<13;i++)

{

gotoxy(px+1, py+i);

if(i==0||i==6||i==12)

printf("%c%c%c%c%c%c",2,2,2,2,2,2);

else if(i>0&&i<6)

printf("%c",2);

else

printf("%5s%c"," ",2);

}

return 0;

}

int print6(Point p)

{

int i=0;

for(;i<13;i++)

{

gotoxy(px+1, py+i);

if(i==0||i==6||i==12)

printf("%c%c%c%c%c%c",2,2,2,2,2,2);

else if(i>0&&i<6)

printf("%c",2);

else

printf("%c%4s%c",2," ",2);

}

return 0;

}

int print7(Point p)

{

int i=0;

for(;i<13;i++)

{

gotoxy(px+1, py+i);

if(i==0) printf("%c%c%c%c%c%c",2,2,2,2,2,2);

else printf("%5s%c"," ",2);

}

return 0;

}

int print8(Point p)

{

int i=0;

for(;i<13;i++)

{

gotoxy(px+1, py+i);

if(i==0||i==6||i==12)

printf("%c%c%c%c%c%c",2,2,2,2,2,2);

else printf("%c%4s%c",2," ",2);

}

return 0;

}

int print9(Point p)

{

int i=0;

for(;i<13;i++)

{

gotoxy(px+1, py+i);

if(i==0||i==6||i==12)

printf("%c%c%c%c%c%c",2,2,2,2,2,2);

else if(i>0&&i<6)

printf("%c%4s%c",2," ",2);

else

printf("%5s%c"," ",2);

}

return 0;

}

int clear(Point p)

{

int i=0;

for(;i<13;i++)

gotoxy(px,py+i); printf("%16s"," ");

return 0;

}

int printtime(Point p, int n)

{

int a,b;

Point pp;

a=n/10, b=n%10;

ppx=px+8, ppy=py;

switch(a)

{

case 0: print0(p); break;

case 1: print1(p); break;

case 2: print2(p); break;

case 3: print3(p); break;

case 4: print4(p); break;

case 5: print5(p); break;

}

switch(b)

{

case 0: print0(pp); break;

case 1: print1(pp); break;

case 2: print2(pp); break;

case 3: print3(pp); break;

case 4: print4(pp); break;

case 5: print5(pp); break;

case 6: print6(pp); break;

case 7: print7(pp); break;

case 8: print8(pp); break;

case 9: print9(pp); break;

}

return 0;

}

int main()

{

Point phour, pmin, psec,point1,point2;

phourx=9, pminx=32, psecx=55;

phoury=pminy=psecy=7;

point1x=25, point2x=49;

point1y=point2y=7;

clrscr();

textbackground(BLUE);

textcolor(YELLOW);

now=time(0);

pt=localtime(&now);

t1=pt;

printtime(phour, t1tm_hour);

printpoint(point1);

printtime(pmin, t1tm_min);

printpoint(point2);

printtime(psec, t1tm_sec);

while(1)

{

now=time(0);

pt=localtime(&now);

t2=pt;

if(t2tm_sec!=t1tm_sec)

{

t1=t2;

clrscr();

printtime(phour, t1tm_hour);

printpoint(point1);

printtime(pmin, t1tm_min);

printpoint(point2);

printtime(psec, t1tm_sec);

}

if(bioskey(1)==0) continue;

else exit(0);

}

return 0;

}

晕,还有一份字数限制了

给你2个选吧,都是原创:

第1个:

#include<graphicsh>

#include<mathh>

#include<dosh>

#define pi 31415926

#define X(a,b,c) x=acos(bcpi/180-pi/2)+300;

#define Y(a,b,c) y=asin(bcpi/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=200cos(i6pi/180)+300;

y1=200sin(i6pi/180)+240;

x2=(200-l)cos(i6pi/180)+300;

y2=(200-l)sin(i6pi/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<graphicsh>

#include<mathh>

#include<dosh>

#define PI 31415926

#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+lsin(th);

y1=y-lcos(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)curtimeti_hour<=12) /午前的处理/

{printf("AM ");

if((float)curtimeti_hour<10) printf("0"); /十点之前在小时数前加零/

printf("%0f:",(float)curtimeti_hour);

}

else /午后的处理/

{printf("PM ");

if((float)curtimeti_hour-12<10) printf("0");

printf("%0f:",(float)curtimeti_hour-12);

}

if((float)curtimeti_min<10) printf("0");

printf("%0f:",(float)curtimeti_min);

if((float)curtimeti_sec<10) printf("0");

printf("%0f",(float)curtimeti_sec);

/以下三行计算表针转动角度,以竖直向上为起点,顺时针为正/

th_sec=(float)curtimeti_sec01047197551; /2π/60=01047197551/

th_min=(float)curtimeti_min01047197551+th_sec/600;

th_hour=(float)curtimeti_hour0523598775+th_min/120; / 2π/12=05235987755 /

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();

}

//定义共阴极数码管,对应显示0-F

uchar code seven_seg[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,

0x6f,0x77,0x7C,0x39,0x5e,0x79,0x71};

uchar keyState = 0 ; // 0表示没有 *** 作 1 修改秒 2 修改分钟 3 修改小时 4 定闹 小时 5定闹分钟

uint ledState =0;

uchar ledStateNum=0;

uint num = 0 ;

sbit KEY1=P3^0; //定义IO口

sbit KEY2=P3^1;

sbit KEY3=P3^2;

sbit KEY4=P3^3;

char timer[] ={ 0 , 0 , 0} ; //时,分,秒

char n_timer[2]={11,0};

//延时程序 ,当调用此函数时,需要传递参数ms,用于设置延时时间

//若为12M晶振,每条指令对应1us的时间 ,要对应1ms的时间,就要选择

//0012M的晶振,也就是12K的晶振(有吗?)

// 若要用12M的晶振,实现1ms的延时,程序需要做修改。如下面注释的语句

void DelayMS(uint ms)

{

ms = 1000; //加上此语句,实现延时1ms

while(ms--);

}

void Display()

{

uchar i = 0,flags;

uchar count = 0;

uchar temp [6] ={ 0xfe,0xfd,0xfb,0xf7,0xef,0xdf};

if( 4 == keyState || 5 == keyState )

//从程序看,应该有外部中断处理函数用于设置keystate参数

//这个if写得比较另类,正常应把参数放在前面,

//判断按键是否按下,两个有一个按下即执行

{

P2 = temp[0]; //向P2口写数据

P0 = 0x00; //有了下面一条指令,这条语句没有用

P0 = 0x40; //向P0口写数据

DelayMS(1); //延时1ms

P2 = temp[1];

P0 = 0x00;

P0 = 0x40;

DelayMS(1);

P2 = temp[2];

P0 = 0x00;

if(ledState && 4 == keyState)

P0 = seven_seg[ n_timer[0] / 10 ];

else

P0 = seven_seg[ n_timer[0] / 10 ] | 0x80;

DelayMS(1);

P2 = temp[3];

P0 = 0x00;

if(ledState && 4 == keyState)

P0 = seven_seg[ n_timer[0] % 10 ];

else

P0 = seven_seg[ n_timer[0] % 10 ] | 0x80;

DelayMS(1);

P2 = temp[4];

P0 = 0x00;

if(ledState && 5 == keyState)

P0 = seven_seg[ n_timer[1] / 10 ];

else

P0 = seven_seg[ n_timer[1] / 10 ] | 0x80;

DelayMS(1);

P2 = temp[5];

P0 = 0x00;

if(ledState && 5 == keyState)

P0 = seven_seg[ n_timer[1] % 10 ];

else

P0 = seven_seg[ n_timer[1] % 10 ] | 0x80;

DelayMS(1);

P2 = 0XFF;

return ;

}

//没有主程序和外部中断处理函数,这段程序读起来不太好读懂

//应该是根据ledstate的状态,也就是4或5来进行时间的校对。

if( 0 == keyState)

{

P2 = temp[0];

P0 = 0x00;

P0 = seven_seg[ timer[0] / 10 ];

DelayMS(1);

P2 = temp[1];

P0 = 0x00;

P0=seven_seg[ timer[0] % 10 ];

DelayMS(1);

P2 = temp[2];

P0 = 0x00;

P0 = seven_seg[ timer[1] / 10 ];

DelayMS(1);

P2 = temp[3];

P0 = 0x00;

P0 = seven_seg[ timer[1] % 10 ];

DelayMS(1);

P2 = temp[4];

P0 = 0x00;

P0 = seven_seg[ timer[2] / 10 ];

DelayMS(1);

P2 = temp[5];

P0 = 0x00;

P0 = seven_seg[ timer[2] % 10 ];

DelayMS(1);

P2 = 0XFF;

return ;

}

//若不需要设置时间,显示时间

while( i < 6 )

{

if(3- keyState == i/2 && ledState == 0)

{

flags = 0;

}

else

{

flags = 1;

}

if( flags)

{

P2 = temp[i];

P0=0x00;

if(0 == i % 2)

{

P0 = seven_seg[ timer[i/2] / 10 ];

}

else

{

P0 = seven_seg[ timer[i/2] % 10 ];

}

}

i++;

}

//这个程序不全,没有看懂

}

这个程序不复杂的。如果懂单片机和C语言很容易读懂的。

有不懂的地方,用Keil或Proteus仿真一下。就读懂了

自己动手 *** 作一下,总比在这里等强。

再说你给的悬赏分也不高啊

以上就是关于四位数字时钟设计 C语言程序全部的内容,包括:四位数字时钟设计 C语言程序、C语言编图形时钟、一部分用C语言编写的C51单片机时钟程序,求加注释等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10070185.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存