189 8069 5689

完成CDate类的几个函数-创新互联

实现一个CDate类

目前累计服务客户数千家,积累了丰富的产品开发及服务经验。以网站设计水平和技术实力,树立企业形象,为客户提供网站制作、网站建设、网站策划、网页设计、网络营销、VI设计、网站改版、漏洞修补等服务。创新互联建站始终以务实、诚信为根本,不断创新和提高建站品质,通过对领先技术的掌握、对创意设计的研究、对客户形象的视觉传递、对应用系统的结合,为客户提供更好的一站式互联网解决方案,携手广大客户,共同发展进步。
class CDate
{
public:
CDate(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{
if (!(_year >= 1900 &&
(_month > 0 && _month < 13) &&
(_day > 0 && _day <= _GetMonthDay(_year, _month))))
{
_year = 1900;
_month = 1;
_day = 1;
}
}
 
     bool IsLeap(int year)//判断闰年
{
if ((year % 4 == 0 && year % 100 != 0) ||
(year % 400 == 0))
{
return true;
}
 
return false;
}
private:
int _GetMonthDay(int year, int month)
{
int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (IsLeap(year) && month == 2)//闰年并且月份为2
{
days[month] += 1;
}
 
return days[month];
}
 private:
int _year;
int _month;
int _day;
};

完成CDate类的几个函数

#include 
using namespace std;
class CDate
{
public:
CDate(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{
if (!(_year >= 1900 &&
(_month > 0 && _month < 13) &&
(_day > 0 && _day <= _GetMonthDay(_year, _month))))
{
_year = 1900;
_month = 1;
_day = 1;
}
}
//比较日期
bool operator<(const CDate& d)
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day(const CDate& d)
{
return (!(*this < d || *this == d));
}
//日期加法
CDate operator+(int day)
{
if (day < 0)
{
return *this - (0 - day);
}
 
CDate temp(*this);
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)
{
if (day < 0)
{
return *this + (0 - day);
}
 
CDate temp(*this);
temp._day -= day;
 
while (temp._day <= 0)
{
if (temp._month == 1)
{
temp._year--;
temp._month = 12;
}
else
{
temp._month--;
}
 
temp._day += _GetMonthDay(temp._year, temp._month);
}
 
return temp;
}
//前置加加
CDate& operator++()
{
*this = *this + 1;
return *this;
}
 
//后置加加
CDate operator++(int)
{
CDate temp(*this);
*this = *this + 1;
return temp;
}
//前置减减
CDate& operator--()
{
*this = *this - 1;
return *this;
}
//后置减减
CDate operator--(int)
{
CDate temp(*this);
*this = *this - 1;
return temp;
}
//两者作差
int operator-(const CDate& d)
{
CDate min(*this);
CDate max(d);
if (min > max)
{
min = d;
max = *this;
}
 
int count = 0;
while (min < max)
{
min = min + 1;
count++;
}
 
return count;
}
 
bool IsLeap(int year)
{
if ((year % 4 == 0 && year % 100 != 0) ||
(year % 400 == 0))
{
return true;
}
 
return false;
}
private:
int _GetMonthDay(int year, int month)
{
int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (IsLeap(year) && month == 2)//闰年并且月份为2
{
days[month] += 1;
}
 
return days[month];
}
 
friend ostream& operator<<(ostream& _cout, const CDate& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
private:
int _year;
int _month;
int _day;
 
 
};
int main()
{
CDate d(1992, 1, 26);
cout << d + 4 << endl;
cout << d + 34 << endl;
cout << d + 10000 << endl;
 
cout << d - 26 << endl;
 
CDate d1(1992, 1, 1);
cout << d - d1 << endl;
cout << d1 - d << endl;
 
cout << d1++ << endl;
cout << ++d1 << endl;
system("pause");
return 0;
}

完成CDate类的几个函数

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


分享文章:完成CDate类的几个函数-创新互联
本文来源:http://cdxtjz.cn/article/iiege.html

其他资讯