C++编写一个时钟程序,模拟数字时钟的显示。

C++编写一个时钟程序,模拟数字时钟的显示。,第1张

程序代码如下:

#include<iostream>

#include<windows.h>

using namespace std

class Clock{

public:

Clock(short h=0,short m=0,short s=0):h(h),m(m),s(s){

}

void displayTime()

private:

short h

short m

short s

}void Clock::displayTime(){

while(true){

cout<<h<<':'<<m<<':'<<s<<" "

Sleep(1000)

cout<<'\r'

if(!(s=++s%60))

if(!(m=++m%60))

h=++h%24

}

} int main()

{

Clock A(23,59,55)

A.displayTime()

return 0

}

扩展资料:

定义一个日期类,包括年、月、日三个成员变量,显示日期的方法如下:

publicclassDemo{

publicstaticvoidmain(String[]args){

Datedate1=newDate(1994,5,22)

date1.showInfo()

Datedate2=newDate()

date2.year=1995

date2.month=6

date2.day=29

date2.showInfo()

}

}

//日期类:

publicclassDate{

intyear

intmonth

intday

//构造方法

publicDate(intyear,intmonth,intday){

this.year=year

this.month=month

this.day=day

}

publicDate(){

}

publicvoidshowInfo(){

System.out.println(year+"年"+month+"月"+day+"日")

}

}

/*实验名称:定时中断0

功能:利用定时器,产生秒计数器,使数码管显示时间和年月日,还有跑表

05-02-01

*/

#include <reg51.h>

//LCD的口线

sbit E=P3^5

sbit RW=P3^6

sbit RS=P3^7

//8字LED的连接方法

//P0.7,P0.6,P0.5,P0.4,P0.3,P0.2,P0.1,P0.0

//G, F A E B H C D

sbit l1=P2^7

sbit l2=P2^6

sbit l3=P2^5

sbit l4=P2^4

sbit l5=P2^3

sbit l6=P2^2

sbit l7=P2^1

sbit l8=P2^0

sbit j1=P1^1

sbit j2=P1^2

unsigned char code ledcode[]={0x84,0xf5,0x46,0x54,0x35,0x1c,0x0c,0xd5,

0x04,0x14,0x05,0x2c,0x8e,0x64,0x0e,0x0f}//数码管段码表

unsigned char ms,s,min,hour,t0100ms,t0101ms,t0sm,s0,s1,mi0,mi1,h0,h1,dsm,dsh,dsm0,dsm1,dsh0,dsh1

unsigned int year,mouch,date,y3,y2,y1,y0,m1,m0,d1,d0,leap,bz,bz1,bz0,pb0,pb1,pb2,pb00,pb10,pb20,pb,cs

void time0(void)

void delay()

void main(void)

{

E=0 //取消LCD对LED的影响

RW=0

RS=1

year=2009, mouch=10, date=18,leap=0

y3=0, y2=0, y1=0, y0=0

m1=0, m0=0, d1=0, d0=0

bz=0,bz1=0,bz0=0

pb0=0,pb1=0,pb2=0,cs=1

ms=0,min=30,hour=12,s=50

s0=0x00,s1=0x00

mi0=0,mi1=0,h0=0,h1=0

t0100ms=0x14

t0101ms=0x14

t0sm=0x01 //从第一个LED开始显示

TH0=0xee

TL0=0x00

TMOD=0x01

TR0=1

IE=0x82

while(1)

{ if((year%4==0&&year%100!=0)||(year%400==0)) //判断是平年还是闰年

{leap=1}

else

{leap=2}

do

{

if(j1==0){delay(), bz++}

if(bz==8){bz=0}

switch(bz)

{ case 0:{ bz1=0} break

case 1:{ bz1=0if(j2==0){delay(), min++}} break

case 2:{ bz1=0if(j2==0){delay(), hour++}} break

case 3:{ bz1=1if(j2==0){delay(), date++}} break

case 4:{ bz1=1if(j2==0){delay(),mouch++}} break

case 5:{ bz1=1if(j2==0){delay(), year++}} break

case 6:{ bz1=2if(j2==0){delay(), dsm++}} break

case 7:{ bz1=2if(j2==0){delay(), dsh++}} break

default:break}

}

while(bz)

do

{

if(j2==0) {delay(),bz0++}

if(bz0==5){bz0=0}

switch(bz0)

{ case 0:{ bz1=0} break

case 1:{ bz1=1if(j1==0){delay(),bz0=0}} break

case 2:{ bz1=3,pb=0,pb0=0,pb1=0,pb2=0} break

case 3:{ bz1=3if(j1==0){cs=2,pb00=pb0,pb10=pb1,pb20=pb2}} break

case 4:{ bz1=3if(j1==0){pb=1}} break

// case 2:{ bz1=0if(j2==0){delay(), hour++}} break

// case 3:{ bz1=1if(j2==0){delay(), date++}} break

// case 4:{ bz1=1if(j2==0){delay(),mouch++}} break

// case 5:{ bz1=1if(j2==0){delay(), year++}} break

default:break}

}

while(bz0)

}

}

