网络上有一个日期计算器可以通过输入的日期来计算天数,或者通过日期加减天数来计算出相应的日期。这个小工具对在我们生活中还是非常有用的,它的代码实现是不是很难呢?其实用我们学习过的C++类来处理问题就变得很简单了。
参考代码:(加强版)
#include<iostream> #include<cstdlib> using namespace std; class Date { public: Date(int year=1900,int month=1,int day=1)//构造函数 :_year(year) ,_month(month) ,_day(day) { if(IsInvalidDate()) { _year=1900; _month=1; _day=1; } } Date(const Date& d)//拷贝构造 { _year=d._year; _month=d._month; _day=d._day; } bool IsInvalidDate() { if((_year<1900) || ((_month<1) || (_month>12)) || ((_day<1) || (_day>DayInMonth()))) { return true; } return false; } int DayInMonth() { int Days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if((_year%400==0) ||((_year%100!=0) && (_year%4==0))) { Days[2]+=1; } return Days[_month]; } Date& operator=(const Date& d) { if(this!=&d) { _year=d._year; _month=d._month; _day=d._day; } return *this; } Date operator+(int Day) { Date d(*this); d._day+=Day; d.ToCorrectDate(); return d; } Date operator-(int Day) { Date d(*this); d._day-=Day; d.ToCorrectDate(); return d; } Date operator+=(int Day) { this->_day+=Day; this->ToCorrectDate(); return *this; } Date operator-=(int Day) { this->_day-=Day; this->ToCorrectDate(); return *this; } void ToCorrectDate() { while(_day<=0) { if(1==_month) { _month=12; _year-=1; } else { _month-=1; } _day+=DayInMonth(); } while(_day>DayInMonth()) { if(12==_month) { _month=1; _year+=1; } else { _month+=1; } _day-=DayInMonth(); } } int operator-(Date& d) { int days=0; Date d1(d); Date d2(*this); if(d1>d2) { d1=(*this); d2=d; } while(d1!=d2) { days++; d2-=1; } return days; } bool operator==(const Date& d) { return (d._year==_year && d._month==_month && d._day==_day); } bool operator!=(const Date& d) { return !(d._year==_year && d._month==_month && d._day==_day); } bool operator>(const Date& d) { return ((_year>d._year) || ((_year==d._year) && (_month>d._month)) || ((_year==d._year) && (_month==d._month) &&(_day>d._day))); } bool operator<(const Date& d) { return !((_year>d._year) || ((_year==d._year) && (_month>d._month)) || ((_year==d._year) && (_month==d._month) &&(_day>d._day))); } friend ostream& operator<<(ostream& os,Date& d); friend istream& operator>>(istream& in,Date& d); private: int _year; int _month; int _day; }; ostream& operator<<(ostream& os,Date& d) { os<<d._year<<"-"<<d._month<<"-"<<d._day<<endl; return os; } istream& operator>>(istream& in,Date& d) { in>>d._year; in>>d._month; in>>d._day; return in; } int main() { Date d2; Date d1; int days=0; int input=1; while(input) { cout<<"***********1、通过日期求天数*************************"<<endl; cout<<"***********2、通过天数求日期——日期减天数***********"<<endl; cout<<"***********3、通过天数求日期——日期加天数***********"<<endl; cout<<"请选择:"<<endl; cin>>input; switch(input) { case 1: cout<<"请输入日期:"<<endl; cin>>d1>>d2; cout<<(d1-d2)<<endl; break; case 2: cout<<"请输入日期和天数:"<<endl; cin>>d1; cin>>days; cout<<(d1-days)<<endl; break; case 3: cout<<"请输入日期和天数:"<<endl; cin>>d1; cin>>days; cout<<(d1+days)<<endl; break; } } system("pause"); return 0; }
博主可能随着知识增长会将其做的更加强大,尽请期待~~~~~~
时间: 2024-10-10 09:29:22