日期类的实现主要是去学习使用operator的
日期类就是计算日期之间的天数,日期与(日期,天数)的相加减
比如日常生活中我们可以计算日期加天数,日期减天数,日期减日期,
但没有日期加日期的说法
日期类的实现
- 1.日期的比较
- 2.日期的计算
- 日期的加法
- 日期的减法
- 前置后置++,与- -
- 3.日期的输入输出
- 日期类完整代码
- Date.h
- Date.cpp
1.日期的比较
日期的比较,当写出大于和等于两个函数的时候,其他的比较函数就都可以复用这两个函数了
//先写大于的
//一定是先比较年,再比较月,再比较日
bool Date::operator>(const Date& 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 > d._day){return true;}return false;
}bool Date::operator==(const Date& d)
{return _year == d._year && _month == d._month && _day == d._day;
}
剩下的复用这两个函数就行了
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 || *this == d;
} bool Date::operator!=(const Date& d)
{return !(*this == d);
}
2.日期的计算
日期的加法
日期的加减
日期的加天数我们要考虑到是否会进入到下一个月,下一年,当前年是否是闰年,二月有几天,所以我们还应该有一个计算当前月天数的函数
我们还要考虑代码运行时的效率,返回值可以用引用就用引用,可以减少调用拷贝构造的次数,+=操作返回的*this在函数结束后没有被销毁,所以可以返回引用
int GetMonthDay(int year,int month)
{assert(month > 0 && month < 13);//这个函数会经常调用所以我们可以把数组定义为静态类形static int MonthDayArr[13] ={-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;}else{return MonthDayArr[month];}
}//先写+=,日期只有加天数
//没有日期加日期,例如2024.5.13+2024.5.1这样的写法没有意义
Date& Date::operate+=(int day)
{_day += day;while(_day > GetMonthDay(_year,_month)){_day -= GetMonthDay(_year,_month);++_month;if(_month == 13){_month = 1;++_year;}}return *this;
}
//+=可以复用+
Date Date::operator+(int day)
{Date tmp = *this;tmp += day;return tmp;
}
日期的减法
日期的减法有两种一种是日期减天数,一种是日期减日期
第一种需要注意的是如果天数day是负数的情况,我们要加的是上个月的天数
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){if (_month == 1){--_year;_month = 12;}else{--_month;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}//日期减日期----日期减日期计算的是他们之间相差多少天
int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;if(max < min){max = d;min = *this;flag = -1;}int n = 0;while(max != min){min++;n++;}return n;
}
前置后置++,与- -
这个不难写,但要注意的一点四是如何区分前置与后置,c++规定后置++,- -的形参列表要有一个int类型
Date& Date::operator++()
{return *this += 1;
}Date Date::operator++(int)
{Date tmp = *this;*this += 1;return tmp;
}Date& Date::operator--()
{return *this -= 1;
}Date Date::operator--(int)
{Date tmp = *this;*this += 1;return tmp;
}
3.日期的输入输出
首先这个>>,<<不能写为成员函数,因为我们调用时要写成,对象.成员函数,所以我们写在类外面,然后用友员函数在类中声明
ostream& operator<<(ostream& out, const Date& d)
{cout << d._year << "-" << d._month << "-" << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d)
{cout << "请依次输入年月日";in >> d._year >> d._month >> d._day;return in;
}
日期类完整代码
Date.h
#pragma once#include<iostream>
#include<assert.h>
using namespace std;class Date
{friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);public:int GetMonthDay(int year, int month){assert(month > 0 && month < 13);static int Month[13] = { -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 Month[month];}//构造函数Date(int year = 1, int month = 1, int day = 1);void Print() const;bool operator>(const Date& d) const;bool operator>=(const Date& d) const;bool operator==(const Date& d) const;bool operator<(const Date& d) const;bool operator<=(const Date& d) const;bool operator!=(const Date& d) const;Date& operator+=(int day);Date operator+(int day) const;Date& operator-=(int day);Date operator-(int day) const;Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);int operator-(Date& d) const;private:int _year = 1;int _month = 1;int _day = 1;
};ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
Date.cpp
#define _CRT_SECURE_NO_WARNINGS#include"Date.h"//构造函数
Date::Date(int year,int month, int day)
{if (month > 0 && month < 13&& day > 0 && day <= GetMonthDay(year, month)){_year = year;_month = month;_day = day;}else{cout << "非法日期" << endl;assert(false);}
}void Date::Print() const
{cout << _year << " " << _month << " " << _day << endl;
}bool Date::operator>(const Date& d) const
{if (_year > d._year){return true;}else if (_year == d._year){if (_month > d._month){return true;}else if (_month == d._month){return _day > d._day;}}return false;
}bool Date::operator>=(const Date& d) const
{return *this > d || *this == d;
}bool Date::operator==(const Date& d) const
{return _year == d._year && _month == d._month && _day == d._day;
}bool Date::operator<(const Date& d) const
{return !(*this >= d);
}bool Date::operator<=(const Date& d) const
{return *this < d || *this == d;
} bool Date::operator!=(const Date& d) const
{return !(*this == d);
}Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}return *this;
}Date Date::operator+(int day) const
{Date tmp = *this;tmp += day;return tmp;
}Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){if (_month == 1){--_year;_month = 12;}else{--_month;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day) const
{Date tmp = *this;tmp -= day;return tmp;
}Date& Date::operator++()
{return *this += 1;
}Date Date::operator++(int)
{Date tmp = *this;*this += 1;return tmp;
}Date& Date::operator--()
{return *this -= 1;
}Date Date::operator--(int)
{Date tmp = *this;*this += 1;return tmp;
}int Date::operator-(Date& d) const
{Date max = *this;Date min = d;int flag = 1;if (max < min){max = d;min = *this;flag = -1;}int n = 0;while (max > min){min++;n++;}return n * flag;
}ostream& operator<<(ostream& out, const Date& d)
{cout << d._year << "-" << d._month << "-" << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d)
{cout << "请依次输入年月日";in >> d._year >> d._month >> d._day;return in;
}