电子时钟C代码.docx
- 文档编号:30621167
- 上传时间:2023-08-18
- 格式:DOCX
- 页数:13
- 大小:15.74KB
电子时钟C代码.docx
《电子时钟C代码.docx》由会员分享,可在线阅读,更多相关《电子时钟C代码.docx(13页珍藏版)》请在冰豆网上搜索。
电子时钟C代码
#include
#include
#include
classCDateTime
{
private:
intyear;
intmonth;
intdays;
inthour;
intminute;
intsecond;
intwday;//星期
private:
intYearToDays(inty);
intDatesToDays();
intMonthToWeek();
intYearMonthToDay(inty,intm);
public:
CDateTime(inty=0,intm=0,intd=0,inth=0,intmt=0,ints=0)
{
year=y;
month=m;
days=d;
hour=h;
minute=mt;
second=s;
wday=DatesToWeek();
}
~CDateTime()
{}
voidNow();
intDatesToWeek();
CDateTimeLastMonth();
CDateTimeNextMonth();
voidLastTime();
voidInputYearMonth();
voidPrintMonth();
voidInputDates();
intWeek()
{
returnwday;
}
CDateTimeoperator+(constint&n);
CDateTimeoperator-(constint&n);
intoperator-(CDateTime&dt);
friendostream&operator<<(ostream&out,constCDateTime&c);
};
voidCDateTime:
:
Now()
{
time_tte;
tm*tp;
time(&te);
tp=localtime(&te);
year=tp->tm_year+1900;
month=tp->tm_mon+1;
days=tp->tm_mday;
hour=tp->tm_hour;
minute=tp->tm_min;
second=tp->tm_sec;
wday=tp->tm_wday;
}
CDateTimeCDateTime:
:
operator-(constint&n)
{
intm=n;
intmday;
CDateTimedt(*this);
mday=YearMonthToDay(dt.year,dt.month);
while(dt.days+m>mday)
{
dt.month--;
if(dt.month>1)
{
dt.year-=1;
dt.month=12;
}
m-=mday;
mday=YearMonthToDay(dt.year,dt.month);
}
dt.days+=m;
returndt;
}
CDateTimeCDateTime:
:
operator+(constint&n)
{
intm=n;
intmday;
CDateTimedt(*this);
mday=YearMonthToDay(dt.year,dt.month);
while(dt.days+m>mday)
{
dt.month++;
if(dt.month>12)
{
dt.year+=1;
dt.month=1;
}
m-=mday;
mday=YearMonthToDay(dt.year,dt.month);
}
dt.days+=m;
returndt;
}
intCDateTime:
:
operator-(CDateTime&dt)
{
inty=dt.year;
intm=dt.month;
intt1=dt.DatesToDays();
intt2=DatesToDays();
return(t2-t1);
}
intCDateTime:
:
YearToDays(inty)
{
intd=0;
inti=(y-1900)*365;
while(y>1900)
{
if((y-1)%4==0&&(y-1)%100!
=0||(y-1)%400==0)
++d;
--y;
}
return(d+i);
}
intCDateTime:
:
YearMonthToDay(inty,intm)
{
intmday=0;
switch(m)
{
case1:
case3:
case5:
case7:
case8:
case10:
case12:
mday=31;
break;
case2:
if(y%4==0&&y%100!
=0||y%400==0)
mday=29;
else
mday=28;
break;
case4:
case6:
case9:
case11:
mday=30;
break;
default:
mday=0;
}
returnmday;
}
intCDateTime:
:
DatesToDays()
{
inttotal=0;
while(month>1)
{
total+=YearMonthToDay(year,month-1);
--month;
}
intnum=YearToDays(year);
return(total+num+days);
}
intCDateTime:
:
DatesToWeek()
{
intw=DatesToDays();
return(w%7);
}
CDateTimeCDateTime:
:
NextMonth()
{
Datetmp(*this);
if(month==12)
{
tmp.month=1;
++tmp.year;
}
else
++tmp.month;
returntmp;
}
CDateTimeCDateTime:
:
LastMonth()
{
CDateTimetmp(*this);
if(month==1)
{
tmp.month=12;
--tmp.year;
}
else
--tmp.month;
returntmp;
}
voidCDateTime:
:
LastTime()
{
}
voidCDateTime:
:
InputDates()
{
cout<<"输入年:
"< cin>>year; while(year<=1900||year>=2099) { cout<<"重新输入年: "< cin>>year; } cout<<"输入月: "< cin>>month; while(month<=0||month>12) { cout<<"重新输入月: "< cin>>month; } cout<<"输入天"< cin>>days; } voidCDateTime: : InputYearMonth() { cout<<"输入年: "< cin>>year; while(year<=1900||year>=2099) { cout<<"重新输入年: "< cin>>year; } cout<<"输入月: "< cin>>month; while(month<=0||month>12) { cout<<"重新输入月: "< cin>>month; } } intCDateTime: : MonthToWeek() { intt=DatesToDays(); return(t-days)%7; } voidCDateTime: : PrintMonth() { inty=year; intm=month; cout<<"------------------------------"< cout<<"******************************"< cout<<""<<"日"<<""<<"一"<<""<<"二"<<""<<"三"<<""<<"四"<<""<<"五"<<""<<"六"< intw=MonthToWeek(); if(w! =6) for(inti=0;i<=w;i++) cout<<""; intk=YearMonthToDay(y,m); for(intj=1;j<=k;++j) { cout< (2)<<""< (2)< if((j+w+1)%7==0) cout< } cout< cout<<"------------------------------"< cout<<"******************************"< } ostream&operator<<(ostream&out,constCDateTime&c) { out< out< "< "< returnout; } voidmain() { inti,j,k; CDateTimed1,d2,d3,d4,d5; cout<<"欢迎使用日历"< cout<<"请选择: "< cout<<"1.查询月历"< cout<<"2.查询星期"< cout<<"3.倒计时"< cout<<"0退出"< cin>>i; while(i! =0) { switch(i) { case0: break; case1: d1.InputYearMonth(); d1.PrintMonth(); cout<<"1.上一个月"< cout<<"2.下一个月: "< cout<<"0.返回: "< cin>>j; while(j! =0) { switch(j) { case0: break; case1: d2=d1.LastMonth(); d2.PrintMonth(); break; case2: d3=d1.NextMonth(); d3.PrintMonth(); break; default: while(j<0||j>2) { cout<<"请重新选择"< cin>>j; } } cout<<"0返回"< cin>>j; } break; case2: d4.InputDates(); k=d4.DatesToWeek(); cout<<"星期"< break; case3: d5.InputDates(); //d5.LastTime(); break; default: while(i<0||i>3) { cout<<"请重新输入"< cin>>i; } } cout<<"1.查询月历"< cout<<"2.查询星期"< cout<<"3.倒计时"< cout<<"0退出"< cin>>i; } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 电子 时钟 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)