////////////////////////////////////

void delay()

{

unsigned int i,j,k

for( i=10i>0i--)

for( j=20j>0j--)

for(k=100k>0k--)

}

///////////////////////////////////////

void time0(void) interrupt 1

{

TH0=0xee,TL0=0x00

t0100ms--

if(t0100ms==0x00){ t0100ms=20,ms++

if(ms==10) { ms=0,s++

if(s==60){ s=0,min++,m0=6

if(min==60) { min=0,hour++

if(hour==24) { hour=0,date++

//*******************************************************************************

switch(mouch)

{ case 1:{if(date==32){date=0,mouch++}}break

case 2:{if(leap==1&&date==30){date=0,mouch++}else{if(leap==2&&date==29){date=0,mouch++}}}break

case 3:{if(date==32){date=0,mouch++}}break

case 4:{if(date==31){date=0,mouch++}}break

case 5:{if(date==32){date=0,mouch++}}break

case 6:{if(date==31){date=0,mouch++}}break

case 7:{if(date==32){date=0,mouch++}}break

case 8:{if(date==32){date=0,mouch++}}break

case 9:{if(date==31){date=0,mouch++}}break

case 10:{if(date==32){date=0,mouch++}}break

case 11:{if(date==31){date=0,mouch++}}break

case 12:{if(date==32){date=0,mouch++}}break

case 13:{mouch=1,year++}break

default: break }

}

}

}

}

}

if(min==60) {min=0}

if(hour==24){hour=0}

if((mouch==1||mouch==3||mouch==5||mouch==7||mouch==8||mouch==10||mouch==12)&&date==32){date=0}

if((mouch==4||mouch==6||mouch==9||mouch==11)&&date==31){date=0}

if(mouch==2&&leap==1&&date==30){date=0}

if(mouch==2&&leap==2&&date==29){date=0}

if(mouch==13){mouch=1}

s0=s%10,s1=s/10%10

mi0=min%10,mi1=min/10%10

h0=hour%10,h1=hour/10%10

d0=date%10,d1=date/10%10

m0=mouch%10,m1=mouch/10%10

y0=year%10,y1=year/10%10,y2=year/100%10,y3=year/1000

dsm0=dsm%10,dsm1=dsm/10%10

dsh0=dsh%10,dsh1=dsh/10%10

if(dsm==60){dsm=0}

if(dsh==24){dsh=0}

//**********************************************************************

if(bz0==4)

{}

else

if(bz0==3)

{ TH0=0xee,TL0=0x00

t0101ms--

if(t0101ms==0x00){ t0101ms=20,pb0++

if( pb0==10) {pb0=0,pb1++

if(pb1==10) {pb1=0,pb2++

if(pb2==10) {pb2=0}

}

}

}

}

/////////////////////////////////////////////////////////////////////////

t0sm++

if(t0sm==9){t0sm=1}

P0=0xff

if(bz1==0)

{switch(t0sm)

{ case 1:{P2=0xff,l1=0P0=ledcode[ms]} break

case 2:{P2=0xff,l2=0P0=ledcode[s0]&0xfb} break

case 3:{P2=0xff,l3=0P0=ledcode[s1]} break

case 4:{P2=0xff,l4=0P0=ledcode[mi0]&0xfb}break

case 5:{if(bz==1){P2=0xff,l5=0P0=ledcode[mi1]&0xfb}else{P2=0xff,l5=0P0=ledcode[mi1]}}break

case 6:{P2=0xff,l6=0P0=ledcode[h0]&0xfb} break

case 7:{if(bz==2){P2=0xff,l7=0P0=ledcode[h1]&0xfb}else{P2=0xff,l7=0P0=ledcode[h1]}}break

case 8:{P2=0xff,l8=0P0=ledcode[bz]&0xfb} break

default:break}

}

if(bz1==1)

{switch(t0sm)

{ case 1:{if(bz==3){P2=0xff,l1=0P0=ledcode[d0]&0xfb}else{P2=0xff,l1=0P0=ledcode[d0]}} break

case 2:{if(bz==3){P2=0xff,l2=0P0=ledcode[d1]&0xfb}else{P2=0xff,l2=0P0=ledcode[d1]}} break

case 3:{P2=0xff,l3=0P0=ledcode[m0]&0xfb} break

case 4:{if(bz==4){P2=0xff,l4=0P0=ledcode[m1]&0xfb}else{P2=0xff,l4=0P0=ledcode[m1]}} break

case 5:{P2=0xff,l5=0P0=ledcode[y0]&0xfb} break

case 6:{if(bz==5){P2=0xff,l6=0P0=ledcode[y1]&0xfb}else{P2=0xff,l6=0P0=ledcode[y1]}} break

case 7:{if(bz==5){P2=0xff,l7=0P0=ledcode[y2]&0xfb}else{P2=0xff,l7=0P0=ledcode[y2]}} break

case 8:{if(bz==5){P2=0xff,l8=0P0=ledcode[y3]&0xfb}else{P2=0xff,l8=0P0=ledcode[y3]}} break

default:break}

}

if(bz1==2)

{switch(t0sm)

{ case 1:{P2=0xff} break

case 2:{P2=0xff} break

case 3:{P2=0xff} break

case 4:{if(bz!=6){P2=0xff,l4=0P0=ledcode[dsm0]}else{P2=0xff,l4=0P0=ledcode[dsm0]&0xfb}}break

case 5:{if(bz!=6){P2=0xff,l5=0P0=ledcode[dsm1]}else{P2=0xff,l5=0P0=ledcode[dsm1]&0xfb}}break

case 6:{P2=0xff,l6=0P0=ledcode[dsh0]&0xfb} break

case 7:{if(bz!=7){P2=0xff,l7=0P0=ledcode[dsh1]}else{P2=0xff,l7=0P0=ledcode[dsh1]&0xfb}}break

case 8:{P2=0xff} break

default:break}

}

if(bz1==3)

{switch(t0sm)

{ case 1:{if(pb==1){P2=0xff,l1=0P0=ledcode[pb00]}else{P2=0xff,l1=0P0=ledcode[pb0]}}break

case 2:{if(pb==1){P2=0xff,l2=0P0=ledcode[pb10]&0xfb}else{P2=0xff,l2=0P0=ledcode[pb1]&0xfb}} break

case 3:{if(pb==1){P2=0xff,l3=0P0=ledcode[pb2]}else{P2=0xff,l3=0P0=ledcode[pb2]}} break

case 4:{P2=0xff}break

case 5:{P2=0xff,l5=0P0=ledcode[cs]}break

case 6:{P2=0xff}break

case 7:{P2=0xff}break

case 8:{P2=0xff}break

default:break}

}

}

