日期类(Date)
来源:互联网 发布:c语言实现冒泡排序 编辑:程序博客网 时间:2024/04/29 00:15
日期类的实现在此分三个步骤实现:
一、构建类
class Date{friend ostream& operator<<(ostream& _cout, const Date& d);//友元函数声明public:Date(int year = 2017, int month = 9, int day = 23);~Date();Date& operator=(const Date& d);Date& operator++();//前置++Date operator++(int);//后置++Date& operator--();//前置--Date operator--(int);//后置--Date operator+(int days);//days天之后的日期Date operator-(int days);//days天之前的日期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);int Is_runnian(int year);//判断闰年int Days(int year, int month);//求月的天数bool Is_hefadate(int year, int month, int day);//日期是否合法private:int _year;int _month;int _day;};
二、在类外定义构造函数,析构函数,友元函数
ostream& operator<<(ostream& _cout, const Date& d){_cout << d._year << "/" << d._month << "/" << d._day << endl;return _cout;}//构造函数Date::Date(int year, int month, int day):_year(year), _month(month), _day(day){}//析构函数Date::~Date(){}
三、运算符的重载及相关操作
//赋值运算符重载Date& Date::operator=(const Date& d){if (this == &d){return *this;}else{_year = d._year;_month = d._month;_day = d._day;}return *this;}//前置++Date& Date::operator++(){_day += 1;return *this;}//后置++Date Date::operator++(int){Date temp = *this;_day += 1;return temp;}//前置--Date& Date::operator--(){_day -= 1;return *this;}//后置--Date Date::operator--(int){Date temp = *this;_day -= 1;return temp;}bool Date::operator==(const Date& d){if ((_year == d._year) && (_month == d._month) && (_day == d._day))return true;elsereturn false;}bool Date::operator!=(const Date& d){if ((_year != d._year) || (_month != d._month) || (_day != d._day))return true;elsereturn false;}bool Date::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;elsereturn false;}bool Date::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;elsereturn false;}int Date::Is_runnian(int year){if ((year % 4 && year % 100) || (year % 400)){return 1;}return -1;}int Date::Days(int year, int month){int day;int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };day = days[month - 1];if (month == 2){day += Is_runnian(year);}return day;}bool Date::Is_hefadate(int year,int month,int day){if ((year < 1) || (month < 1) || (month > 13) || (day < 1) || (day>Days(year, month)))return true;elsereturn false;}//days天之后的日期Date Date::operator+(int days){Date temp = *this;if (days < 0){int day = -days;return temp - day;}else{temp._day += days;while (temp._day>Days(temp._day, temp._month)){temp._day -= Days(temp._day, temp._month);if (temp._month == 12){temp._year++;temp._month = 1;}else{temp._month++;}}return temp;}}//days天之前的日期Date Date::operator-(int days){Date temp = *this;if (days < 0){int day = -days;return temp + day;}else{temp._day -= days;while (temp._day <= 0){temp._day = temp._day + Days(temp._day, temp._month);if (temp._month== 1){temp._year--;temp._month = 12;}else{temp._month--;}}return temp;}}//计算量日期之间的距离int Date::operator-(const Date& d){int count = 0;Date max=*this;Date min=d; if (max < min){ max = d; min = *this;} while ((min + count) < max) { count++; } return count;}
到这里,日期类的构建就结束了,不妨,我们来测试一下吧。
void test(){Date d1(2017, 8, 25),d2(2017,7,5),d3;int count = 0;d1++;cout << d1;++d1;cout << d1;d1--;cout << d1;--d1;cout << d1;d3=d1 + 5;cout << d3;d3=d2 - 5;cout << d3;count=d1 - d2;cout << count<<endl;}int main(){test();system("pause");return 0;}
阅读全文
0 0
- Date(日期类)
- 日期类(Date)
- 当前日期(Date类)
- C++中的Date类(日期类)
- 日期处理(Date类 Calendar类)
- 日期操作方法(总结Date,Calender类)
- 日期操作类(Date、Calendar)
- Date Format类(国际化日期)
- 日期操作方法(总结Date,Calender类)
- Date日期类的实现(C++)
- 日期类的实现 class Date()
- Date类日期计算
- Date 日期处理类
- Date日期工具类
- Date 日期类
- 日期类Date
- date 日期工具类
- 日期类Date、Calendar
- 微信内置浏览器 长按识别二维码 功能的两三个坑与解决方案
- CentOS7下使用yum安装Mongodb3.4
- 【测试】学习使用mocha(摩卡)编写测试用例-常用命令
- APACHE BEAM2.0.0版本如何支持SPARK2.0.2
- 支付宝钱包系统架构内部剖析
- 日期类(Date)
- 笔记——二分查找
- 洛谷P1074 [Noip2009]靶形数独
- Java NIO2
- jquery 内部插入与外部插入
- python知识
- Hadoop远程调试
- MT n95 Queries查询
- 数理逻辑习题集(3)