日期類的實現主要是去學習使用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;
}