//#include<stdio.h>
#include<iostream>
#include<assert.h>
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){//拷贝构造函数
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator=(const Date& d){//赋值构造函数
if (this != &d){
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
~Date(){}//析构函数
bool isLeapYear(int year){//判断是否为闰年
if (year < 1990){
return false;
}
else{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
return true;
}
}
return false;
}
int GetMonthDay(int year, int month){//获取每个月的天数
if (year < 1990 || month<0 || month>12){
return 0;
}
int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30 };
int day = days[month];
if (isLeapYear(year)||month==2){
day++;
}
return day;
}
void Show(){//输出函数
cout << _year << "-" << _month << "-" << _day << endl;
}
bool operator==(const Date& d){//判断是否相等
if (_year == d._year
&&_month == d._month
&&_day == d._day){
return true;
}
else{
return false;
}
}
bool operator!=(const Date& d){//判断是否不等
/*if (_year != d._year || _year != d._year || _day != d._day){
return true;
}
else{
return false;
}*/
if (!operator==(d)){
return true;
}
else{
return false;
}
}
// // d1 < d2
bool operator<(const Date& d){//判断d1是否小于d2
if (_year < d._year){
return true;
}
else if (_month < d._month){
return true;
}
else if (_day < d._day){
return true;
}
else{
return false;
}
}
bool operator>=(const Date& d){//判断d1是否大于等于d2
if (!operator<(d)){
return true;
}
else{
return false;
}
}
bool operator<=(const Date& d){//判断d1是否小于等于d2
if (operator<(d) || operator==(d)){
return true;
}
else{
return false;
}
}
bool operator>(const Date& d){//判断d1是否大于d2
if (!operator<=(d)){
return true;
}
else{
return false;
}
}
// // d1 + 10
Date operator+(int day){
Date ret(*this);
ret._day += day;
while (ret._day > GetMonthDay(ret._year, ret._month)){
ret._day -= GetMonthDay(ret._year, ret._month);
++ret._month;
if (ret._month == 13){
ret._month = 1;
++ret._year;
}
}
return ret;
}
Date& operator+=(const int day){
_day += day;
while (_day > GetMonthDay(_year, _month)){
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13){
_month = 1;
++_year;
}
}
return *this;
}
Date operator-(int day){
Date ret(*this);
ret._day -= day;
while (ret._day <1){
--ret._month;
if (ret._month < 1){
ret._month = 12;
--ret._year;
}
ret._day += GetMonthDay(ret._year, ret._month);
}
return ret;
}
Date& operator-=(int day){
_day -= day;
while (_day < 1){
--_month;
if (_month < 1){
_month = 12;
--_year;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
int operator-(const Date& d){
Date max = 0;
Date min = 0;
if (operator>(d)){
max = *this;
min = d;
}else if(operator==(d)){
return 0;
}
else{
max = d;
min = *this;
}
int count = 0;
while (min.operator!=(max)){
min.operator++(1);
count++;
}
return count;
}
++d1
Date& operator++() // 前置
{
return *this += 1;
}
// //d1++
Date operator++(int) // 后置
{
Date ret(*this);
ret += 1;
*this = ret;
return ret;
}
// Date operator--();
//Date operator--(int);
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2012,7,8);
// d1.operator-(60).Show();
/*d1.Show();*/
Date d2(2012, 7, 27);
//d2.operator-=(20).Show();
cout << d1.operator-(d2) << endl;
//d1 .operator+=(1000);
//cout << (d1 != d2) << endl;
//cout << (d1<=d2) << endl;
//d1.Show();
//Date d2(2011, 2, 12);
//d2.operator+(1).Show();
//d2.Show();
//++d1;
//d1.operator++().Show();
//d1++;
// d1.operator++(1).Show();
system("pause");
return 0;
}
【C++】实现data类
最新推荐文章于 2024-09-06 20:33:16 发布