本文主要是介绍Date类实现(c++),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 前言
- 总体框架
- Date(int year=1, int month=1, int day=1);
- ~Date();
- Date(const Date& d);
- Date& operator=(const Date& d);
- int getMonthDay(int year, int month);
- void Print()const;
- bool operator>(const Date& d)const;
- bool operator==(const Date& d)const;
- bool operator<(const Date& d)const;
- bool operator!=(const Date& d)const;
- bool operator>=(const Date& d)const;
- bool operator<=(const Date& d)const;
- Date operator+(int day);
- Date& operator+=(int day);
- Date operator-(int day);
- Date& operator-=(int day);
- int operator-(const Date&d);
- Date& operator++();
- Date operator++(int);
- Date& operator--();
- Date operator--(int);
- 总结
前言
我们本篇内容将会对Date类进行模拟实现
总体框架
class Date
{
public:
private:int _year=1;int _month=1;int _day=1;
};
Date(int year=1, int month=1, int day=1);
拷贝构造,判断传值是否合法
Date::Date(int year, int month, int day)
{if (year >= 1 && month >= 1 && month <= 12 && day >= 1 && day <= getMonthDay(year, month)){_year = year;_month = month;_day = day;}else{assert(false);}
}
~Date();
析构函数,这里不涉及资源,我们可以不写
Date::~Date()
{}
Date(const Date& d);
拷贝构造,浅拷贝
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}
Date& operator=(const Date& d);
赋值
Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}
int getMonthDay(int year, int month);
获取每月的天数,方便计算。
int Date::getMonthDay(int year, int month)
{int arr[13] = { 0,31,28,31,30,31,30,31,31,30 ,31,30,31 };//闰年:4年一闰年且百年不润年 400年一闰年if (month==2&&((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}return arr[month];
}
void Print()const;
进行简单打印,
void Date::Print()const
{cout << _year << " " << _month << " " << _day << endl;
}
bool operator>(const Date& d)const;
bool Date::operator>(const Date& d)const
{if (_year > d._year){return true;}if (_year == d._year && _month > d._month){return true;}if (_year == d._year && _month > d._month && _day > d._day){return true;}return false;
}
bool operator==(const Date& d)const;
bool Date::operator==(const Date& d)const
{return _year == d._year && _month == d._month && _day == d._day;
}
bool operator<(const Date& d)const;
后面几个接口都是根据前面两个接口进行复用。
bool Date::operator<(const Date& d)const
{return !(*this > d) || (*this == d);
}
bool operator!=(const Date& d)const;
bool Date::operator!=(const Date& d)const
{return !(*this == d);
}
bool operator>=(const Date& d)const;
bool Date::operator>=(const Date& d)const
{return (*this > d) || (*this == d);
}
bool operator<=(const Date& d)const;
bool Date::operator<=(const Date& d)const
{return !(*this > d);
}
Date operator+(int day);
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}
Date& operator+=(int day);
我们这里选用+=复用+,而不是用+复用+=。
可以减少拷贝,提高效率。
Date& Date::operator+=(int day)
{_day += day;while (_day > getMonthDay(_year, _month)){_day -= getMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}
Date operator-(int day);
Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return tmp;
}
Date& operator-=(int day);
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){//+上个月的_month--;if (_month == 0){_year--;_month = 12;}_day += getMonthDay(_year, _month);}return *this;
}
int operator-(const Date&d);
两个日期相减,我们直接进行减是不容易操作的。
int Date::operator-(const Date& d)
{int flag = 1;Date min = *this;Date max = d;if (*this > d){min = d;max = *this;flag = -1;}int n = 0;while (min != max){if (n == 19){int i = 0;}++n;++min;}return n*flag;
}
Date& operator++();
Date& Date::operator++()
{*this += 1;return *this;
}
Date operator++(int);
Date Date::operator++(int)
{Date tmp(*this);*this = *this + 1;return tmp;
}
Date& operator–();
Date& Date::operator--()
{*this = *this -1;return *this;
}
Date operator–(int);
Date Date::operator--(int)
{Date tmp(*this);*this = *this - 1;return tmp;
}
整体代码
#include <iostream>
using namespace std;
#include <assert.h>
class Date
{
public:Date(int year=1, int month=1, int day=1);~Date();Date(const Date& d);Date& operator=(const Date& d);int getMonthDay(int year, int month);void Print()const;bool operator>(const Date& d)const;bool operator<(const Date& d)const;bool operator==(const Date& d)const;bool operator!=(const Date& d)const;bool operator>=(const Date& d)const;bool operator<=(const Date& d)const;Date operator+(int day);Date& operator+=(int day);Date operator-(int day);Date& operator-=(int day);int operator-(const Date&d);Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);private:int _year=1;int _month=1;int _day=1;
};
#include "Date.h"int Date::getMonthDay(int year, int month)
{int arr[13] = { 0,31,28,31,30,31,30,31,31,30 ,31,30,31 };//闰年:4年一闰年且百年不润年 400年一闰年if (month==2&&((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}return arr[month];
}void Date::Print()const
{cout << _year << " " << _month << " " << _day << endl;
}Date::Date(int year, int month, int day)
{if (year >= 1 && month >= 1 && month <= 12 && day >= 1 && day <= getMonthDay(year, month)){_year = year;_month = month;_day = day;}else{assert(false);}
}
Date::~Date()
{
}Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}bool Date::operator>(const Date& d)const
{if (_year > d._year){return true;}if (_year == d._year && _month > d._month){return true;}if (_year == d._year && _month > d._month && _day > d._day){return true;}return false;
}
bool Date::operator==(const Date& d)const
{return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator<(const Date& d)const
{return !(*this > d) || (*this == d);
}
bool Date::operator>=(const Date& d)const
{return (*this > d) || (*this == d);
}
bool Date::operator!=(const Date& d)const
{return !(*this == d);
}
bool Date::operator<=(const Date& d)const
{return !(*this > d);
}//d1+100
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}
Date& Date::operator+=(int day)
{_day += day;while (_day > getMonthDay(_year, _month)){_day -= getMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}
Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return tmp;
}
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){//+上个月的_month--;if (_month == 0){_year--;_month = 12;}_day += getMonthDay(_year, _month);}return *this;
}Date& Date::operator++()
{*this += 1;return *this;
}
Date Date::operator++(int)
{Date tmp(*this);*this = *this + 1;return tmp;
}
Date& Date::operator--()
{*this = *this -1;return *this;
}
Date Date::operator--(int)
{Date tmp(*this);*this = *this - 1;return tmp;
}int Date::operator-(const Date& d)
{int flag = 1;Date min = *this;Date max = d;if (*this > d){min = d;max = *this;flag = -1;}int n = 0;while (min != max){if (n == 19){int i = 0;}++n;++min;}return n*flag;
}
总结
以上就是今天要讲的内容。希望对大家的学习有所帮助,仅供参考 如有错误请大佬指点我会尽快去改正 欢迎大家来评论~~ 😘 😘 😘
这篇关于Date类实现(c++)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!