qq_34174865 于 2016.03.23 14:12 提问

#include
using namespace std;
const int lmonth[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
//闰年每个月份的时间
const int umonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//非闰年每个月份的时间，定义符合使用习惯
bool isLeapYear(int year);
class Date
{
private:
int m_year;
int m_month;
int m_day;
public:
Date(int year=2016,int month=3,int day=23);
Date(Date&b);
Date operator+(int days);
Date operator-(int days);
int operator-(Date &b);
friend bool operator>(Date &a,Date &b);
friend ostream& operator <<(ostream& out,Date &b);
friend istream& operator >>(istream&in,Date &b);
friend bool operator!=(Date &a,Date &b);
};
int main()
{
Date date1,date2,date3;
//测试日期相减
cout<<"请依次输入日期1的年月日：（请输入正确日期！）"< cin>>date1;
cout<<"请依次输入日期2的年月日：（请输入正确日期！）"< cin>>date2;
cout< cout int t;
cin>>t;
//测试日期加一天减一天
cout< cout return 0;
}
inline bool isLeapYear(int year)
{
if(year%4==0&&year%100!=0)
return true;
else if(year%400==0)
return true;
else
return false;
}
//构造函数
Date::Date(int year,int month,int day)
{
m_year=year;
m_month=month;
m_day=day;
}
//拷贝构造函数
Date::Date(Date&b)
{
m_year=b.m_year;
m_month=b.m_month;
m_day=b.m_day;
}
//重载>
bool operator>(Date &a,Date &b)
{
if(a.m_year>b.m_year)
return true;
else if(a.m_month>b.m_month)
return true ;
else if(a.m_day>b.m_day)
return true;
else
return false;
}
//无副作用的+-
Date Date::operator+(int days)
{
Date tmp=*this;//目的是无副作用
tmp.m_day=tmp.m_day+days;
loop1:while((tmp.m_day>lmonth[tmp.m_month])&&isLeapYear(tmp.m_year))
{
tmp.m_day-=lmonth[tmp.m_month];//闰年加上闰年的月份
if(tmp.m_month==12)//每12月进一年
{
tmp.m_month=1;
tmp.m_year++;
}
else
tmp.m_month++;
}
//平年加上平年的月份
while((tmp.m_day>umonth[tmp.m_month])&&!isLeapYear(tmp.m_year))
{
tmp.m_day-=umonth[tmp.m_month];
if(tmp.m_month==12)
{
tmp.m_month=1;
tmp.m_year++;

}
else
tmp.m_month++;
goto loop1;
}
return tmp;
}
Date Date::operator-(int days)
{
Date tmp=*this;
tmp.m_day-=days;
loop2:while(tmp.m_day<=0&&isLeapYear(tmp.m_year))
{
tmp.m_day+=lmonth[tmp.m_month];
if(tmp.m_month==1)
{
tmp.m_month=12;
tmp.m_year--;
}
else
tmp.m_month--;
}
while(tmp.m_day<=0&&!isLeapYear(tmp.m_year))
{
tmp.m_day+=umonth[tmp.m_month];
if(tmp.m_month==1)
{
tmp.m_month=12;
tmp.m_year--;
}
else
tmp.m_month--;
goto loop2;
}
return tmp;
}
int Date::operator-(Date &b)
{
int days=0;
if(*this>b)
{
for(int y=b.m_year;y<=m_year;y++)
if(isLeapYear(y))
days+=366;
else
days+=365;
if(isLeapYear(m_year)
{
days+=366;
for(int m=m_month;m<=12;m++)//thisyear还没过
days-=lmonth[m];
days+=m_day;
}
else
{
days+=365;
for(int m=m_month;m<=12;m++)
days-=umonth[m];
days+=m_day;
}
if(isLeapYear(b.m_year)//h
{
days+=366;
for(int m=1;m<=b.m_month;m++)//thisyear还没过
days-=lmonth[m];
days+=(lmonth(b.m_month)-b.m_day);
}
else
{
days+=365;
for(int m=1;m<=b.m_month;m++)//thisyear还没过
days-=umonth[m];
days+=(umonth(b.m_month)-b.m_day);
}
}
else
{
for(int y=m_year;y<=b.m_year;y++)
if(isLeapYear(y))
days+=366;
else
days+=365;
if(isLeapYear(b.m_year)
{
days+=366;
for(int m=b.m_month;m<=12;m++)//thisyear还没过
days-=lmonth[m];
days+=b.m_day;
}
else
{
days+=365;
for(int m=b.m_month;m<=12;m++)
days-=umonth[m];
days+=b.m_day;
}
if(isLeapYear(m_year)//h
{
days+=366;
for(int m=1;m<=m_month;m++)//thisyear还没过
days-=lmonth[m];
days+=(lmonth(m_month)-m_day);
}
else
{
days+=365;
for(int m=1;m<=m_month;m++)//thisyear还没过
days-=umonth[m];
days+=(umonth(m_month)-m_day);
}
}
}
//实现自定义输出
ostream& operator <<(ostream& out,Date &b)
{
cout< return out;
}
//实现自定义输入
istream& operator >>(istream&in,Date &b)
{
cin>>b.m_year>>b.m_month>>b.m_day;
return in;
}
//重定义！=
bool operator!=(Date &a,Date &b)
{
if(a.m_month==b.m_month&&a.m_year==b.m_year&&a.m_day==b.m_day)
return false;
else
return true;
}

2个回答

sl_18500   2016.03.23 14:25

IT_163   2016.03.23 14:28