日期类的实现
来源:互联网 发布:熊片数据库手机版 2.1 编辑:程序博客网 时间:2024/05/26 02:55
#include <iostream>using namespace std;class Date{ friend ostream& operator<<(ostream& _cout, const Date& date); friend istream& operator>>(istream& _cin, Date& date);public: Date(int year, int month, int day) : _year(year) , _month(month) , _day(day) {} Date(const Date& d) : _year(d._year) , _month(d._month) , _day(d._day) {} Date& operator=(const Date& d); // 计算当前日期day天之后日期 Date& operator-(int day); // 计算当前日期day天之前日期 Date& operator+(int day); // 计算两个日期之间差距 int operator-(const Date& d); //前置++ Date& operator++(); // 后置++ Date operator++(int); Date& operator--(); Date operator--(int); bool operator>(const Date& d); bool operator<(const Date& d); bool operator==(const Date& d); bool operator!=(const Date& d); // 重载输出运算符 private: bool IsLeapYear(int year) { return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); } int GetDayInMonth(int year, int month) { int months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 }; if(IsLeapYear(year)) { months[2] = 29; } return months[month]; }private: int _year; int _month; int _day;};ostream& operator<<(ostream& _cout, const Date& date){ _cout << date._year << "/" << date._month << "/" << date._day; return _cout;}istream& operator>>(istream& _cin, Date& date){ _cin >> date._year >> date._month >> date._day; return _cin;}Date& Date::operator=(const Date& date){ if (this != &date) { _year = date._year; _month = date._month; _day = date._day; } return *this;}Date& Date::operator+(int day){ Date& tmp = *this; tmp._day += day; while (tmp._day > GetDayInMonth(tmp._year, tmp._month)) { tmp._day -= GetDayInMonth(tmp._year, tmp._month); if (tmp._month == 12) { tmp._year++; tmp._month = 1; } else tmp._month++; } return tmp;}Date& Date::operator-(int day){ Date& tmp = *this; tmp._day -= day; while (tmp._day < 0) { if (tmp._month == 1) { tmp._year--; tmp._month = 12; } else { --tmp._month; } tmp._day += GetDayInMonth(tmp._year, tmp._month); } return tmp;}int Date::operator-(const Date& date){ Date datemax = *this; Date datemin = date; int days = 0; if (datemin > datemax) { datemax = date; datemin = *this; } while (1) { if (datemin + days == datemax) break; days++; } return days;}//前置Date& Date::operator++(){ Date& tmp = *this; tmp._day++; if (tmp._day > GetDayInMonth(tmp._year, tmp._month)) { if (tmp._month == 12) { tmp._year++; tmp._month = 1; tmp._day = 1; } tmp._month++; tmp._day = 1; } return tmp; //return (*this = *this +1);}Date& Date::operator--(){ return (*this = *this - 1);}//后置Date Date::operator++(int day){ Date tmp = *this; *this = *this + 1; return tmp;}Date Date::operator--(int day){ Date tmp(*this); *this = *this - 1; return tmp;}bool Date::operator>(const Date& d){ if (this->_year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day)) return true; else return false;}bool Date::operator<(const Date& d){ if (this->_year < d._year || (_year == d._year && _month < d._month) || (_year == d._year && _month == d._month && _day < d._day)) return true; else return false;}bool Date::operator==(const Date& d){ if (_year == d._year && _month == d._month && _day == d._day) return true; else return false;}bool Date::operator!=(const Date& d){ if (_year == d._year && _month == d._month && _day == d._day) return false; else return true;}void FunTest(){ Date date(2016, 11, 17); cout << ++date << endl; cout << --date << endl; cout << date << endl; cout << date + 9 << endl; cout << date - 9 << endl; cout << date-- << endl; cout << date++ << endl; cout << date << endl; date = Date(2016, 10, 17); cout << date + 100 << endl; Date date2(1996, 3, 1); cout << date2 - 100 << endl; cout << (date - date2) << endl;}int main(){ FunTest(); getchar(); return 0;}
阅读全文
0 0
- 日期类的实现
- 日期类的实现
- 日期类的实现
- 日期类的实现
- 日期类的实现
- 日期类的实现
- 日期类的实现
- 自己实现的日期类
- C++:日期类的实现
- C++日期类的实现
- c++日期类的实现
- C++日期类的实现
- 日期类的简单实现
- C++日期类的实现
- 【c++】日期类的实现
- 完成日期类的实现
- c++日期类的实现
- C++ 自定义日期类实现日期的加减算法
- git flow介绍
- 小仙女滴大二修仙之路
- 每一次技术进步,都有大公司接盘的身影
- Spring-AOP
- 关于Unity粒子系统优化,你可能遇到这些问题
- 日期类的实现
- 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字
- 虚拟机Hadoop-2.7.1环境搭建(CentOS-6.5)
- 2017第三届智能制造与工业4.0国际峰会即将于11月震撼来袭!
- 二.ARM裸机学习之S5PV210刷机
- 将spring cloud打包docker镜像
- ansible-playbook批量部署nginx
- angularjs中如何实现控制器和指令之间交互
- [NOIP2015]运输计划 树链剖分 二分 差分