日期比较大小
先定义一个日期类,这里包含年月日,这里头文件和源文件分离
重载符号
1.operator>
先声明函数
这里大于可以把*this> d 的情况全部列举出来
bool date::operator>(const date& d)
{if (_year > d._year){return true;}else if (_year == d._year){if (_month > d._month){return true;}else if (_month == d._month){if (_day > d._day){return true;}}}return false;
}
最开始的麻烦点后面全部复用
2. operator>=
bool date::operator>=(const date& d)
{return *this > d || *this == d;
}
3.operator<
bool date::operator<(const date& d)
{return !(*this >= d);
}
4.operator<=
bool date::operator<=(const date& d)
{return !(*this > d);
}
5.operator==
bool date:: operator==(const date& d)
{return _year == d._year && _month == d._month && _day == d._day;
}
6.operator!=
bool date:: operator!=(const date& d)
{return !(_year == d._year && _month == d._month && _day == d._day);}
日期加减
operator+=
date& date::operator+=(int n)
{if (n < 0){return *this -= -n;}_day = _day + n;while (_day > GetMonthDay(_year, _month)){if (_day > GetMonthDay(_year, _month)){_day = _day - GetMonthDay(_year, _month);_month++;if (_month > 12){_month = 1;_year++;}}}return *this;
}
operator+
date date::operator+(int n)
{date tmp = *this; tmp += n;return tmp;
}
operator-=
date& date::operator-=(int n)
{if (n < 0){return *this += -n;}_day = _day - n;while (_day<=0){_month--;if (_month == 0){_month = 12;_year--;}_day = _day + GetMonthDay(_year, _month);}return *this;
}
operator-
date date::operator-(int n)
{date tmp = *this;tmp -= n;return tmp;
}
operator前置++
date& date::operator++()
{*this += 1;return *this;
}
operator后置++
date date::operator++(int)
{date tmp = *this;*this += 1;return tmp;
}
为了区分后置++需要加个int来重载
日期-日期
int date::operator-(const date& d)
{date max = *this;date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int day = 0;while (max != min){++min;++day;}return day * flag;
}
完整代码
//date.h
#pragma once
#include<bits/stdc++.h>
using namespace std;
class date
{
public://date();date(int year=1, int month=1, int day=1);~date();date& 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);bool operator!=(const date& d);date& operator+=(int n);date operator+(int n);date& operator-=(int n);date operator-(int n);date& operator++();//前置++date operator++(int);//后置++date& operator--();date operator--(int);int operator-(const date& d);void print();
private:int _year;int _month;int _day;
};
int GetMonthDay(int year, int month);
//date.cpp
#include"date.h"
//date::date()
//{
// _year = 0;
// _month = 0;
// _day = 0;
//}
void date::print()
{cout << _year << "-" << _month << "-" << _day;cout << endl;
}
date::date(int year ,int month ,int day)
{_year = year;_month = month;_day = day;
}
date::~date()
{;
}bool date::operator>(const date& d)
{if (_year > d._year){return true;}else if (_year == d._year){if (_month > d._month){return true;}else if (_month == d._month){if (_day > d._day){return true;}}}return false;
}
bool date::operator>=(const date& d)
{return *this > d || *this == d;
}
bool date::operator<(const date& d)
{return !(*this >= d);
}
bool date::operator<=(const date& d)
{return !(*this > d);
}bool date:: operator==(const date& d)
{return _year == d._year && _month == d._month && _day == d._day;
}
bool date:: operator!=(const date& d)
{return !(_year == d._year && _month == d._month && _day == d._day);}
date& date::operator=(const date& d)
{_year = d._year;_month = d._month;_day = d._day;return *this;}
int GetMonthDay(int year, int month)
{int arr[] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return 29;}return arr[month];
}date& date::operator+=(int n)
{if (n < 0){return *this -= -n;}_day = _day + n;while (_day > GetMonthDay(_year, _month)){if (_day > GetMonthDay(_year, _month)){_day = _day - GetMonthDay(_year, _month);_month++;if (_month > 12){_month = 1;_year++;}}}return *this;
}
date date::operator+(int n)
{date tmp = *this; tmp += n;return tmp;
}
date& date::operator-=(int n)
{if (n < 0){return *this += -n;}_day = _day - n;while (_day<=0){_month--;if (_month == 0){_month = 12;_year--;}_day = _day + GetMonthDay(_year, _month);}return *this;
}
date date::operator-(int n)
{date tmp = *this;tmp -= n;return tmp;
}date& date::operator++()
{*this += 1;return *this;
}
date date::operator++(int)
{date tmp = *this;*this += 1;return tmp;
}
date& date::operator--()
{*this -= 1;return *this;
}date date::operator--(int)
{date tmp = *this;*this -= 1;return tmp;
}int date::operator-(const date& d)
{date max = *this;date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int day = 0;while (max != min){++min;++day;}return day * flag;
}