【C++】日期类+日期万年历+日期计算器
2016-05-30 15:04
288 查看
对于日期类,我们主要实现一下日期类的基本函数,构造,拷贝构造,运算符的重载,析构。当然这里运算符的重载需要实现的还是挺多的,如:=、<、>、<=、>=、等
对于实现日期计算器,我们主要考虑的是加天数和减天数,那么问题就来了,对于加法,如果加的日期超过当前月的天数就需要考虑月的进位,对于年来说,如果月份大于12就需要重置为1,年进位。还需要考虑的一个问题就是,是否为闰年的2月份天数不同,那么应该如何解决呢?我们用一个数组把每个月的天数给保存起来,然后写一个判断闰年的函数,如果是闰年就在数组对应的2月上加上1天。对于减法,就相当于加上一个负天数,问题和加法一样。
本文出自 “滴水” 博客,请务必保留此出处http://10740329.blog.51cto.com/10730329/1765067
#include <iostream> using namespace std; class Date { public: Date(int year = 1990, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; } ~Date() {} //万年历 bool operator == (const Date& d) { return this->_year == d._year && this->_month == d._month && this->_day == d._day; } 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); } 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); }
对于实现日期计算器,我们主要考虑的是加天数和减天数,那么问题就来了,对于加法,如果加的日期超过当前月的天数就需要考虑月的进位,对于年来说,如果月份大于12就需要重置为1,年进位。还需要考虑的一个问题就是,是否为闰年的2月份天数不同,那么应该如何解决呢?我们用一个数组把每个月的天数给保存起来,然后写一个判断闰年的函数,如果是闰年就在数组对应的2月上加上1天。对于减法,就相当于加上一个负天数,问题和加法一样。
// 日期计算器 Date operator+ (int day); Date operator+= (int day); Date operator- (int day) { this->_day -= day; while (_day < 0) { _day += GetMonthDay(2016, 3); _month -= 1; if (_month < 1) { _month = 12; _year -= 1; } } return *this; } Date operator-= (int day); Date operator++(); Date operator++(int); Date operator--(); Date operator--(int); int operator-(const Date& d); //计算器 Date& calendar(int day = 0) { if (day > 0)//加正天数 { this->_day += day; while (_day > GetMonthDay(2016, 2)) { _day -= GetMonthDay(2016, 2); _month += 1; if (_month > 12) { _month = 1; _year += 1; } } } else//加负天数 { this->_day -= day; while (_day < 0) { _day += GetMonthDay(2016, 3); _month -= 1; if (_month < 1) { _month = 12; _year -= 1; } } } return *this; } private: bool IsLeapYear(int year) { if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { return true; } return false; } int GetMonthDay(int year, int month) { int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int day = monthArray[month]; if (month == 2 && IsLeapYear(year)) { day += 1; } return day; } private: int _year; int _month; int _day;
本文出自 “滴水” 博客,请务必保留此出处http://10740329.blog.51cto.com/10730329/1765067
相关文章推荐
- 【C++】智能指针的原始写法、scoped写法、shared写法
- 【C语言】【面试题】【笔试题】使用main函数实现一个整数计算器!
- 【C语言】【面试题】【笔试题】模拟实现数据库中memmove函数
- 【C语言】【面试题】【笔试题】模拟实现数据库中memcpy函数
- 【C语言】【面试题】【笔试题】模拟实现数据库中strcpy函数
- 【C语言】【面试题】【笔试题】模拟实现数据库中strcat函数
- 【C语言】【面试题】【笔试题】模拟实现数据库中strcmp函数
- 【C语言】【面试题】【笔试题】二维数组中的查找,杨氏矩阵
- 【C语言】【面试题】【笔试题】模拟实现strstr函数
- 【C语言】在终端输入多行信息,找出包含“ould”的行,并打印改行
- 【C语言】【面试题】【笔试题】编写一个函数实现n^k,使用递归实现
- 【C语言】【面试题】【笔试题】写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和
- 【C语言】编写一个函数reverse_string(char * string)(递归实现
- 【C语言】【笔试题】模拟实现strncat
- 【C语言】【面试题】【笔试题】模拟实现strncmp
- 【C语言】【面试题】【笔试题】模拟实现strncpy
- 【C语言】【笔试题】模拟实现memset
- 【C语言】【笔试题】模拟实现memcmp
- 【C语言】【指针相关知识小结】
- 【C语言】【笔试题】使用回调函数编写冒泡排序,可以排序整形数组,也可以排序字符串