c++时间程序.docx

上传人:b****8 文档编号:10019927 上传时间:2023-02-08 格式:DOCX 页数:17 大小:17.01KB
下载 相关 举报
c++时间程序.docx_第1页
第1页 / 共17页
c++时间程序.docx_第2页
第2页 / 共17页
c++时间程序.docx_第3页
第3页 / 共17页
c++时间程序.docx_第4页
第4页 / 共17页
c++时间程序.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

c++时间程序.docx

《c++时间程序.docx》由会员分享,可在线阅读,更多相关《c++时间程序.docx(17页珍藏版)》请在冰豆网上搜索。

c++时间程序.docx

c++时间程序

//闽南师范大学-数学与统计学院-09/10金融班-吴童鞋

#include

#include

usingnamespacestd;

intmax[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

//classDate;

classTime

{

public:

inthour;

intminute;

intsec;

Time();

Time(inth,intm,ints){hour=h;minute=m;sec=s;}

voidget_time();

//voidshow_time(Date&);

intpd_time(int,int,int);

};

classDate

{

public:

intyear;

intmonth;

intday;

Date();

Date(inty,intm,intd){year=y;month=m;day=d;}

voidget_date();

voidshow_date();

intpd_date(int,int,int);/*友元函数*/

//friendvoidTime:

:

show_time(Date&);

};

/*由基类Time派生,用来放计算结果几天几小时几分几秒的类*/

classJs:

publicTime

{

public:

inttian;

Js():

Time(){tian=0;}

Js(intt,inth,intm,ints):

Time(h,m,s){tian=t;}

voidget_js();

voidshow_js(){cout<

};

/*由基类Time和基类Date派生的类Timedate,重载运算符函数作为成员函数*/

classTimedate:

publicDate,publicTime

{

public:

Timedate():

Date(),Time(){}

Timedate(inty,intmon,intd,inth,intmin,ints):

Date(y,mon,d),Time(h,min,s){}

voidshow_timedate(){printf("%04d-%02d-%02d__%02d:

%02d:

%02d",year,month,day,hour,minute,sec);}

Timedateoperator+(Jsa);

Timedateoperator-(Jsb);

Jsoperator-(Timedatetd);

};

/*时间的成员函数*/

Time:

:

Time()

{

hour=0;

minute=0;

sec=0;

}

voidTime:

:

get_time()

{

inth,m,s;

cout<<"请输入时、分、秒:

"<

cin>>h>>m>>s;

while(!

pd_time(h,m,s))

{

cout<<"输入有误,正确格式为:

00:

00:

00-23:

59:

59。

"<

cout<<"请再一次输入时、分、秒:

"<

cin>>h>>m>>s;

}

hour=h;

minute=m;

sec=s;

}

/*voidTime:

:

show_time(Date&d)

{

printf("%04d-%02d-%02d",d.year,d.month,d.day);

printf("%02d:

%02d:

%02d\n",hour,minute,sec);

}*/

intTime:

:

pd_time(inth,intm,ints)/*判断时间*/

{

if(h<=23&&m<=59&&s<=59)return1;

elsereturn0;

}

/*年月日的成员函数*/

Date:

:

Date()

{

year=0;

month=0;

day=0;

}

voidDate:

:

get_date()

{

inty,x,z;

cout<<"请输入年、月、日:

"<

cin>>y>>x>>z;

while(!

pd_date(y,x,z))

{

cout<<"请再一次输入年、月、日:

"<

cin>>y>>x>>z;

}

year=y;

month=x;

day=z;

}

voidDate:

:

show_date()

{

cout<

}

intDate:

:

pd_date(inty,intx,intz)/*判断日期*/

{

if(y%4==0&&y%100!

=0||y%400==0)max[2]=29;elsemax[2]=28;

if(x>12||x<=0)

{

cout<<"月份的格式输入有误,正确格式为:

1-12。

"<

return0;

}

if(x<13&&z>max[x])

{

cout<<"日期的格式输入有误"<

1-"<

return0;

}

elsereturn1;

}

//派生类Js的成员函数

voidJs:

:

get_js()

{

intx1,x2,x3,x4;

cout<<"请输入要计算的天数、时数、分钟数及秒数"<

cin>>x1>>x2>>x3>>x4;

tian=x1;hour=x2;minute=x3;sec=x4;

}

/*派生类timedate的成员函数;重载运算符函数*/

TimedateTimedate:

:

operator+(Jsa)/*加法:

一段时间后的日期是d*/

{

Timedatetd;

td.sec=sec+a.sec;

if(td.sec>=60){td.sec=td.sec-60;td.minute=td.minute+1;}

td.minute=td.minute+minute+a.minute;

if(td.minute>=60){td.minute=td.minute-60;td.hour=td.hour+1;}

td.hour=td.hour+hour+a.hour;

if(td.hour>=24){td.hour=td.hour-24;td.day=td.day+1;}

td.day=td.day+day+a.tian;

td.month=month;

td.year=year;

if(td.year%4==0&&td.year%100!

=0||td.year%400==0)max[2]=29;elsemax[2]=28;

while(td.day>max[td.month])

{

if(td.year%4==0&&td.year%100!

=0||td.year%400==0)max[2]=29;elsemax[2]=28;

td.day=td.day-max[td.month];

td.month++;

if(td.month==13)

{

td.month=1;

td.year++;

}

}

returntd;

}

TimedateTimedate:

:

operator-(Jsb)/*减法:

一段时间前的日期是d*/

{

Timedatetd;

td.sec=sec-b.sec;

if(td.sec<0){td.sec=td.sec+60;td.minute=td.minute-1;}

td.minute=td.minute+minute-b.minute;

if(td.minute<=0){td.minute=td.minute+60;td.hour=td.hour-1;}

td.hour=td.hour+hour-b.hour;

if(td.hour<=0){td.hour=td.hour+24;td.day=td.day-1;}

td.day=td.day+day-b.tian;

td.month=month;

td.year=year;

while(td.day<=0)

{

td.month--;

if(td.month==0)

{

td.month=12;

td.year--;

}

if(td.year%4==0&&td.year%100!

=0||td.year%400==0)max[2]=29;elsemax[2]=28;

td.day=td.day+max[td.month];

}

returntd;

}

JsTimedate:

:

operator-(Timedatetd)/*减法:

2个时间点相距多长时间*/

{

Jsj;

inti;

inta1=0;inta2=0;inta3=0;

if(year

{

for(i=year+1;i

{

if(i%4==0&&i%100!

=0||i%400==0)a1=a1+366;

elsea1=a1+365;

}

if(year%4==0&&year%100!

=0||year%400==0)max[2]=29;elsemax[2]=28;

for(i=month+1;i<=12;i++)

{

a2=a2+max[i];

}

a2=a2+(max[month]-day-1);

if(td.year%4==0&&td.year%100!

=0||td.year%400==0)max[2]=29;elsemax[2]=28;

for(i=td.month-1;i>=1;i--)

{

a3=a3+max[i];

}

a3=a3+td.day;

j.sec=(60-sec)+td.sec;

if(j.sec>=60){j.sec=j.sec-60;j.minute=j.minute+1;}

j.minute=j.minute+(60-minute-1)+td.minute;

if(j.minute>=60){j.minute=j.minute-60;j.hour=j.hour+1;}

j.hour=j.hour+(24-hour-1)+td.hour;

if(j.hour>=24){j.hour=j.hour-24;j.tian=j.tian+1;}

}

if(year>td.year)

{

for(i=td.year+1;i

{

if(i%4==0&&i%100!

=0||i%400==0)a1=a1+366;

elsea1=a1+365;

}

if(td.year%4==0&&td.year%100!

=0||td.year%400==0)max[2]=29;elsemax[2]=28;

for(i=td.month+1;i<=12;i++)

{

a2=a2+max[i];

}

a2=a2+(max[td.month]-td.day-1);

if(year%4==0&&year%100!

=0||year%400==0)max[2]=29;elsemax[2]=28;

for(i=month-1;i>=1;i--)

{

a3=a3+max[i];

}

a3=a3+day;

j.sec=(60-td.sec)+sec;

if(j.sec>=60){j.sec=j.sec-60;j.minute=j.minute+1;}

j.minute=j.minute+(60-td.minute-1)+minute;

if(j.minute>=60){j.minute=j.minute-60;j.hour=j.hour+1;}

j.hour=j.hour+(24-td.hour-1)+hour;

if(j.hour>=24){j.hour=j.hour-24;j.tian=j.tian+1;}

}

if(year==td.year)

{

while(year%4==0&&year%100!

=0||year%400==0)

{

max[2]=29;

}

if(month

{

for(i=month+1;i

{

a2=a2+max[i];

}

a3=a3+max[month]-day-1+td.day;

j.sec=(60-sec)+td.sec;

if(j.sec>=60){j.sec=j.sec-60;j.minute=j.minute+1;}

j.minute=j.minute+(60-minute-1)+td.minute;

if(j.minute>=60){j.minute=j.minute-60;j.hour=j.hour+1;}

j.hour=j.hour+(24-hour-1)+td.hour;

if(j.hour>=24){j.hour=j.hour-24;j.tian=j.tian+1;}

}

if(month>td.month)

{

for(i=td.month+1;i

{

a2=a2+max[i];

}

a3=a3+max[td.month]-td.day-1+day;

j.sec=(60-td.sec)+sec;

if(j.sec>=60){j.sec=j.sec-60;j.minute=j.minute+1;}

j.minute=j.minute+(60-td.minute-1)+minute;

if(j.minute>=60){j.minute=j.minute-60;j.hour=j.hour+1;}

j.hour=j.hour+(24-td.hour-1)+hour;

if(j.hour>=24){j.hour=j.hour-24;j.tian=j.tian+1;}

}

if(month==td.month)

{

if(day

{

a3=td.day-day-1;

j.sec=(60-sec)+td.sec;

if(j.sec>=60){j.sec=j.sec-60;j.minute=j.minute+1;}

j.minute=j.minute+(60-minute-1)+td.minute;

if(j.minute>=60){j.minute=j.minute-60;j.hour=j.hour+1;}

j.hour=j.hour+(24-hour-1)+td.hour;

if(j.hour>=24){j.hour=j.hour-24;j.tian=j.tian+1;}

}

if(day>td.day)

{

a3=day-td.day-1;

j.sec=(60-td.sec)+sec;

if(j.sec>=60){j.sec=j.sec-60;j.minute=j.minute+1;}

j.minute=j.minute+(60-td.minute-1)+minute;

if(j.minute>=60){j.minute=j.minute-60;j.hour=j.hour+1;}

j.hour=j.hour+(24-td.hour-1)+hour;

if(j.hour>=24){j.hour=j.hour-24;j.tian=j.tian+1;}

}

if(day==td.day)

{

if(hour>td.hour)

{

j.sec=sec-td.sec;

if(j.sec<0){j.sec=j.sec+60;j.minute=j.minute-1;}

j.minute=j.minute+minute-td.minute;

if(j.minute<0){j.minute=j.minute+60;j.hour=j.hour-1;}

j.hour=j.hour+hour-td.hour;

}

if(hour

{

j.sec=td.sec-sec;

if(j.sec<0){j.sec=j.sec+60;j.minute=j.minute-1;}

j.minute=j.minute+td.minute-minute;

if(j.minute<0){j.minute=j.minute+60;j.hour=j.hour-1;}

j.hour=j.hour+td.hour-hour;

}

if(hour==td.hour)

{

if(minute>td.minute)

{

j.sec=sec-td.sec;

if(j.sec<0){j.sec=j.sec+60;j.minute=j.minute-1;}

j.minute=j.minute+minute-td.minute;

}

if(minute

{

j.sec=td.sec-sec;

if(j.sec<0){j.sec=j.sec+60;j.minute=j.minute-1;}

j.minute=j.minute+td.minute-minute;

}

if(minute==td.minute)

{

if(sec>=td.sec)j.sec=sec-td.sec;

elsej.sec=td.sec-sec;

j.minute=0;

}

j.hour=0;

}

j.tian=0;

}

}

}

j.tian=j.tian+a1+a2+a3;

returnj;

}

/*主函数*/

intmain()

{

Jsj1,j2;

Timedatetd1,td2,tda,tdb;

cout<<"1、计算某个时间点加上一段时间后是什么时候;"<

cout<<"时间点a:

";td1.get_date();/*调用基类Date中的函数*/

cout<<"时间点a:

";td1.get_time();/*调用基类Time中的函数*/

j1.get_js();

system("cls");

cout<<"1、计算某个时间点加上一段时间后是什么时候;"<

cout<<"时间点a为:

";td1.show_timedate();cout<<";"<

cout<<"*******************************************************"<

tda=td1+j1;

cout<<"";td1.show_timedate();cout<

cout<<"+";j1.show_js();cout<

cout<<"——————————————";cout<

cout<<"=";tda.show_timedate();cout<

cout<<"*******************************************************"<

cout<<"2、计算某个时间点减去一段时间后是什么时候;"<

cout<<"时间点a:

";td1.get_date();/*调用基类Date中的函数*/

cout<<"时间点a:

";td1.get_time();/*调用基类Time中的函数*/

j1.get_js();

system("cls");

cout<<"2、计算某个时间点减去一段时间后是什么时候;"<

cout<<"时间点a为:

";td1.show_timedate();cout<<";"<

cout<<"*******************************************************"<

tda=td1-j1;

cout<<"";td1.show_timedate();cout<

cout<<"-";j1.show_js();cout<

cout<<"——————————————";cout<

cout<<"=";tda.show_timedate();cout<

cout<<"*******************************************************"<

cout<<"3、计算2个时间点相差多少时间。

"<

cout<<"时间点a:

";td1.get_date();/*调用基类Date中的函数*/

cout<<"时间点a:

";td1.get_time();/*调用基类Time中的函数*/

cout<<"时间点b:

";td2.get_date();/*调用基类Date中的函数*/

cout<<"时间点b:

";td2.get_time();/*调用基类Time中的函数*/

system("cls");

cout<<"3、计算2个时间点相差多少时间。

"<

cout<<"时间点a为:

";td1.show_timedate();cout<<";";

cout<<"时间点b为:

";td2.show_timedate();cout<

cout<<"*******************************************************"<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1