一、万年历类中所包含函数,以及功能
/******************************************************************************************
Date.hpp:
class Time
{
二、代码
#include <iostream> using namespace std; class Time { public: Time(int hour=0,int minute=0,int second=0) { //cout<<"构函数"<<endl; _hour=hour; _minute=minute; _second=second; } friend class Date; Time(const Time& t) { //cout<<"copy构造函数"<<endl; } Time& operator=(const Time& t) { _hour = t._hour; _minute=t._minute; _second=t._second; return *this; } private: int _hour; int _minute; int _second; }; class Date { public: // 初始化列表进行初始化 //Date (int year = 1900, int month = 1, int day = 1,int hour=0):_t(hour) //{ //// cout<<"构造函数"<<endl; // _year = year; // _month = month; // _day = day; // if (CheckDate() == false) // { // Date(); // } //} Date (int year = 1900, int month = 1, int day = 1) :_year(year) ,_month(month) ,_day(day) { // 检查如果输入参数是非法时间,则初始化为1900-1-1 if (CheckDate()) { year = 1900; month = 1; day = 1; } } Date (const Date& d) { _year = d._year; _month = d._month; _day = d._day; //cout<<"copy构造函数"<<endl; } Date& operator= (const Date& d) { _year=d._year; _month=d._month; _day=d._day; return *this; } void Display (Time& t) { cout<<_year<<"-"<<_month<<"-"<<_day<<endl; cout<<t._hour<<":"<<t._minute<<":"<<t._second<<endl; } public: //日期检查 bool CheckDate () { if (_year < 1 || ( _month < 1 || _month > 12) || ( _day < 1 || _day > MonthDay(_year, _month ))) { return true ; } return false ; } // // 比较日期的运算符重载 // 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)&&(_month > d._month)) { return true; } else if((_year == d._year)&&(_month == d._month)&&(_day >d._day)) { return true; } else { 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); } // 将日期转换为正确的日期 //注意此函数功能多次用到,因此进行单个包装,用来减短代码长度!!! void _ToCrrectDate() { while(_day < 0) { _day += MonthDay (_year, _month); if (_month == 1) { _year--; _month = 12; } else { _month--; } } while (_day > MonthDay(_year, _month)) { _day -= MonthDay (_year, _month); if (_month == 12) { _year++; _month = 1; } else { _month++; } } } // // 计算一个日期加减一个天数以后的日期。 // Date operator+(int day) { Date tmp(*this); tmp._day+=day; tmp._ToCrrectDate(); return tmp; } Date operator-(int day) { Date tmp(*this); tmp._day=tmp._day-day; tmp._ToCrrectDate(); return tmp; } Date& operator-=(int day) { this->_day -= day; this->_ToCrrectDate(); return *this; } Date& operator+=(int day) { this->_day += day; this->_ToCrrectDate(); return *this; } const Date& operator++() // 前置++ { *this += 1; return *this; return *this; } Date operator++(int) // 后置++ 若没有加上(int),为何有如此多的错误 { Date tmp(*this); *this += 1; return tmp; } const Date& operator--() // 前置-- { *this -= 1; return *this; } Date operator--(int) // 后置-- { Date tmp(*this); *this -= 1; return tmp; } // // 两个日期相加没有意义 // 计算两个日期相减以后的差的天数 // int operator-(const Date& d) { int flag = 1; //标示量 Date x1 = *this, x2 = d; if (x1 < x2) { flag = -1; x1 = d; x2 = *this; } int Days = 0; //日期相差天数 while (x1 != x2) { ++x2; Days++; } return Days*flag; } int MonthDay(int year,int month) { if (year < 1 || month < 1 || month > 12) { return 0; } static int array[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; //注意static的作用 if(((year%4)&&(year%100)!=0)||(year%400==0)) { array[2]=28; } return array[month]; } friend ostream& operator<<( ostream& os , const Date& d ); friend istream& operator>>( istream& is , Date& d); private: int _year; int _month; int _day; /*const int _testConst; int& _testRef;*/ Time _t; }; ostream& operator<<( ostream& os , const Date& d ) { os<<d._year<<"-"; os<<d._month<<"-"; os<<d._day; return os ; } istream& operator>>( istream& is , Date& d) { cout<<" 请分别输入年月日: "<<endl ; is>>d._year; is>>d._month; is>>d._day; return is ; } void PromptInfo() { cout<<"========日期计算器======="<<endl; cout<<"0.退出"<<endl; cout<<"1.推后几天的日期"<<endl; cout<<"2.计算日期差"<<endl; cout<<"3.清屏"<<endl; cout<<"========================="<<endl; } void main() { Date d1, d2; int in, days; do { PromptInfo(); cin>>in; switch(in) { case 0: break; case 1: cin>>d1; if (d1.CheckDate()) { cout<<"输入日期非法!"<<endl; break; } cout<<"请出入计算推后的天数"<<endl; cin>>days; d1+=days; cout<<d1<<endl; break; case 2: cin>>d1; if (d1.CheckDate()) { cout<<"输入日期非法!"<<endl; break; } cin>>d2; if (d2.CheckDate()) { cout<<"输入日期非法!"<<endl; break; } days = d1 - d2; cout<<"相差的天数:"<<days<<endl; break; case 3: system("CLS"); break ; default: cout<<"选项错误,请重新选择"<<endl; break; } }while(in != 0); getchar(); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zhangchaoq/article/details/48135553