日期计算器
2016-02-19 12:57
197 查看
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<Windows.h> using namespace std; class Date { public: Date(int _year, int _month, int _day); //构造函数 void ShowDate() { cout << year << "-" << month << "-" << day << endl; } 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); //大于等于运算符的重载 Date operator+(int day); //某个日期+天数 Date &operator+=(int day); //某个日期+=天数 Date operator-(int day); //某个日期-天数 Date &operator-=(int day); //某个日期-=天数 Date& operator++(); //某个日期++(前置) Date operator++(int); //某个日期++(后置) Date& operator--(); //某个日期--(前置) Date operator--(int); //某个日期--(后置) int operator-( Date& d); //两个日期相减 friend bool IsTrueDate(Date &d); //友元函数,判断某个日期是否是合法日期 private: int year; int month; int day; }; Date::Date(int _year, int _month, int _day) : // 用初始化列表初始化构造函数 year(_year), month(_month), day(_day) { } bool Date::operator<(const Date& d) //小于运算符的重载 { return this->year < d.year || this->year == d.year && this->month<d.month || this->year == d.year && this->month == d.month && this->day<d.day; } bool Date::operator==(const Date& d) //等于运算符的重载 { return this->year == d.year && this->month == d.month && this->day == d.day; } bool Date::operator>(const Date& d) //大于运算符的重载 { return this->year > d.year || this->year == d.year && this->month>d.month || this->year == d.year && this->month == d.month && this->day>d.day; } bool Date::operator<=(const Date& d) //小于等于运算符的重载 { return !(*this > d); } //bool Date::operator<=(const Date& d) //小于等于运算符的重载 //{ //return *this < d && *this == d; //} bool Date::operator>=(const Date& d) //大于等于运算符的重载 { return !(*this < d); } //bool Date::operator>=(const Date& d) //大于等于运算符的重载 //{ //return *this > d && *this == d; //} bool IsLeapYear(int year) //判断某年是否是闰年 { if ((year % 400 == 0) || ((year % 4 == 0) && (year % 1001 != 0))) { return true; } else 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; } //日期计算器 Date& Date:: operator += (int day) { *this=*this + day; return *this; } Date Date::operator+ (int day) { if (day<0) { *this -(-day); } Date tmp(*this); if (IsTrueDate(*this)) { tmp.day += day; while (tmp.day > GetMonthDay(tmp.year, tmp.month)) { tmp.day -= GetMonthDay(tmp.year, tmp.month); if (tmp.month == 12) { tmp.year++; tmp.month = 1; } else { tmp.month++; } } } else { cout << "The date is eror!" << endl; tmp.year = 1900; tmp.month = 1; tmp.day = 1; } return tmp; } Date &Date::operator -= (int day) { *this = *this - day; return *this; } Date Date::operator - (int day) { if (day<0) { *this + (-day); } Date tmp(*this); if (IsTrueDate(*this)) { tmp.day -= day; while (tmp.day <= 0) { if (tmp.month == 1) { tmp.month = 12; tmp.year--; } else { tmp.month--; } tmp.day += GetMonthDay(tmp.year, tmp.month); } } return tmp; } Date Date::operator--(int) //后置-- { Date tmp(*this); *this -= 1; return tmp; } Date Date::operator++(int) //后置++ { Date tmp(*this); *this += 1; return tmp; } Date& Date::operator--() //前置-- { *this -= 1; return *this; } Date& Date::operator++() //前置++ { *this += 1; return *this; } bool IsTrueDate(Date &d) { if (d.year > 1900 && d.month > 0 && d.month<13 && d.day>0 && d.day <= GetMonthDay(d.year, d.month)) { return true; } else { cout << "The date is eror!" << endl; d.year = 1900; d.month = 1; d.day = 1; return false; } } int Date::operator - ( Date& d) { int days=0; int flag = 1; if (IsTrueDate(*this) && IsTrueDate(d)) { while (!(*this == d)) { if (*this > d) { d.day++; days++; } else { this->day++; days++; flag = -1; } } } return flag * days; }
相关文章推荐
- Ruby 之 class 中的 private、 protected、public
- Ruby中require、load、include、extend的区别介绍
- vbscript include的办法实现代码第1/2页
- 解析C++编程中的#include和条件编译
- PHP脚本中include文件出错解决方法
- Flex include和import ActionScript代码
- set_include_path在win和linux下的区别
- C#日历样式的下拉式计算器实例讲解
- Ruby中的public、private、protected区别小结
- php include加载文件两种方式效率比较
- 纯js代码实现简单计算器
- JavaScript编写简单的计算器
- How to Auto Include a Javascript File
- 基于javascript实现简单计算器功能
- 浅谈ASP.NET的include的使用方法
- 一个简单的jQuery计算器实现了连续计算功能
- 基于HTML+CSS,jQuery编写的简易计算器后续(添加了键盘监听)
- js表达式计算器代码
- java/jsp中 中文问题详解
- php相对当前文件include其它文件的方法