C++,C++......C++高手请进,兄弟姐妹们帮帮忙啊,急啊...........感激不尽! 20
创建2005.8.21和2008.8.8两个日期,并计算中间相隔的天数,前者加上300天会是什么日期?后者减去300天会是什么日期? 谢谢! 帮帮忙吧。就20分,给完了。 展开
#include <iostream>
using namespace std;
class Date{/*日期类*/
public :
int year,month,day;
void set(int m=1,int d=1,int y=1900)
{
year=y;
month=m;
day=d;
}
inline int operator-(const Date &b)/*‘-’操作符重载,用于计算两个日期之差*/
{
int days=0;
if (year==b.year&&month==b.month ) {
days=day-b.day;
days=days<0?-days:days;
}
else if (year==b.year) {
int ml,mh;
mh=month>b.month ?(ml=b.month,month):(ml=month,b.month) ;
for (int i=ml; i<mh; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days-=ml==month?day:b.day;
days+=mh==b.month?b.day :day;
}
else{
int yl,yh,ml,mh;
yh=year>b.year ?(yl=b.year,year):(yl=year,b.year) ;
for (int i=yl+1; i<yh; i++) {
if (i%4==0&&i%100||i%400==0)days+=366;
else days+=365;
}
ml=yl==year?month:b.month;
mh=yh==b.year?b.month:month;
for (; ml<=12; ml++) {
switch (ml) {
default:
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days-=yl==year?day:b.day;
for (--mh; mh>=1; mh--) {
switch (mh) {
default:
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days+=yh==b.year?b.day:day;
}
return days;
}
inline bool operator>(const Date &a)/*'>'操作符重载,用于判断日期的先后*/
{
if (year==a.year &&month==a.month ) {
return day>a.day;
}
else if (year==a.year ) {
return month>a.month ;
}
else return year>a.year ;
}
inline Date operator+(int days)/*'+'操作符重载,用于计算days天之后的日期*/
{
for (; days>=0; --days) {
if (day==29&&month==2&&!(year%4==0&&year%100||year%400==0)) {
++month;
day=1;
}
else if (day==30&&month==2&&(year%4==0&&year%100||year%400==0)) {
++month;
day=1;
}
else if (day==31) {
switch (month) {
case 4:
case 6:
case 9:
case 11:month++;day=1;
}
}
else if (day==32) {
month++;
day=1;
if (month==13) {
month=1;
year++;
}
}
if (days) day++;
}//for
return *this;
}
inline Date operator-(int days)//'-'操作符重载,用于计算days天之后的日期
{
for (; days>0; days--)
{
day--;
if ((day==0)&&((month-1)==2)&&!(year%4==0&&year%100||year%400==0))
{
month=2;
day=28;
}
else if ((day==0)&&((month-1)==2)&&(year%4==0&&year%100||year%400==0))
{
month=2;
day=29;
}
else if (day==0)
{
switch (month)
{
case 1:month--;day=31;break;
case 4:month--;day=31;break;
case 6:month--;day=31;break;
case 9:month--;day=31;break;
case 11:month--;day=31;break;
// case 3:month--;day=29;break;
case 5:month--;day=31;break;
case 7:month--;day=30;break;
case 8:month--;day=31;break;
case 10:month--;day=31;break;
case 12:month--;day=31;break;
case 2:month--;day=31;break;
}
if (month==0)
{
month=12;
year--;
}
}
}//for
return *this;
}
};
ostream& operator<<(ostream &a,Date &b)//*输出操作符重载,用于输出指定格式的日期
{
a<<b.year <<"年"<<b.month <<"月"<<b.day <<"日";
return a;
};
int main(void)/*调用实例*/
{
Date a,b;
a.set(8,21,2005);/*设置为1900-1-1*/
b.set(3,15,2001);/*设置为2009-1-1*/
int t=b-a;/*求两个日期之差*/
cout<<"a日期为:"<<a<<endl;
cout<<"300天后为:";
cout<<a+300<<endl;
cout<<"b日期为:"<<b<<endl;
cout<<"300天前为:";
cout<<b-300<<endl;
cout<<"两个日期之差是:"<<endl;
cout<<t<<"天"<<endl;
return 0;
}
2009-06-25
using namespace std;
class Date{/*日期类*/
public :
int year,month,day;
void set(int m=1,int d=1,int y=1900)
{
year=y;
month=m;
day=d;
}
inline int operator-(const Date &b)/*‘-’操作符重载,用于计算两个日期之差*/
{
int days=0;
if (year==b.year&&month==b.month ) {
days=day-b.day;
days=days<0?-days:days;
}
else if (year==b.year) {
int ml,mh;
mh=month>b.month ?(ml=b.month,month):(ml=month,b.month) ;
for (int i=ml; i<mh; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days-=ml==month?day:b.day;
days+=mh==b.month?b.day :day;
}
else{
int yl,yh,ml,mh;
yh=year>b.year ?(yl=b.year,year):(yl=year,b.year) ;
for (int i=yl+1; i<yh; i++) {
if (i%4==0&&i%100||i%400==0)days+=366;
else days+=365;
}
ml=yl==year?month:b.month;
mh=yh==b.year?b.month:month;
for (; ml<=12; ml++) {
switch (ml) {
default:
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days-=yl==year?day:b.day;
for (--mh; mh>=1; mh--) {
switch (mh) {
default:
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days+=yh==b.year?b.day:day;
}
return days;
}
inline bool operator>(const Date &a)/*'>'操作符重载,用于判断日期的先后*/
{
if (year==a.year &&month==a.month ) {
return day>a.day;
}
else if (year==a.year ) {
return month>a.month ;
}
else return year>a.year ;
}
inline Date operator+(int days)/*'+'操作符重载,用于计算days天之后的日期*/
{
for (; days>=0; --days) {
if (day==29&&month==2&&!(year%4==0&&year%100||year%400==0)) {
++month;
day=1;
}
else if (day==30&&month==2&&(year%4==0&&year%100||year%400==0)) {
++month;
day=1;
}
else if (day==31) {
switch (month) {
case 4:
case 6:
case 9:
case 11:month++;day=1;
}
}
else if (day==32) {
month++;
day=1;
if (month==13) {
month=1;
year++;
}
}
if (days) day++;
}
return *this;
}
};
ostream& operator<<(ostream &a,Date &b)/*输出操作符重载,用于输出指定格式的日期*/
{
a<<b.year <<"年"<<b.month <<"月"<<b.day <<"日";
return a;
}
int main(void)/*调用实例*/
{
Date a,b;
a.set();/*设置为1900-1-1*/
b.set(1,1,2009);/*设置为2009-1-1*/
int t=b-a;/*求两个日期之差*/
a=a+60;/*计算60天之后的日期*/
cout<<a;/*输出a日期*/
return 0;
}
参考资料: http://zhidao.baidu.com/question/71832740.html?si=3