日期类(全)
2016-10-20 23:36
225 查看
日期类一般面试的时候都是只给15到25分钟左右的时间来实现,所以要完成必要的几个功能函数
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> #include<windows.h> using namespace::std; class Date { public: Date(int year = 1990, int month = 1, int day = 1) :_year(year) , _month(month) , _day(day) { if (!(year >= 1990 && (month > 0 && month < 13) && (day>0 && day<GetDaysInMonth(year,month)))) { _year=1990; _month = 1; _day = 1; } } ~Date() {} //获取本月天数 int GetDaysInMonth(int year,int month) { int days[] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (IsLeapYear(year)&&(2==month)) { days[month] = 29; } return days[month]; } //判断闰年 bool IsLeapYear(int year)const { if ((0 == year % 4) && (0 != year % 100) || (0 == year % 400)) { return true; } return false; } //拷贝构造函数 Date(const Date& d) :_year(d._year) , _month(d._month) , _day(d._day) {} //赋值运算符重载 Date& operator=(const Date& d) { if (this != &d) { _year = d._year; _month = d._month; _day = d._day; } return *this; } /* 必要的判断函数 */ 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; } Date operator+(int days) { if (days < 0) { days = 0 - days; return *this - days; } Date temp(*this); temp._day += days; int dayInMonth = 0; while (temp._day > (dayInMonth=GetDaysInMonth(temp._year, temp._month))) { temp._day -= dayInMonth; if (12 == temp._month) { ++temp._year; temp._month = 1; } else { temp._month++; } } return temp; } Date operator-(int days) { if (days < 0) { days = 0 - days; return *this + days; } Date temp(*this); temp._day -= days; while (temp._day <= 0) { if (temp._month == 1) { --temp._year; temp._month = 12; } else { --temp._month; } temp._day += GetDaysInMonth(temp._year, temp._month); } return temp; } /*前置++重载*/ Date& operator++() { if (_day == GetDaysInMonth(_year, _month)) { if (_month == 12) { ++_year; _month = 1; _day = 1; } else { ++_month; _day = 1; } } else { ++_day; } return *this; } //后置++重载 //这里相当于先创建一个临时对象给临时变量*this的内容,然后让this天数++ Date operator++(int) { Date temp(*this); if (_day == GetDaysInMonth(_year, _month)) { if (_month == 12) { ++_year; _month = 1; _day = 1; } else { ++_month; _day = 1; } } else { ++_day; } return temp; } friend ostream& operator<<(ostream& _cout, const Date& d) { _cout << d._year << "-" << d._month << "-" << d._day; return _cout; } int operator-(const Date& d) { Date minDate(*this); Date maxDate(d); if (minDate > maxDate) { std::swap(minDate, maxDate); } int count = 0; while (minDate < maxDate) { count++; ++minDate; } return count; } private: int _year; int _month; int _day; }; FunTest() { Date d1(2016,10,20); Date d2; d2=d1++; } int main() { FunTest() system("pause"); return 0; }
相关文章推荐
- 难点请教:DotNet中使用Table及TableCell时,如有类似日期的值要显示,如何避免显示成日期格式
- 日期加减处理
- 用NSDateFormatter转换日期,得到大写字母的星期(SUN etc.)
- 04 - 基本控件:DatePicker日期选择器、TimePicker时间选择器
- DB2中有关日期和时间的函数,及应用
- 一个JS下拉搜索框,日期级联控件
- 日期计算
- avalon实现日期联动
- js-日期格式处理的几个小技巧
- Spring Boot Spring MVC Rest json输出日期的问题
- 真日期时间格式
- 转换中文日期的PHP程序
- 基姆拉尔森计算公式(根据日期判断星期)
- C# string.Format格式化日期
- jQuery Mobile 移动开发中的日期插件Mobiscroll 2.3 使用说明
- MY97日期选择控件支持日期CTRL多选
- 利用Apache的beanutils判断字符串是否为一个合法日期
- 设置格式,比较日期大小 (alter session set NLS_LANGUAGE = 'AMERICAN')
- 关于日期时间简述
- 第十六周项目—计算日期