您的位置:首页 > 编程语言 > C语言/C++

【C++】日期类

2016-11-09 22:10 417 查看

用C++实现一个日期类,完成以下接口

class Date

{

public:

Date();

Date(const Date& d);

Date& operator=(const Date& d);

Date operator+(int day);

Date& operator++();

Date operator++(int);

Date operator-(int day);

int operator-(const Date& d);

Date& operator--();

Date operator--(int);

bool operator>(const Date& d);

bool operator<(const Date& d);

bool operator==(const Date& d);

bool operator!=(const Date& d);

bool operator>=(const Date& d);

bool operator<=(const Date& d);

friend ostream& operator<<(ostream& _cout, const Date& d);

friend istream& operator>>(istream& _cin, Date& d);

private:

int year;

int month;

int day;

static int op[13];

};

这里面涉及到了运算符重载,构造以及拷贝构造函数

注意输出和输入运算符需要重载成友元函数,这一点在很久之前就讲解过~

实现代码

#include<iostream>
using namespace std;

class Date
{
public:
Date(int _year = 1900, int _month = 1, int _day = 1)
:year(_year), month(_month), day(_day)
{}
Date(const Date& d)
{
this->day = d.day;
this->month = d.month;
this->year = d.year;
}
Date& operator=(const Date& d);
Date operator+(int day);
Date& operator++();
Date operator++(int);
Date operator-(int day);
int operator-(const Date& d);
Date& operator--();
Date operator--(int);
bool operator>(const Date& d);
bool operator<(const Date& d);
bool operator==(const Date& d);
bool operator!=(const Date& d);
bool operator>=(const Date& d);
bool operator<=(const Date& d);

friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);

private:
int year;
int month;
int day;
static int op[13];
};

int Date::op[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

bool IsLeapYear(int year)
{
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
return true;
}
return false;
}

Date& Date::operator=(const Date& d)
{
this->day = d.day;
this->month = d.month;
this->year = d.year;
return *this;
}

Date Date::operator+(int day)
{
int add_year = 0;
int add_month = 0;
int tmp_month = this->month;
while ((day + this->day)>(op[tmp_month] + (tmp_month == 2 && IsLeapYear(this->year + add_year))))//(20+40)>30
{
add_month++;
if (add_month + this->month>12)
{
add_month -= 12;
}

day -= (op[month] + (tmp_month == 2 && IsLeapYear(this->year + add_year)));
if (tmp_month != 12)
{
tmp_month++;
}
else
{
add_year++;
tmp_month = 1;
}
}
this->day += day;
this->month += add_month;
this->year += add_year;

return (*this);
}

Date& Date::operator++()//前置++
{
*this = *this + 1;
return *this;
}

Date Date::operator++(int)//后置++
{
Date old(*this);
++(*this);
return old;
}

Date Date::operator-(int day)
{
int sub_year = 0;
int tmp_month = this->month;
while (day - this->day >= 0)//1>1
{
tmp_month--;
if (tmp_month == 0)
{
tmp_month = 12;
sub_year++;
}
day -= (op[tmp_month] + (tmp_month == 2 && IsLeapYear(this->year - sub_year)));
if ((tmp_month == 2 && IsLeapYear(this->year - sub_year)) == true)
{
day--;
}
}
this->year -= sub_year;
this->month = tmp_month;
this->day -= day;
return (*this);
}

Date& Date::operator--()//前置--
{
*this = *this - 1;
return (*this);
}

Date Date::operator--(int)//后置--
{
Date old(*this);
--(*this);
return old;
}

int Date::operator-(const Date& d)
{
size_t count = 0;
Date Temp = d;
if (Temp>*this)
{
while (Temp != *this)
{
--Temp;
count++;
}
}
else
{
while (Temp != *this)
{
++Temp;
count++;
}
}
return count;
}

bool Date::operator>(const Date& d)
{
if (this->year>d.year)
{
return true;
}
else if (this->year == d.year && this->month > d.month)
{
return true;
}
else if (this->year == d.year && this->month == d.month && this->day > d.day)
{
return true;
}
else
{
return false;
}
}

bool Date::operator<(const Date& d)
{
if (this->year<d.year)
{
return true;
}
else if (this->year == d.year && this->month < d.month)
{
return true;
}
else if (this->year == d.year && this->month == d.month && this->day < d.day)
{
return true;
}
else
{
return false;
}
}

bool Date::operator==(const Date& d)
{
if (this->year == d.year && this->month == d.month && this->day == d.day)
{
return true;
}
else
{
return false;
}
}

bool Date::operator!=(const Date& d)
{
if (this->year != d.year || this->month != d.month || this->day != d.day)
{
return true;
}
else
{
return false;
}

}

bool Date::operator>=(const Date& d)
{
if (this->year >= d.year)
{
return true;
}
else if (this->year == d.year && this->month >= d.month)
{
return true;
}
else if (this->year == d.year && this->month == d.month && this->day >= d.day)
{
return true;
}
else
{
return false;
}
}

bool Date::operator<=(const Date& d)
{
if (this->year <= d.year)
{
return true;
}
else if (this->year == d.year && this->month <= d.month)
{
return true;
}
else if (this->year == d.year && this->month == d.month && this->day <= d.day)
{
return true;
}
else
{
return false;
}
}

ostream& operator<<(ostream &_cout, const Date &d)
{
_cout << d.year << "-" << d.month << "-" << d.day;

return _cout;
}

istream& operator>>(istream& _cin, Date& d)
{
_cin >> d.year >> d.month >> d.day;
return _cin;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 日期类