实现日期类
来源:互联网 发布:小视频配音软件 编辑:程序博客网 时间:2024/05/29 14:59
#include <iostream>#include<Windows.h>using namespace std;class Date{ friend ostream& operator<<(ostream& _cout, const Date& d);public: Date(int year = 1900, int month = 1, int day = 1) : _year(year) , _month(month) , _day(day) { if ((_year<0)|| ((_month<1) || (_month>12)) || ((_day<0) || (_day>GetMonthdays(month)))) { _year = 1990; _month = 1; _day = 1; } } Date operator+(int days) { if (days < 0) { return *this - (0 - days); } Date temp(*this); temp._day += days; int daysinmonth; while (temp._day > (daysinmonth =temp.GetMonthdays(temp._month))) { temp._day -= daysinmonth; temp._month++; if (temp._month > 12) { temp._year++; temp._month = 1; } } return temp; } Date operator-(int days) { if (days < 0) { return *this + (0 - days); } Date temp(*this); temp._day -= days; int daysinmonth = temp.GetMonthdays(temp._month-1); while (temp._day <1) { temp._month--; if (temp._month <1) { temp._year--; temp._month = 12; } temp._day += (daysinmonth=temp.GetMonthdays(temp._month )); } return temp; } Date& operator++() { *this = *this + 1; return *this; } Date operator++(int) { Date temp = *this; ++(*this); return temp; } Date& operator--() { *this = *this - 1; return *this; } Date operator--(int) { Date temp = *this; --(*this); return temp; } bool operator>(const Date& d) { if ((_year > d._year) || ((_year == d._year) && (_month > d._month)) || ((_year == d._year) && (_month == d._month) && (_day > d._day))) { return true; } return false; } bool operator<(const Date& d) { if ((_year < d._year) || ((_year == d._year) && (_month < d._month)) || ((_year == d._year) && (_month == d._month) && (_day < d._day))) { return true; } return false; } bool operator==(const Date& d) { if (((_year == d._year) && (_month == d._month) && (_day == d._day))) { return true; } return false; } bool operator!=(const Date& d) { if (((_year != d._year) ||(_month != d._month) || (_day != d._day))) { return true; } return false; } int operator-(const Date& d) { int sumDay = 0; if (*this == d) { return 0; } else if (*this>d) { Date temp = d; while (temp != *this) { temp++; sumDay++; } return sumDay; } else { Date temp = *this; while (temp != d) { temp++; sumDay++; } return sumDay; } }private: int _year; int _month; int _day; int GetMonthdays(int month) { int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (Isleapyear()&&2 == month ) { days[2] += 1; } return days[month]; } bool Isleapyear()const { if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0)) { return true; } return false; }};ostream& operator<<(ostream& _cout, const Date& d){ _cout << d._year << "-" << d._month<<"-"<<d._day; return _cout;}int main(){ Date d1(2017, 2, 28); Date d2(2016, 2, 28); int sum = d2- d1; cout << d1; system("pause"); return 0;}
0 0
- C++日期实现类
- 实现日期类
- 日期类的实现
- 实现日期类
- C++实现日期类
- 日期类的实现
- C++实现日期类
- 实现日期类
- 日期类的实现
- 日期类的实现
- 日期类的实现
- C++实现日期类
- 【日期类】C++实现
- 模拟实现日期类
- 日期类的实现
- 日期类的实现
- 实现日期类(日期计算器)
- 自己实现的日期类
- Cookie的一些API及注意事项
- HttpClient 中的get, post使用示例
- 在Linux安装redis
- Xcode8不能使用快捷键“command+/”注释代码的解决方法
- oj刷题(坑题)Problem J: C++结构体之统计最高最低分
- 实现日期类
- 解决onActivityResult方法不执行的办法
- js面向对象--封装
- 向量知识应用学习
- 清空SQL数据库日志
- Google工程师:教你用树莓派+Arduino+TensorFlow搭建图像识别小车
- 【面试】JavaBean的使用
- 有什么pdf转换jpg的工具
- nginx配置入门