
////////////////////////////////////////////
// 程序名称:钟表模拟程序(表针形式)
// 编译环境:Visual C++ 60,EasyX 2011惊蛰版
// 程序编写:BestAns <BestAns@qqcom>
// 最后更新:2010-10-30
//
#include
<graphicsh>
#include
<conioh>
#include
<mathh>
#define
PI
31415926536
void
DrawHand
(
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
;
// 时、分、秒针的末端位置
// 计算时、分、秒针的弧度值
a_sec
=
second
2
PI
/
60
;
a_min
=
minute
2
PI
/
60
+
a_sec
/
60
;
a_hour
=
hour
2
PI
/
12
+
a_min
/
12
;
// 计算时、分、秒针的末端位置
x_sec
=
int
(
120
sin
(
a_sec
));
y_sec
=
int
(
120
cos
(
a_sec
));
x_min
=
int
(
100
sin
(
a_min
));
y_min
=
int
(
100
cos
(
a_min
));
x_hour
=
int
(
70
sin
(
a_hour
));
y_hour
=
int
(
70
cos
(
a_hour
));
// 画时针
setlinestyle
(
PS_SOLID
,
NULL
,
10
);
setcolor
(
WHITE
);
line
(
320
+
x_hour
,
240
-
y_hour
,
320
-
x_hour
/
7
,
240
+
y_hour
/
7
);
// 画分针
setlinestyle
(
PS_SOLID
,
NULL
,
6
);
setcolor
(
LIGHTGRAY
);
line
(
320
+
x_min
,
240
-
y_min
,
320
-
x_min
/
5
,
240
+
y_min
/
5
);
// 画秒针
setlinestyle
(
PS_SOLID
,
NULL
,
2
);
setcolor
(
RED
);
line
(
320
+
x_sec
,
240
-
y_sec
,
320
-
x_sec
/
3
,
240
+
y_sec
/
3
);
}
void
DrawDial
()
{
// 绘制一个简单的表盘
circle
(
320
,
240
,
2
);
circle
(
320
,
240
,
60
);
circle
(
320
,
240
,
160
);
outtextxy
(
296
,
310
,
"BestAns"
);
// 绘制刻度
int
x
,
y
;
for
(
int
i
=
0
;
i
<
60
;
i
++)
{
x
=
320
+
int
(
145
sin
(
PI
2
i
/
60
));
y
=
240
+
int
(
145
cos
(
PI
2
i
/
60
));
if
(
i
%
15
==
0
)
bar
(
x
-
5
,
y
-
5
,
x
+
5
,
y
+
5
);
else
if
(
i
%
5
==
0
)
circle
(
x
,
y
,
3
);
else
putpixel
(
x
,
y
,
WHITE
);
}
}
void
main
()
{
initgraph
(
640
,
480
);
// 初始化 640 x 480 的绘图窗口
DrawDial
();
// 绘制表盘
setwritemode
(
R2_XORPEN
);
// 设置 XOR 绘图模式
// 绘制表针
SYSTEMTIME
ti
;
while
(!
kbhit
())
// 按任意键退出钟表程序
{
GetLocalTime
(&
ti
);
// 获取当前时间
DrawHand
(
ti
wHour
,
ti
wMinute
,
ti
wSecond
);
// 画表针
Sleep
(
1000
);
// 延时 1 秒
DrawHand
(
ti
wHour
,
ti
wMinute
,
ti
wSecond
);
// 擦表针(擦表针和画表针的过程是一样的)
}
closegraph
();
// 关闭绘图窗口
}
其了就是用了easyx
/开发环境:turbo c 20模拟时钟转动程序代码/
#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=200sin(i6pi/180)+300;
y1=200cos(i6pi/180)+240;
x2=(200-l)sin(i6pi/180)+300;
y2=(200-l)cos(i6pi/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();
}
/源程序默认硬件环境:52单片机,12MHz晶振,四位共阳数码管,P0段选,P20-P23低电平位选,P23最高位,P20最低位/#include"reg52h"//包含52头文件#defineTRUE1//定义布尔量'1':真#defineFALSE0//定义布尔量'0':假#defineucharunsignedchar//定义无符号字符型数据简称#defineuintunsignedint//定义无符号整型数据简称#defineth00x3c#definetl00xb0//50msat12MHz(定时器工作模式1状态)#defineth10xfc#definetl10x18//1msat12MHz(定时器工作模式1状态)#defineT1sAt50msCount20//定义1s在50ms计时基准状态下的计数值为20#defineSEG_Num4#defineSEG_DataP0//数码管段驱动接口#defineSEG_EnP2//数码管位驱动接口#defineSEG_AllOff(SEG_En|=0x0f)//关闭所有数码管(位驱动)#defineDisTimeAt1msCount4//单'位'数码管显示时间,数码管刷新频率f=1/(Nt),其中N为数码管位数,t为单'位'数码管显示时间ucharSEG_B_List[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共阳数码管代码表"0-9"ucharSec,Min;ucharbdataFlag=2;sbitT1sTimesUpFlag=Flag^0;sbitDisplayFlag=Flag^1;voidTimer0()interrupt1//定时器0中断函数{staticuchart50ms;TL0=tl0;TH0=th0;t50ms=++t50ms%T1sAt50msCount;//先对50ms计时变量加1,后对变量范围进行限制(0~19)(即对20取模)if(!t50ms)T1sTimesUpFlag=TRUE;//若计时变量归0,表示计时变量曾经到达20(1s),则对1s计时标志位置位}voidTimer1()interrupt3{staticuchart1ms;TL1=tl1;TH1=th1;t1ms=++t1ms%DisTimeAt1msCount;//先计数值加1,后对计数范围进行限制0~(DisTimeAt1msCount-1)if(!t1ms)DisplayFlag=TRUE;//若定时计数值归0,则表示计数值曾到达单'位'显示时间(DisTimeAt1msCount),显示标志置位}voidTimerInit(){TMOD=0x11;//开启定时器0、定时器1,并都工作在模式1TH0=th0;TL0=tl0;TR0=1;//启动T0定时器计时ET0=1;//允许定时器0中断TH1=th1;TL1=tl1;TR1=1;//启动T1定时器计时ET1=1;//允许定时器1中断EA=1;//开启系统中断功能}voidTimesUpdata()//时间更新函数{if(T1sTimesUpFlag)//若1s计时标志位为真,即1s定时时间到{Sec=++Sec%60;//秒Sec在0-59范围内加1if(!Sec)Min=++Min%60;//若秒Sec重归0,则分Min在0-59范围内加1T1sTimesUpFlag=FALSE;//清1s计时标志位}}floatPow_Self(floatx,uinty)//自编简易x的y次方函数,y只能是非负整数{floatsum;if(x==0&&y==0)return;//0的0次方无意义elseif(x==0)sum=0;//可有可无,y!=0的情况已经包含x=0,不加不影响结果,但影响运算速度elseif(y==0)sum=1;//除上述情况外,任何数的0次方均为1elseif(y==1)sum=x;//任何数的1次方均为本身elseif(y>1)sum=Pow_Self(x,--y)x;//递归调用,降幂returnsum;//返回计算结果}voidDisplay(uintdis_num)//显示函数,显示内容为无符号整型数据dis_num{staticdis_loca;//定义静态变量显示位置dis_loca=++dis_loca%SEG_Num;//先对显示位置加1,后对变量范围进行限制0~(SEG_Num-1)SEG_AllOff;//关闭所有数码管显示(位驱动)SEG_Data=SEG_B_List[(dis_num/(uint)(Pow_Self(10,dis_loca)))%10];//将显示内容(dis_num)本次需显示的位(dis_loca)上的数值转成代码,并送到数据端口if(dis_loca==2)SEG_Data&=0x7f;//显示小数点,用于区分Min跟SecSEG_En&=~(1<
#include<reg52h>
#include<intrinsh>
#define uchar unsigned char
#define uint unsigned int
uchar code DSY_CODE[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0xff};
uchar DSY_BUFFER[]={0,0,0xBF,0,0,0xBF,0,0};
uchar Scan_BIT;
uchar DSY_IDX;
uchar Key_State;
uchar h,m,s,s100;
void DelayMS(uchar x)
{
uchar i;
while(x--) for(i=0;i<120;i++);
}
void Increase_Hour()
{
if(++h>23)h=0;
DSY_BUFFER[0]=DSY_CODE[h/10];
DSY_BUFFER[1]=DSY_CODE[h%10];
}
void Increase_Minute()
{
if(++m>59)
{
m=0;Increase_Hour();
}
DSY_BUFFER[3]=DSY_CODE[m/10];
DSY_BUFFER[4]=DSY_CODE[m%10];
}
void Increase_Second()
{
if(++s>59)
{
s=0;Increase_Minute();
}
DSY_BUFFER[6]=DSY_CODE[s/10];
DSY_BUFFER[7]=DSY_CODE[s%10];
}
void T0_INT()interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
P3=Scan_BIT;
P0=~DSY_BUFFER[DSY_IDX];
Scan_BIT=_crol_(Scan_BIT,1);
DSY_IDX=(DSY_IDX+1)%8;
}
void T1_INT()interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
if(++s100==20)
{
s100=0;Increase_Second();
}
}
void main()
{
P0=P3=0xFF;
TMOD=0x11;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TH1=0xDC;
TL1=0;
TCON=0x01;
EA=1;
ET0=1;
ET1=1;
h=12;m=s=s100=0;
DSY_BUFFER[0]=DSY_CODE[h/10];
DSY_BUFFER[1]=DSY_CODE[h%10];
DSY_BUFFER[3]=DSY_CODE[m/10];
DSY_BUFFER[4]=DSY_CODE[m%10];
DSY_BUFFER[6]=DSY_CODE[s/10];
DSY_BUFFER[7]=DSY_CODE[s%10];
Scan_BIT=0xFE;
DSY_IDX=0;
TR0=TR1=1;
Key_State=0xFF;
while(1)
{
if(P1^Key_State)
{
DelayMS(10);
if(P1^Key_State)
{
Key_State=P1;EA=0;
if((Key_State&0x01)==0)Increase_Hour();
else if((Key_State&0x02)==0)
{
m=(m+1)%60;
DSY_BUFFER[3]=DSY_CODE[m/10];
DSY_BUFFER[4]=DSY_CODE[m%10];
}
EA=1;
}
}
}
}
分给我吧,我不知道您要的是液晶时钟还是数码管的,但是我给你的是时钟,日历很好加,自己根据理解家上吧。希望对你有帮助。
#include "stdioh";
void main(){
int input;
scanf("%d",&input);
if(input<100||input>9999) printf("输入数字不在范围\n");
int hr,min;
hr=input/100;
min=input%100;
if(hr<0||hr>23||min<0||min>59){
printf("输入数字无法转换为正确时间\n");
}
else{
printf("输入的时间为%d时%d分\n",hr,min);
}
}//代码未经调试,自己试一下。
#include <AT89X52H> unsigned char dispbitcnt; //数码管位码扫描变量
unsigned char second; //秒变量
unsigned char minite; //分变量
unsigned char hour; //时变量
unsigned char tcnt; //秒信号产生变量
unsigned char mstcnt; //扫描时间变量 const unsigned char dispcode[10]={0xc0,0xf9,0xa4,0xb0,0x99,<br> 0x92,0x82,0xf8,0x80,0x90};
//0123456789段码
const unsigned char dispbitcode[6]={0xfe,0xfd,0xfb,0xf7,<br> 0xef,0xdf};
//123456位码
unsigned char dispbuf[6]={0,0,0,0,0,0};
//缓冲数组
//TMR0中断数码管扫描程序
void Timer0() interrupt 1
{ P2=dispcode[dispbuf[dispbitcnt]];//段码
P1=dispbitcode[dispbitcnt];//位码
dispbitcnt++;
if(dispbitcnt==6)
{dispbitcnt=0;<br> }
tcnt++; //中断产生秒信号
if(tcnt==244)
{tcnt=0;<br> second++;<br> if(second==60)<br> {second=0;<br> minite++;<br> if(minite==60)<br> {minite=0;<br> hour++;<br> if(hour==24)<br> {hour=0;<br> }
}
}
dispbuf[4]=second%10; //得秒钟的低位,送缓冲数组
dispbuf[5]=second/10; //得秒钟的高位,送缓冲数组
dispbuf[2]=minite%10; //得分钟的低位,送缓冲数组
dispbuf[3]=minite/10; //得分钟的高位,送缓冲数组
dispbuf[0]=hour%10; //得时钟的低位,送缓冲数组
dispbuf[1]=hour/10; //得时钟的高位,送缓冲数组
}
TMOD=0x01;
TL0=0x10;
TH0=0xf0; }
///1ms延时程序
void delay_1ms(unsigned char i)
{
unsigned char j;
for(i=0;i<144;i++)
for(j=0;j<144;j++);
} ///按键检测
void key_scan(void)
{
while(1)
{
if(P3_0==0)
{
delay_1ms(20);
if(P3_0==0)
{second++;<br> if(second==60)<br> {second=0;<br> }
dispbuf[4]=second%10;
dispbuf[5]=second/10;
while(P3_0==0);
}
}
if(P3_1==0)
{
delay_1ms(20);
if(P3_1==0)
{
minite++;
if(minite==60)
{minite=0;<br> }
dispbuf[2]=minite%10;
dispbuf[3]=minite/10;
while(P3_1==0);
}
}
if(P3_2==0)
{
delay_1ms(20);
if(P3_2==0)
{
hour++;
if(hour==24)
{
hour=0;
}
}
dispbuf[0]=hour%10;
dispbuf[1]=hour/10;
while(P3_2==0);
}
}
}
//主程序
void main(void)
{
TMOD=0x01;TL0=0x10;
TH0=0xf0;
while(1)
{
dispbitcnt=0;P1=0x00; //开段码
P2=0x00; //开位码
delay_1ms(200); //延时
delay_1ms(200); //延时
delay_1ms(200); //延时
delay_1ms(200); //延时
P2=0xff; //关位码EA=1;
ET0=1;
TR0=1;
key_scan();
}
}
以上就是关于跪求C语言编写的时钟(能修改时钟时间,且时钟是指针式的 只编写修改时间的部分也行)全部的内容,包括:跪求C语言编写的时钟(能修改时钟时间,且时钟是指针式的 只编写修改时间的部分也行)、C语言程序设计题:模拟时钟转动的程序、四位数码管的时钟C语言程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)