实现Data类
阅读量:2242 次
发布时间:2019-05-09
本文共 3124 字,大约阅读时间需要 10 分钟。
主要功能
实现日期加日期
实现日期间日起
判断N天后是几月几号
判断N天前是几月几号
实现复制运算符重载
class Date { public: Date(int year = 1900, int month = 1, int day = 0) /*构造函数*/ :_year(year) ,_month(month) ,_day(day) { assert(JudgeDay()); cout<<"Date()"<
if(this != &d) {
_year = d._year ; _month = d._month; _day = d._day; } cout<<"operator=()"<
if ( _year >= 1900 && _month > 0 && _month < 12 && _day > 0 && _day <= GetDay(_year, _month) ) {
return true; } return false; } int GetDay(int year, int month)/*判断这个月有几天*/ {
int numberOfDays; if (month == 4 || month == 6 || month == 9 || month == 11) numberOfDays = 30; else if (month == 2) { bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); if (isLeapYear) numberOfDays = 29; else numberOfDays = 28; } else numberOfDays = 31; //不是二月 return numberOfDays ; } Date operator+(int day)/*" +'号运算符重载* if(day < 0) return *this -="(int" (-day); date tmp(*this); tmp._day +="tmp.GetDay(tmp._year," while (tmp.judgeday()="=" false) tmp._month); if (12="=" tmp._month) 加一年,变成一月 tmp._year++; tmp._month="12;" else 加一个月 tmp._month++; tmp; operator-(int day) *'-'号算符重载* (day (tmp._month="=" 1) tmp._year--; tmp._month--; inline *'-="算符重载*/ {
if (day < 0) {
return *this +=(-day); } this->_day -= day; while (this->JudgeDay() == false) {
if (1 == this->_month) {
this->_year--; this->_month = 12; } else {
this->_month--; } this->_day += this->GetDay(this->_year, this->_month); } return *this; } inline Date& operator +=(int day)/*+=运算符重载*/ {
if (day < 0) {
return *this -= (-day); } this->_day += day; while (this->JudgeDay() == false) {
this->_day -= this->GetDay(this->_year, this->_month); if (12 == this->_month) {
this->_year++; this->_month = 1; } else {
this->_month++; } } return *this; } inline Date& operator++()/*前置++*/ {
return *this+=1; } inline Date operator++(int)/*后置++*/ {
// } inline Date& operator--()/*前置--*/ {
return *this -= 1; } int operator-(const Date& date) /*" -'运算符重载* (this->
_year >= date._year) {
int addYear = this->_year - date._year; if (this->_month >= date._month) {
Date newDate(date); int sum = 0; while (newDate._month != this->_month) {
sum += newDate.GetDay(newDate._year, newDate._month); newDate._month++; } return ( sum += addYear * 365 + this->_day - date._day ); } else if (this->_month < date._month) {
Date newDate(*this); int sum = 0; while (newDate._month != date._month) {
sum += newDate.GetDay(newDate._year, newDate._month); newDate._month++; } return ( sum = addYear*365 - sum + this->_day - date._day ); } } } bool operator<(const Date& date)/*
<运算符重载* { if (this->
_year < date._year || (this->_year == date._year && this->_month < date._month) || (this->_month == date._month && this->_day < date._day) ) {
return true; } return false; } bool operator <=(const Date& date)/*'<'运算符重载*/ {
if ( *this < date || (this->_year == date._year && this->_month == date._month&& this->_day == date._day) ) {
return true; } return false; } bool operator>(const Date& date)/*'>'运算符重载*/ {
if ( this->_year > date._year|| (this->_year == date._year && this->_month > date._month) || (this->_month == date._month && this->_day > date._day) ) {
return true; } return false; } bool operator >=(const Date& date) /*'>='运算符重载*/ {
if (*this > date || (this->_year == date._year && this->_month == date._month && this->_day == date._day) ) {
return true; } return false; } bool operator==(const Date& date) {
if (this->_year == date._year && this->_month == date._month && this->_day == date._day) {
return true; } return false; } bool operator!=(const Date& date) {
if ( this->_year != date._year || (this->_year == date._year && this->_month != date._month )|| (this->_month == date._month && this->_day != date._day) ) {
return true; } return false; } ~Date() {
cout<<" ~Date()"<
运算符重载*>
若有疑问和建议请发邮件到blbagony@163.com
欢迎指出问题