前言:代码所需知识点在后两篇博客。默认成员函数+友元函数。
一相关思考
闰年问题:
- 四年一闰,百年不闰/四百年一闰;
- 只涉及到二月为28/29天。
- 可写为类内函数,便于调用。
相关+,-运算符需要考虑隔好几个月的问题,使用循环解决。
二代码
#include<iostream>
using namespace std;
class date {
public:
friend ostream& operator<<(ostream& _cout, const date& d);
date(int year, int mouth, int day)
:_year(year)
,_mouth(mouth)
{
_year = year;
_mouth = mouth;
_day = day;
}
date(const date&d) {
_year = d._year;
_mouth = d._mouth;
_day = d._day;
}
date& operator=(const date& d) {
_year = d._year;
_mouth = d._mouth;
_day = d._day;
return *this;
}
int getmouthday(int year,int mouth) {
int day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (mouth == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
return day[2]+1;
}
return day[mouth];
}
date operator+(int days) {
date temp(*this);
int sum = days + _day;
if (sum <= getmouthday(temp._year, temp._mouth)) {
temp._day = sum;
return temp;
}
else {
while (sum > getmouthday(temp._year, temp._mouth))
{
sum -= getmouthday(temp._year, temp._mouth);
if (temp._mouth == 12) {
temp._mouth = 1;
temp._year++;
}
else {
temp._mouth++;
}
}
temp._day = sum;
return temp;
}
}
date operator-(int days) {
date temp(*this);
int sum = _day-days;
if (sum >=1) {
temp._day = sum;
return temp;
}
else {
while (sum <=0)
{
if (temp._mouth == 1) {
temp._mouth = 12;
temp._year--;
}
else {
temp._mouth--;
}
sum += getmouthday(temp._year, temp._mouth);
}
temp._day = sum;
return temp;
}
}
int operator-(const date& d)
{
int i;
int total = (_year - d._year)*365;
if (_mouth > d._mouth) {
for ( i = d._mouth; i < _mouth; i++)
{
total += getmouthday(_year, i);
}
total += _day - d._day;
}
else {
for ( i = _mouth; i < d._mouth; i++)
{
total -= getmouthday(_year, i);
}
total += _day - d._day;
}
for (i = d._year; i <= _year; i++) {
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
if (i == d._year&&d._mouth > 2)
{
continue;
}
if (i == _year && d._mouth <= 2)
{
continue;
}
total++;
}
}
return total;
}
date operator++() {
date temp(*this);
int sum = 1 + _day;
if (sum <= getmouthday(temp._year, temp._mouth)) {
temp._day = sum;
return temp;
}
else {
temp._mouth++;
temp._day = 1;
return temp;
}
}
date operator++(int) {
date temp(*this);
int sum = 1 + _day;
if (sum <= getmouthday(_year, _mouth)) {
_day = sum;
return temp;
}
else {
while (sum > getmouthday(_year, _mouth))
{
sum -= getmouthday(_year, _mouth);
if (_mouth == 12) {
_mouth = 1;
_year++;
}
else {
_mouth++;
}
}
_day = sum;
return temp;
}
}
date operator--() {
date temp(*this);
int sum = _day - 1;
if (sum >= 1) {
temp._day = sum;
return temp;
}
else {
if (temp._mouth == 1) {
temp._mouth = 12;
temp._year--;
}
else {
temp._mouth--;
}
temp._day = getmouthday(temp._year, temp._mouth);
return temp;
}
}
date operator--(int) {
date temp(*this);
int sum = _day - 1;
if (sum >= 1) {
_day = sum;
return temp;
}
else {
if (_mouth == 1) {
_mouth = 12;
_year--;
}
else {
_mouth--;
}
_day = getmouthday(_year,_mouth);
return temp;
}
}
bool operator<(const date& d)const {
if (_year < d._year || (_year == d._year&&_mouth < d._mouth) || (_year == d._year&&_mouth == d._mouth&&_day < d._day)) {
return true;
}
return false;
}
bool operator>(const date& d)const {
if (_year > d._year || (_year == d._year&&_mouth > d._mouth) || (_year == d._year&&_mouth == d._mouth&&_day > d._day)) {
return true;
}
return false;
}
bool operator<=(const date& d)const {
return !(*this > d);
}
bool operator>=(const date& d)const {
return !(*this < d);
}
bool operator==(const date& d)const {
if (_year == d._year&&_mouth == d._mouth&&_day == d._day) {
return true;
}
return false;
}
bool operator!=(const date& d)const {
return !(*this == d);
}
private:
int _year;
int _mouth;
int _day;
};
ostream& operator<<(ostream& _cout,const date& d) {
_cout <<d._year << d._mouth <<d._day;
return _cout;
}
void test() {
date d1(2019, 9, 22);
date d2(2019, 9, 21);
date d3(2019, 9, 23);
date d4 = d3 + 5;
d2--;
d1++;
d4 = d4 + 30;
cout << d4 - d1 << endl;
cout << d3 << endl;
cout << d2 << endl;
cout << d1 << endl;
cout << d4 << endl;
}
int main(){
test();
system("pause");
}