C++日期类(运算符的重载)
来源:互联网 发布:查看mysql用户权限 编辑:程序博客网 时间:2024/05/16 09:56
日期类(运算符的重载)
在C++里可以实现运算符的重载,所以可以实现直接对日期类进行加减、比较等操作。
以下是整个Date.h头文件的源代码:
#pragma once#include<cassert>using namespace std;class Date{public:Date(int year = 1900, int month = 1, int day = 1):_year(year), _month(month), _day(day){assert(IsInvalid()); //判断日期是否合法}Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}~Date(){}void Display() //打印日期类{cout << _year << "-" << _month << "-" << _day << endl;}bool IsInvalid() //判断日期是否合法{if (_year >= 1900 && _month > 0 && _month < 13 && _day>0 && _day <= GetMonthDays(_year, _month)){return true;}return false;}int GetMonthDays(int year, int month) //获取当前月份对应的天数{static int MonthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month != 2){return MonthDays[month];}else{if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) //闰年{return 29;}return 28;}}Date operator+(int day); //重载+Date& operator+=(int day);//重载++Date& operator++(); //前置++Date operator++(int); //后置++Date operator-(int day); //重载-Date& operator-=(int day);//重载-=Date& operator--(); //前置--Date operator--(int); //后置--int operator-(const Date& d); //两日期类对象相减返回一个整数bool operator>(const Date& d); //重载>bool operator<(const Date& d);bool operator>=(const Date& d);bool operator<=(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);private:int _year;int _month;int _day;};inline Date Date::operator+(int day){Date tmp(*this);if (day < 0){return *this - (-day);}tmp._day = _day + day;while (!tmp.IsInvalid()){int MonthDay = GetMonthDays(tmp._year, tmp._month);tmp._day -= MonthDay;tmp._month++;if (tmp._month == 13){tmp._year++;tmp._month = 1;}}return tmp;}inline Date& Date::operator+=(int day){*this = *this + day; //对加法重载的代码复用return *this;}inline Date& Date::operator++() //前置++{return *this+=1;}inline Date Date::operator++(int) //后置++{Date tmp(*this);*this += 1;return tmp;}inline Date Date::operator-(int day){Date tmp(*this);if (day < 0){return *this + (-day);}tmp._day = _day - day;while (!tmp.IsInvalid()){if (tmp._month != 1){tmp._month--;}else //month==1的情况{tmp._year--;tmp._month = 12;}int MonthDay = GetMonthDays(tmp._year, tmp._month);tmp._day += MonthDay;}return tmp;}inline Date& Date::operator-=(int day){*this = *this - day;return *this;}inline Date& Date::operator--() //前置--{return *this -= 1;}inline Date Date::operator--(int) //后置--{Date tmp(*this);*this -= 1;return tmp;}inline bool Date::operator>(const Date& d){if (_year > d._year || (_year == d._year && (_month > d._month || (_month == d._month && _day > d._day)))){return true;}return false;}inline bool Date::operator<(const Date& d){return !(*this > d || *this == d);}inline bool Date::operator>=(const Date& d){return (*this > d || *this == d);}inline bool Date::operator<=(const Date& d){return (*this < d || *this == d);}inline bool Date::operator==(const Date& d){if (_year == d._year && _month == d._month && _day == d._day){return true;}return false;}inline bool Date::operator!=(const Date& d){return !(*this == d);}inline int Date::operator-(const Date& d) //两日期类对象相减返回一个整数{int count = 0;Date tmp(*this);Date tmp2(d);if (tmp < tmp2) //第一个日期比第二个小交换顺序{Date x;x = tmp;tmp = tmp2;tmp2 = x;}//处理_day使两个日期类的_day相同count = GetMonthDays(tmp2._year, tmp2._month) - tmp2._day + tmp._day;tmp2._day = tmp._day;if (tmp._day >= tmp2._day) //count多加了一个月{tmp2._month++;if (tmp2._month == 13){tmp2._year++;tmp2._month = 1;}}//处理_month使两日期类_month相同while (tmp._month != tmp2._month){count = count + GetMonthDays(tmp2._year, tmp2._month);tmp2._month++;if (tmp2._month == 13) {tmp2._year++;tmp2._month = 1;}}//处理两日期类的_yearwhile (tmp._year > tmp2._year){//判断是否为闰年,并且在二月份一下的才能算366天if (((tmp2._year % 4 == 0 && tmp2._year % 100 != 0) || (tmp2._year % 400 == 0)) && (tmp2._month <= 2)){count += 366;}else{count += 365;}tmp2._year++;}return count;}
2 0
- 运算符重载的应用:日期类
- C++日期类(运算符的重载)
- 运算符重载----日期类
- 运算符重载--日期类
- c++_类的运算符重载
- c++日期类的实现级运算符的重载
- C++日期类的运算符重载和简单边界条件
- c++-运算符的重载
- C++--运算符的重载
- 运算符重载(c++)
- C++:运算符重载3(单目:日期自增)
- [C/C++]运算符的重载
- 日期类的实现(其中包括构造函数,拷贝构造,赋值运算符重载,输出运算符重载,友元函数等等)
- 用C/C++实现一个日期类,重载运算符=,==,+,-,++,--,>,>=,<,<=等
- 【C++】模拟实现一个日期类,实现基本重载运算符等
- 重载赋值运算符(c/c++)
- C++-运算符重载
- [C++]重载运算符
- tableview 自定义cell 不显示 self 与 sel.contentview 的区别 多选右移cell不移动的问题
- HTML 转义字符
- Lua——table.insert
- Linux Kernel 设备驱动之I2C之host描述
- CFileDialog类应用详解
- C++日期类(运算符的重载)
- Win7彻底卸载Oracle 11g(图文步骤)
- git 新建本地仓库,到提交
- Jsoup实现网络爬虫抓取数据
- CODEVS 1230 元素查找
- CT107D蓝桥杯EEPROM使用(8)
- 解决 vc6 unresolved external symbol ___security_cookie 问题
- follow Sam哥 OS 课程的漫长之路 Day2
- 数据库MySql类库系列(九)-DBServicePool