#include "reg52.h"

#define uint unsigned int

#define uchar unsigned char

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

uchar shi,fen,miao

uchar time

void delay(uint x)

{

uint y

for(x>0x--)

{

for(y=0y<124y++)

}

}

void display(uchar shi,uchar fen,uchar miao)

{

P2=0 //位码

P0=(tab[shi/10]) //段码

delay(2)

P2=1

P0=(tab[shi%10])

delay(2)

P2=2 //位码

P0=0x40 //段码

delay(2)

P2=3 //位码

P0=(tab[fen/10]) //段码

delay(2)

P2=4

P0=(tab[fen%10])

delay(2)

P2=5 //位码

P0=0x40 //段码

delay(2)

P2=6 //位码

P0=(tab[miao/10]) //段码

delay(2)

P2=7

P0=(tab[miao%10])

delay(2)

}

void main()

{

TMOD=0x01

TH0=(65536-50000)/256

TL0=(65536-50000)%256

EA=1

ET0=1

TR0=1

while(1)

{

if(time==20)

{

time=0

miao++

if(miao==60)

{

miao=0

fen++

if(fen==60)

{

fen=0

shi++

if(shi==24)

shi=0

}

}

}

display(shi,fen,miao)

}

}

void timer0() interrupt 1

{

TH0=(65536-50000)/256

TL0=(65536-50000)%256

time++

}

/*还有什么不明白继续追加*/


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

原文地址: http://outofmemory.cn/yw/11833587.html

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

发表评论

登录后才能评论

评论列表(0条)

保存