内容:
针对Date类重载<,>,<=,>=,==,++,<<,>>运算符,并构建排序函数,将时间进行升序排序。
要求:
#ifndef DATE_H #define DATE_H class Date { public: friend ostream& operator<<(ostream& output, Date& d);//运算符重载 friend istream& operator>>(istream& input, Date& d);//运算符重载 //Date(); Date(int = 1900, int = 1, int = 1); Date(const Date &);//拷贝构造函数 ~Date();//析构函数 void setDate(int, int, int); //对输入日期做有效性验证 inline int getYear() const; inline int getMonth() const;//显示定义内联函数 inline int getDay() const; void displayDate() const; Date* addOneDay(); bool isLeapYear(); bool operator==(Date &);//运算符重载 bool operator!=(Date &); bool operator>(Date &); bool operator<(Date &); bool operator<=(Date &); bool operator>=(Date &); Date& operator++(); //前置 Date operator++(int);//后置 void Sort(Date date[6]); private: int year, month, day; }; #endif
#include <iostream> using namespace std; #include "Date.h" //运算符重载函数实现 ostream& operator<<(ostream& output, Date& d) { output << d.year << "/" << d.month << "/" << d.day; return output; } istream& operator>>(istream& input, Date& d) { cout << "请输入年份:"; while (1) { input >> d.year; if (d.year >= 1900) break; else cout << "错误,请从新输入:"; } cout << "请输入月份:"; while (1) { input >> d.month; if (d.month>0 && d.month<13) break; else cout << "错误,请从新输入:"; } cout << "请输入日:"; input >> d.day; return input; } Date::Date(int y, int m, int d){ setDate(y, m, d); } Date::Date(const Date &d){ year = d.year; month = d.month; day = d.day; } Date::~Date(){ } void Date::setDate(int y, int m, int d){ while (1)//验证年 { if (y<1900){ cout << "please input the year(>=1900) again:" << endl; cin >> y; } else{ year = y; break; } } while (1)//验证月 { if (m<1 || m>12){ cout << "please input the month(1=<month<=12) again:" << endl; cin >> m; } else{ month = m; break; } } while (1)//验证日期 { if ((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12)){ if (d<1 || d>31){ cout << "please input the day(1=<day<=31) again:" << endl; cin >> d; } else{ day = d; break; } } if ((m == 4) || (m == 6) || (m == 9) || (m == 11)){ if (d<1 || d>30){ cout << "please input the day(1=<day<=30) again:" << endl; cin >> d; } else{ day = d; break; } } if (m == 2){ if (isLeapYear()){ if (d<1 || d>29){ cout << "please input the day(1=<day<=29) again:" << endl; cin >> d; } else{ day = d; break; } } else { if (d<1 || d>28){ cout << "please input the day(1=<day<=29) again:" << endl; cin >> d; } else{ day = d; break; } } } } } inline int Date::getYear()const { return year; } inline int Date::getMonth()const{ return month; } inline int Date::getDay()const{ return day; } void Date::displayDate()const{ cout << getYear() << "/" << getMonth() << "/" << getDay() << endl; } Date* Date::addOneDay(){ switch (month) { case 2: if (isLeapYear()){ if (day<29) day++; else{ day = 1; month++; } } else{ if (day<28) day++; else{ day = 1; month++; } } break; case 1: case 3: case 5: case 7: case 8: case 10: if (day>30){ day = 1; month++; } else day++; break; case 4: case 6: case 9: case 11: if (day>29){ day = 1; month++; } else day++; break; case 12: if (day>30){ day = 1; month = 1; year++; } else day++; break; default: cout << "ERROR" << endl; } return this; } bool Date::isLeapYear(){ if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return true; else return false; } bool Date::operator==(Date& d) { return (this->year == d.year) && (this->month == d.month) && (this->day == d.day); } bool Date::operator!=(Date& d) { return (this->year != d.year) || (this->month != d.month) || (this->day != d.day); } bool Date::operator>(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; else return false; } } return false; } bool Date::operator<(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; else return false; } } return false; } bool Date::operator<=(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; else return false; } } return false; } bool Date::operator>=(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; else if (day<d.day) return false; } } return false; } Date& Date::operator++() { addOneDay(); return *this; } Date Date::operator++(int) { Date temp = *this; addOneDay(); //cout<<"测试:"<<temp<<" "<<*this<<endl; return temp; } void Date::Sort(Date date[6]) { int i, j; Date temp(1900, 1, 1); for (i = 0; i<6; i++) { for (j = 0; j<6; j++) { if (date[j]<date[j + 1]) { temp = date[j]; date[j] = date[j + 1]; date[j + 1] = temp; } } } }
#include <iostream> using namespace std; #include "Date.h" void Sort(Date date1, Date date2, Date date3, Date date4, Date date5, Date date6) { int i, j; Date date[6] = { date1, date2, date3, date4, date5, date6 }; Date temp; for (i = 0; i<6; i++) { for (j = 0; j<6 - i; j++) { if (date[j]<date[j + 1]) { temp = date[j]; date[j] = date[j + 1]; date[j + 1] = temp; } } cout << date[i] << " "; } } int main() { Date date1(2020, 4, 10); cout << "date1:" << date1 << endl; Date *date2 = date1.addOneDay(); cout << "date2:" << *date2 << endl; Date date3(2000, 1, 9); cout << "date3:" << date3 << endl; Date date4(2000, 3, 1); cout << "date4:" << date4 << endl; Date date5(1999, 12, 31); cout << "date5:" << date5 << endl; Date date6(1900, 12, 1); cout << "date6:" << date6 << endl; if (date1 == date3) cout << endl << "两日期相等(重载==运算符)" << endl; else cout << "两日期不相等(重载==运算符)" << endl; if (date4 != date3) cout << "两日期不相等(重载!=运算符)" << endl; else cout << "两日期相等(重载!=运算符)" << endl; if (date4 >= date3) cout << date4 << ">=" << date3 << "(重载>=运算符)" << endl; else cout << date4 << "<" << date3 << "(重载>=运算符)" << endl; if (date6 <= date3) cout << date6 << "<=" << date3 << "(重载<=运算符)" << endl; else cout << date6 << ">" << date3 << "(重载<=运算符)" << endl; if (date3<*date2) cout << date3 << "<" << *date2 << "(重载<运算符)" << endl; else cout << date3 << ">" << *date2 << "(重载<运算符)" << endl; if (date5>date6) cout << date5 << ">" << date6 << "(重载>运算符)" << endl; else cout << date5 << "<" << date6 << "(重载>运算符)" << endl; date5++; cout << date5 << "(重载后置++运算符)" << endl; ++date5; cout << date5 << "(重载前置++运算符)" << endl; cout << "日期排序:"; Sort(date1, *date2, date3, date4, date5, date6); return 0; }