万年历
来源:互联网 发布:php中变量的作用域 编辑:程序博客网 时间:2024/06/06 05:27
#include<iostream>using namespace std;class Date{public: // 初始化列表进行初始化。 Date(int year = 1900, int month = 1, int day = 1) //构造函数 :_year(year) , _month(month) , _day(day) { if (_year < 1900 || _month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(year,month)) { cout << "Invalue Date" << endl; //如果传入非法日期就恢复初始日期 _year = 1900; _month = 1; _day = 1; } } Date(const Date& d)//拷贝构造 :_year(d._year) , _month(d._month) , _day(d._day) { } Date& operator= (const Date& d) { _year = d._year; _month = d._month; _day = d._day; return *this; } void Display() { cout << _year << "-" << _month << "-" << _day << endl; } static int GetMonthDay(int year,int month) { int MonthDay[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) //判断闰年 { MonthDay[2] = 29; } else { MonthDay[2] = 28; } return MonthDay[month]; }public: // // 比较日期的运算符重载 // bool operator == (const Date& d) { /*if ((_year == d._year) && (_month == d._month) && (_day == d._day)) { return true; } else { return false; }*/ 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); } // // 计算一个日期加减一个天数以后的日期。 // Date operator+(int day) { Date tmp(*this); tmp._day += day; while (tmp._day > GetMonthDay(tmp._year, tmp._month)) { tmp._day -= GetMonthDay(tmp._year, tmp._month); tmp._month++; if (tmp._month > 12) { tmp._year++; tmp._month = 1; } } return tmp; } Date operator-(int day) { Date tmp(*this); tmp._day -= day; while (tmp._day <= 0) { tmp._month--; if (tmp._month < 1) { tmp._year--; tmp._month = 12; } tmp._day += GetMonthDay(tmp._year, tmp._month); } return tmp; } Date& operator-=(int day) { return (*this) = (*this - day); } Date& operator+=(int day) { return (*this) = (*this + day); } const Date& operator++() // 前置++ { (*this) += 1; return (*this); } Date operator++(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) { Date d1(*this), d2(d); if (*this < d) { Date tmp = d1; d1 = d2; d2 = tmp; } int count = 0; while (d1 > d2) { count++; d2++; } cout<<count<<endl; return count; }private: int _year; int _month; int _day;};void TestDate(){}int main(){ Date d1(1995,4,19),d2(1995,4,19),d3,d4; d1.Display(); cout << "d1 = d2 ?" << (d1 == d2) << endl; cout << "d1 != d2 ?" << (d1 != d2) << endl; cout << "d1 > d2 ?" << (d1 > d2) << endl; cout << "d1 >= d2 ?" << (d1 >= d2) << endl; cout << "d1 < d2 ?" << (d1 < d2) << endl; d3 = d1 + (-19); d3.Display(); d4 = d2 - 365; d4.Display(); /*d2 -= 30; d2.Display(); d1 += 30; d1.Display();*/ /*d1--; d1.Display();*/ d1 - d2; getchar(); return 0;}
0 0
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 万年历
- 面试总结
- Use Windows PowerShell cmdlets to manage sites in SharePoint 2013
- 爱情,不过是 需要 和 被需要 的游戏
- C语言实现链式队列
- 1200 -- 无聊又简单的游戏
- 万年历
- Cocos2dx之入门基础
- 【LeetCode】之Add Two Numbers
- 特权模式
- 12bit,24bit,32bit颜色
- Bootstrap typeahead使用问题记录及解决方案
- LeetCode Minimum Path Sum(动态规划)
- Struts2核心工作原理解析
- TCP/IP源码学习(47)——socket与VFS的关联(1) 2 http://blog.chinaunix.net/uid-23629988-id-3080166.html