189 8069 5689

c++学习笔记_CDate类的实现-创新互联

#ifndef _CALENDAR__H _

创新互联主要业务有网站营销策划、网站建设、网站设计、微信公众号开发、小程序开发、HTML5、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、营销型网站建设资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。 

#include

#include

using namespace std;

class CDate

{

friend bool leapyear(int year);

friend int getmonthday(int year, int month);

public:

void SetDate(int year, int month, int day)

{

_year = year;

_month = month;

_day = day;

}

CDate(int year = 1990, int month = 1, int day = 1)

{

if (year >= 1990 && month <= 12 && month > 0 && day > 0 && day <= getmonthday(year, month))

{

_year = year;

_month = month;

_day = day;

}

else

{

cout << "输入错误" << endl;

_year = 1990;

_month = 1;

_day = 1;

}

}

CDate(CDate &d)

{

_year = d._year;

_month = d._month;

_day = d._day;

}

void display()

{

cout << _year << "-" << _month << "-" << _day << endl;

}

private:

int getmonthday(int year, int month)

{

int montharr[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int day = montharr[month];

if (month == 2 && leapyear(year))

{

day = day + 1;

}

return day;

}

bool leapyear(int year)

{

if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)

{

return true;

}

return false;

}

int _year;

int _month;

int _day;

public:

bool operator==(const  CDate& d)

{

return (_year == d._year &&_month == d._month&&_day == d._day);

}

bool operator!=(const  CDate& d)

{

return !(*this == d);

}

bool operator>(const  CDate& d)

{

return (_year > d._year&&_month > d._month&&_day > d._day);

}

bool operator>=(const  CDate& d)

{

return (*this > d || *this == d);

}

bool operator<(const  CDate& d)

{

return !(*this > d || *this == d);

}

bool operator<=(const  CDate& d)

{

return !(*this > d);

}

CDate& operator--()

{

*this -= 1;

return *this;

}

CDate operator--(int)

{

CDate temp(*this);

*this -= 1;

return temp;

}

CDate& operator++()

{

*this += 1;

return *this;

}

CDate operator++(int)

{

CDate temp(*this);

*this += 1;

return temp;

}

CDate operator+(int day)

{

CDate temp(*this);

if (day <0)

{

return *this - (-day);

}

temp._day += day;

while (temp._day > getmonthday(temp._year, temp._month))

{

temp._day -= getmonthday(temp._year, temp._month);

if (temp._month == 12)

{

temp._year++;

temp._month = 1;

}

else

{

temp._month++;

}

}

return temp;

}

CDate operator-(int day)

{

CDate temp(*this);

if (day < 0)

{

return *this + (-day);

}

temp._day -= day;

while (temp._day <= 0)

{

if (temp._month == 1)

{

temp._year--;

temp._month = 12;

}

else

{

temp._month--;

}

temp._day += getmonthday(_year, _month);

}

return temp;

}

CDate operator-=(int day)

{

*this = *this - day;

return *this;

}

CDate operator+=(int day)

{

*this = *this + day;

return *this;

}

int operator-(CDate &d)

{

int flag = 1;

CDate max = *this;

CDate min = d;

if (max < min)

{

std::swap(max._year, min._year);

std::swap(max._month, min._month);

std::swap(max._day, min._day);

flag = -1;

}

int count = 0;

while (max != min)

{

++count;

++min;

}

return (count*flag);

}

};

#endif _CALENDAR__H _

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:c++学习笔记_CDate类的实现-创新互联
文章来源:http://cdxtjz.com/article/gcijs.html

其他资讯