//日期计算器
#include<iostream> using namespace std; #include<string> class Date { public: Date (int year = 2015, int month = 11, int day = 15)//构造函数 :_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; } void Display1() { cout<<"日期:"<<_year<<"-"<<_month<<"-"<<_day<<endl; } public: bool operator==(const Date& d) { return _year == d._year && _month == d._month && _day == d._day; } bool operator!=(const Date& d) { return !(*this==d); } bool operator > (const 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; } } } return false; } bool operator>=(const Date& d) { return *this > d || *this == d; } bool operator<(const Date& d) { return !(*this >= d); } bool operator<=(const Date& d) { return !(*this > d); } bool Judge_ymd(int year=2015,int month =11,int day=15)//判断有效性 { if(_year<1||_month<0||_month>12 ||_day<1||_day>Judge_day(_year,_month)) return false; return true; } bool IsLeapYear (int year)//闰年 { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; return false; } int YearMonth(Date& d)//月数 ++ { ++d._month; if(d._month==13) { d._month=1; ++d._year; } else { d._month; } return d._month; } int Judge_day(int year,int month)//每月天数 { int MonthDay[12]={31,28,31,30,31,30,31,31,30,31,30,31}; if(IsLeapYear(year)) { return MonthDay[month-1]+1; } return MonthDay[month-1]; } public: void CorrectDate()//转换日期 { while(_day<=0) { if(_month==1) { _year--; _month=12; } else { _month--; } _day+=Judge_day(_year,_month); } while(_day>=Judge_day(_year,_month)) { if(_month==12) { _year++; _month=1; } else { _month++; } _day-=Judge_day(_year,_month); } } //计算:日期前后n天的日期 Date operator+(int day) { cout<<day<<"天之后:"<<endl; Date d(*this); d._day+=day; d.CorrectDate(); return d; } Date operator-(int day) { cout<<day<<"天之前:"<<endl; Date d(*this); d._day-=day; d.CorrectDate(); return d; } Date& operator+=(int day) { this->_day+=day; this->CorrectDate(); return *this; } Date& operator-=(int day) { this->_day-=day; this->CorrectDate(); return *this; } Date& operator++()//前置++ { *this+=1; return *this; } Date operator++(int)//后置++ { Date tmp(*this); *this+=1; return tmp; } //计算:两日期之间天数 int operator-(Date& d) { int days=0; Date d1=*this; Date d2=d; cout<<"两日期间天数:"<<endl; if(d1<d2)//d2较小 { d1=d; d2=*this; } while(d1 != d2) { days++; } return days; } friend istream& operator>>(istream& in, Date& d); friend ostream& operator<<(ostream& out, Date& d); private: int _year; int _month; int _day; }; istream& operator>>(istream& in, Date& d) { in>>d._year; in>>d._month; in>>d._day; return in; } ostream& operator<<(ostream& out, Date& d) { out<<d._year<<"-"<<d._month<<"-"<<d._day<<endl; return out; } void DisplayMenu() { cout<<"*************************************"<<endl; cout<<"********* 万 年 历 ************"<<endl; cout<<"*************************************"<<endl; cout<<"**** 1 计算日期前/后 n天的日期 ***"<<endl; cout<<"**** 2 计算两日期之间天数 *******"<<endl; cout<<"**** 0 退出 ******************"<<endl; cout<<"*************************************"<<endl; } int main() { int input=0,flag=1; Date d1; Date d2; while(flag) { DisplayMenu(); cout<<"请选择:"<<endl; cin>>input; switch(input) { case 1: { next1: int k=0; cout<<"请输入日期:"<<endl; cin>> d1; if(d1.Judge_ymd()==false) { cout<<"非法日期,请重新输入"<<endl; goto next1; } cout<<"请输入之后的天数:"<<endl; cin>>k; (d1+k).Display1(); break; } case 2: { next2: int days=0; cout<<"请输入日期1:"<<endl; cin>> d1; if(d1.Judge_ymd()==false) { cout<<"非法日期,请重新输入"<<endl; goto next2; } next3: cout<<"请输入日期2:"<<endl; cin>> d2; if(d2.Judge_ymd()==false) { cout<<"非法日期,请重新输入"<<endl; goto next3; } days=d1-d2; cout<<"相差天数:"<<days<<endl; break; } case 0: { cout<<"退出"<<endl; break; } default: { cout<<"输入错误,请重新输入"<<endl; break; } } if(input==0) { flag=0; } else { flag=1; } } return 0; }
时间: 2024-11-05 10:23:51