日期計算器

#define _CRT_SECURE_NO_WARNINGS #include #include using namespace std; class Date { public: Date(int _year, int _month, int _day);  //構造函數 void ShowDate() { cout << year << "-" << month << "-" << day << endl; } 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  day);    //某個日期+天數 Date &operator+=(int  day);   //某個日期+=天數 Date operator-(int day);     //某個日期-天數 Date &operator-=(int day);   //某個日期-=天數 Date& operator++();        //某個日期++(前置) Date operator++(int);      //某個日期++(後置) Date& operator--();        //某個日期--(前置) Date operator--(int);      //某個日期--(後置) int operator-( Date& d);    //兩個日期相減 friend bool IsTrueDate(Date &d);   //友元函數,判斷某個日期是否是合法日期 private: int year; int month; int day; }; Date::Date(int _year, int _month, int _day) :  // 用初始化列表初始化構造函數 year(_year), month(_month), day(_day) { } bool Date::operator<(const Date& d)   //小於運算符的重載 { return this->year < d.year || this->year == d.year && this->monthyear == d.year && this->month == d.month && this->dayyear == d.year && this->month == d.month && this->day == d.day; } bool Date::operator>(const Date& d)    //大於運算符的重載 { return this->year > d.year || this->year == d.year && this->month>d.month || this->year == d.year && this->month == d.month && this->day>d.day; } 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); } //bool Date::operator>=(const Date& d)    //大於等於運算符的重載 //{ //return *this > d && *this == d; //} bool IsLeapYear(int year)      //判斷某年是否是閏年 { if ((year % 400 == 0) || ((year % 4 == 0) && (year % 1001 != 0))) { return true; } else return false; } int GetMonthDay(int year, int month)    //返回某個月的天數 { int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int day = monthArray[month]; if (month == 2 && IsLeapYear(year)) { day += 1; } return day; } //日期計算器 Date& Date:: operator += (int day) { *this=*this + day; return *this; } Date Date::operator+ (int day) { if (day<0) { *this -(-day); } Date tmp(*this); if (IsTrueDate(*this)) { tmp.day += day; while (tmp.day > GetMonthDay(tmp.year, tmp.month)) { tmp.day -= GetMonthDay(tmp.year, tmp.month); if (tmp.month == 12) { tmp.year++; tmp.month = 1; } else { tmp.month++; } } } else { cout << "The date is eror!" << endl; tmp.year = 1900; tmp.month = 1; tmp.day = 1; } return tmp; } Date &Date::operator -= (int  day) { *this = *this - day; return *this; } Date Date::operator - (int day) { if (day<0) { *this + (-day); } Date tmp(*this); if (IsTrueDate(*this)) { tmp.day -= day; while (tmp.day <= 0) { if (tmp.month == 1) { tmp.month = 12; tmp.year--; } else { tmp.month--; } tmp.day += GetMonthDay(tmp.year, tmp.month); } } return tmp; } Date Date::operator--(int)   //後置-- { Date tmp(*this); *this -= 1; return tmp; } Date Date::operator++(int)   //後置++ { Date tmp(*this); *this += 1; return tmp; } Date& Date::operator--()   //前置-- { *this -= 1; return *this; } Date&  Date::operator++()   //前置++ { *this += 1; return *this; } bool  IsTrueDate(Date &d) { if (d.year > 1900 && d.month > 0 && d.month<13 && d.day>0 && d.day <= GetMonthDay(d.year, d.month)) { return true; } else { cout << "The date is eror!" << endl; d.year = 1900; d.month = 1; d.day = 1; return false; } } int Date::operator - ( Date& d)  { int days=0; int flag = 1; if (IsTrueDate(*this) && IsTrueDate(d)) { while (!(*this == d)) { if (*this > d) { d.day++; days++; } else { this->day++; days++; flag = -1; } } } return flag * days; }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章