#include<graphicsh> / 引入graphich /
#include<mathh> / 引入mathh /
#include<dosh> / 引入dosh /
#define pi 31415926 /定义pi=314159/
#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); /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();
}
具体的。。就是套用用几个函数算时间。。
不要对这种很长的东西害怕,其实大部分都是在画这个钟~
加油哦~
#include<REG2051H>
code senen_seg[10]={0x81,0xe7,0x92,0xa2,0xe4,0xa8,0x88,0xe3,0x80,0xa0}; //P17(冒号)口高电平
bit key1_enter=0,key2_enter=0,key3_enter=0,countdown_mark=0,stopwatch_mark=0,count_mark=0,bell_mark=0; //状态标志
unsigned char program=0,program_variable=0,count_bit=0,count=0;
unsigned char hour=10,minute=10,second=0; //时间变量
unsigned char delayed_hour=22,delayed_minute=10,delayed_second=0; //定时变量
unsigned char count_hour=0,count_minute=0,count_second=0; //计时计数变量
unsigned char count_time=0,count_count=0;
void delay(unsigned int t) //延时子程序
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<10;j++)
;
}
void time0_init(void){ //定时计数0器初始化
EA=0;
TR0=0;
TMOD=0x01;
TH0=0xec;
TL0=0x73;
ET0=1;
TR0=1;
EA=1;
}
static void timer0_isr(void) interrupt TF0_VECTOR using 1 //定时计数0器中断函数
{
TR0=0;
TH0=0xec;
TL0=0x73;
TR0=1;
count_time++; //时钟计时程序
if(count_time>=199){
count_time=0;
second++;
if(second>=60){
second=0;
minute++;
if(minute>=60){
minute=0;
hour++;
if(hour>=24)hour=0;
}
}
}
if(delayed_hour==hour && delayed_minute==minute && second<4) P3_7=0;
else P3_7=1;
if(countdown_mark==1){ //倒计时程序
count_count++;
if(count_count>=199 && (count_second!=0|count_minute!=0|count_hour!=0)){
count_count=0;
count_second--;
if(count_second>=60){
count_second=59;
count_minute--;
if(count_minute>=60){
count_minute=59;
count_hour--;
if(count_hour>=100) count_hour=99;
}
}
}
if(count_second==0&&count_minute==0&&count_hour==0&&count_count<=12000) P3_7=0;
else P3_7=1;
if(count_count>=15000) count_count=14000;
}
if(stopwatch_mark==1){ //秒表程序
count_count++;
if(count_count>=2){
count_count=0;
count_second++;
if(count_second>=100){
count_second=0;
count_minute++;
if(count_minute>=60){
count_minute=0;
count_hour++;
if(count_hour>=60) count_hour=0;
}
}
}
}
}
unsigned char show_key (void){
unsigned char x=0,y=0;
switch (program){
case 0: P1&=senen_seg[second%10]; //时钟秒的个位
break;
case 1: if(count_time>=90) P1&=senen_seg[second%10]; //校正秒的个位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟秒的个位
else P1&=senen_seg[delayed_second%10];
break;
case 3: if(count_bit>=0) P1&=senen_seg[count_second%10];//倒计时秒的个位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_second%10]; //秒表秒的个位
break;
case 5: P1&=senen_seg[count_second%10]; //计数器个位
break;
}
P3_3=0;
delay(10);
if(P3_5==0){ //功能键1识别
key1_enter=1;
if(count<=254)count++;
}
if(P3_4==0) key2_enter=1; //功能键2识别
if(P3_2==0) key3_enter=1; //功能键3识别
P3_3=1;
P1|=0xff;
switch (program){
case 0: P1&=senen_seg[second/10]; //时钟秒的十位
break;
case 1: if(count_time>=90) P1&=senen_seg[second/10]; //校正秒的十位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟秒的十位
else P1&=senen_seg[delayed_second/10];
break;
case 3: if(count_bit>=1) P1&=senen_seg[count_second/10];//倒计时秒的十位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_second/10]; //秒表秒的十位
break;
case 5: P1&=senen_seg[count_second/10]; //计数器十位
break;
}
P3_1=0;
delay(10);
P3_1=1;
P1|=0xff;
switch (program){
case 0: P1&=senen_seg[minute%10]; //时钟分的个位
break;
case 1: if(count_time>=90) P1&=senen_seg[minute%10]; //校正分的个位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟分的个位
else P1&=senen_seg[delayed_minute%10];
break;
case 3: if(count_bit>=2) P1&=senen_seg[count_minute%10];//倒计时分的个位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_minute%10]; //秒表分的个位
break;
case 5: P1&=senen_seg[count_minute%10]; //计数器百位
break;
}
P3_2=0;
delay(10);
P3_2=1;
P1|=0xff;
switch (program){
case 0: P1&=senen_seg[minute/10]; //时钟秒的个位
break;
case 1: if(count_time>=90) P1&=senen_seg[minute/10]; //校正秒的个位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟秒的个位
else P1&=senen_seg[delayed_minute/10];
break;
case 3: if(count_bit>=3) P1&=senen_seg[count_minute/10];//倒计时秒的个位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_minute/10]; //秒表秒的个位
break;
case 5: P1&=senen_seg[count_minute/10]; //计数器千位
break;
}
P3_5=0;
delay(10);
P3_5=1;
P1|=0xff;
switch (program){
case 0: P1&=senen_seg[hour%10]; //时钟时的个位
break;
case 1: if(count_time>=90) P1&=senen_seg[hour%10]; //校正时的个位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟时的个位
else P1&=senen_seg[delayed_hour%10];
break;
case 3: if(count_bit>=4) P1&=senen_seg[count_hour%10];//倒计时时的个位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_hour%10]; //秒表时的个位
break;
case 5: P1&=senen_seg[count_hour%10]; //计数器万位
break;
}
P3_0=0;
delay(10);
if(P3_4==1 && key2_enter==1){
x=3; //确认功能键2识别,返回3
key2_enter=0;
}
P3_0=1;
P1|=0xff;
switch (program){
case 0: P1&=senen_seg[hour/10]; //时钟时的个位
break;
case 1: if(count_time>=90) P1&=senen_seg[hour/10]; //校正时的个位
break;
case 2: if(delayed_hour==24) P1=0xfe; //闹钟时的个位
else P1&=senen_seg[delayed_hour/10];
break;
case 3: if(count_bit>=5)P1&=senen_seg[count_hour/10]; //倒计时时的个位
else P1=0xff;
break;
case 4: P1&=senen_seg[count_hour/10]; //秒表时的个位
break;
case 5: P1&=senen_seg[count_hour/10]; //计数器万位
break;
}
P3_4=0;
delay(10);
if(P3_5==1 && key1_enter==1){ //确认功能键1识别,执行退出或进入下一个功能
if(count>=127) x=1; //确认功能键1为长按,返回1
else x=2; //确认功能键1为短按,返回2
key1_enter=0;
count=0;
}
if(P3_2==1 && key3_enter==1){
x=4; //确认功能键3识别,返回4
key3_enter=0;
}
P3_4=1;
P1|=0xff;
if(program<=1 && count_time>=100) P1&=0xff; //校时和显示功能时钟时冒号闪烁
if(program<=1 && count_time<=100) P1&=0x7f;
if(program==2) P1&=0x7f; //设置闹钟功能时冒号长亮
if(program==3) P1&=0xff; //设置倒计时功能时冒号长灭
if(program==4) P1&=0x7f; //设置秒表功能时冒号长亮
if(program==5) P1&=0xff; //设置计数时冒号长灭
y=x;
x=0;
return y;
}
void main(){ //主程序
P1=0xff;
P3=0xff;
time0_init();
while(1){
switch(program){
case 0: while(program==0){ //时钟菜单
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=1;
break;
}
}
break; //校时菜单
case 1: while(program==1){
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=2;
break;
case 3: hour++;
if(hour>=24)hour=0;
break;
case 4: minute++;
if(minute>=60)minute=0;
break;
}
}
break;
case 2: while(program==2){ //闹钟菜单
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=3;
break;
case 3: delayed_hour++;
if(delayed_hour>=25)delayed_hour=0;
break;
case 4: delayed_minute++;
if(delayed_minute>=60)delayed_minute=0;
break;
}
}
break;
case 3: while(program==3){ //倒计时菜单
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=4;
break;
case 3: count_bit++;
if(count_bit>=7)count_bit=0;
break;
case 4: switch(count_bit){
case 0: count_second+=1;
break;
case 1: count_second+=10;
break;
case 2: count_minute+=1;
break;
case 3: count_minute+=10;
break;
case 4: count_hour+=1;
break;
case 5: count_hour+=10;
break;
case 6: break;
}
if(count_hour>=100) count_hour-=100;
if(count_minute>=60) count_minute-=60;
if(count_second>=60) count_second-=60;
break;
}
if(count_bit==6) countdown_mark=1;
else countdown_mark=0;
}
break;
case 4: count_hour=0; //秒表菜单
count_minute=0;
count_second=0;
while(program==4){
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=5;
break;
case 3: stopwatch_mark=~stopwatch_mark;
break;
case 4: if(stopwatch_mark==0){
count_hour=0;
count_minute=0;
count_second=0;
}
break;
}
}
break; //计数器菜单
case 5: count_hour=0;
count_minute=0;
count_second=0;
while(program==5){
switch(show_key()){
case 0: break;
case 1: program=0;
break;
case 2: program=0;
break;
case 3: count_second++;
if(count_second>=100){
count_second=0;
count_minute++;
if(count_minute>=100){
count_minute=0;
count_hour++;
if(count_hour>=100)count_hour=0;
}
}
break;
case 4: count_hour=0;
count_minute=0;
count_second=0;
break;
}
if(P3_7==0){
while(P3_7==0) show_key();
count_second++;
if(count_second>=100){
count_second=0;
count_minute++;
if(count_minute>=100){
count_minute=0;
count_hour++;
if(count_hour>=100)count_hour=0;
}
}
}
}
break;
}
if(program!=3) countdown_mark=0;
if(program!=4) stopwatch_mark=0;
}
}
#include "stdioh"
#include "dosh"
//#include <windowsh>
#include <timeh>
#define S2M_M2H 60
struct N
{
int hour;
int minute;
int second;
} mytime = {0,0,0};
int main()
{
time_t rt;
struct tm t;
time ( &rt );
t = localtime ( &rt );
mytimehour=t->tm_hour;
mytimeminute = t->tm_min;
mytimesecond = t->tm_sec;
//1楼答得不错,你不会自己合直来么,我帮你合起来了
//以后我白天不能上网,晚上来答一两个就玩去了,这题如果可行,就采纳1楼的,我没功劳
for(; ; mytimesecond++)
{
if(mytimesecond==S2M_M2H)
{
mytimeminute++;
mytimesecond=0;
if(mytimeminute==S2M_M2H)
{
mytimehour++;
mytimeminute=0;
}
}
{
printf("\b\b\b\b\b\b\b\b%2d:%02d:%02d",mytimehour,mytimeminute,mytimesecond);
delay(1000);
}
}
return 0;
}
1、画一大一小两个圆,大的用蓝色填充、置于底层,小的用白色填充,然后把两个圆同心叠放,选择组合。
2、在幻灯片上插入文本框,添加时间字体。再ctrl+a全选,右键选择组合。
3、在自选图形中的“箭头总汇”中的“上箭头”做为表针,并更改填充颜色。将该箭头复制1个,并按图中位置放置,两个箭头一起选中,右键,组合在一起。
4、选中下面的箭头,双击设置最下的箭头,填充色设置为“无填充颜色”,线条颜色设置为“无线条颜色”,这样我们的时针就做好了。
5、将做好的时针放到表盘的中间。
6、选中时针,选择“自定义动画”,添加“动作效果”为:强调-->陀螺旋。
7、右键点击动画列表的“组合25”,选中“效果选项”
8、在计时标签下,设置如下:
开始:之前
延时:0秒
速度:12:00:00秒
(此处手动输入,注意冒号为英文输入法下面的符号)
重复:直到下一次点击
9、按上面同样的方法,用箭头分别作出分针和秒针,并叠放在一起,注意层次(钟面最底层,接着秒针,再分针,最上面时针)分针和秒针设置同样的动画,但分针效果选项下的速度输入“01:00:00秒”,秒针速度输入“01:00秒”,其他设置跟时针一样。
10、最终运行效果:
注:更多请关注办公软件教程
栏目,三联电脑办公群:189034526欢迎你的加入
用c++的话还是弄成类比较好的,下面是我的解决方法:
#include <ctime>
#include <cstring>
class Timer
{
private:
static const int size = 50;
std::time_t time;
std::tm localTime;
char Ascll[size];
char flag;
public:
Timer()
{
std::time(&time);
flag = 0;
}
~Timer() {}
const char printTime()
{
if(!flag)
{
localTime = std::localtime(&time);
std::strcpy(Ascll, std::asctime(&localTime));
}
flag++;
return Ascll;
}
double timeDiff(Timer time2) const
{
return difftime(time, time2 -> time);
}
} ;
int main()
{
Timer t1;
for(int i = 0; i < 10000; i++ )
{
cout << i;
}
Timer t2;
std::cout << t1printTime() << std::endl;
std::cout << t2printTime() << std::endl;
std::cout << t2timeDiff(&t1) << std::endl;
return 0;
}
#include<reg51h>
#define uchar unsigned char
uchar code ledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0-9
unsigned char sec=0,min=0,hour=0,scanled;
unsigned char key,time;
unsigned char disdat[6];
sbit K=P1^0;
void dischg()
{
disdat[0]=sec%10;
disdat[1]=sec/10;
disdat[2]=min%10;
disdat[3]=min/10;
disdat[4]=hour%10;
disdat[5]=hour/10;
}
void t0isr() interrupt 1 //秒计时
{
TR0=0;
TH0=0x3c;
TL0=0xb0;
TR0=1;
time++;
if(time==20)
{
time=0;
sec++;
if(sec>59)
{
sec=0;
min++;
if(min>59)
{
min=0;
hour++;
if(hour>23)hour=0;
}
}
}
dischg();
}
void t1isr() interrupt 3 //显示
{
TH1=0xec;
TL1=0x78;
switch(scanled)
{
case 0:
P2=0x01;
P0=~ledtab[disdat[5]];
break;
case 1:
P2=0x02;
P0=~ledtab[disdat[4]]&0x7f;
break;
case 2:
P2=0x04;
P0=~ledtab[disdat[3]];
break;
case 3:
P2=0x08;
P0=~ledtab[disdat[2]]&0x7f;
break;
case 4:
P2=0x10;
P0=~ledtab[disdat[1]];
break;
case 5:
P2=0x20;
P0=~ledtab[disdat[0]];
break;
default:break;
}
scanled++;
scanled%=6;
}
main()
{
TMOD=0x11;
TH0=0x3c;
TL0=0xb0;
TH1=0xec;
TL1=0x78;
EA=1;
TR1=1;
ET0=1;
ET1=1;
sec=0;
min=0;
hour=0;
scanled=0;
time=0;
key=0;
dischg();
while(1)
{
if(K==0)
{
while(K==0);
key++;
key%=2;
}
switch(key)
{
case 0:
TR0=1;
break;
case 1:
TR0=0;
break;
}
}
}
以上就是关于c语言时钟代码全部的内容,包括:c语言时钟代码、请教有AI89C51单片机的数字时钟的设计电路和程序,高分悬赏,邮箱13980836483@qq.com、C语言模拟动态时钟程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)