风云办公ppt模板网站,深圳游戏网站开发,黄页电话号码,睢宁网站制作一路#xff0c;一路#xff0c;一路从泥泞到风景............................................................................................... 目录
前言
一、【什么是日期类】
二、【代码实现】
1.【Date.h】部分#xff1a;
2.【Date.cpp】部分#xff1a;… 一路一路一路从泥泞到风景............................................................................................... 目录
前言
一、【什么是日期类】
二、【代码实现】
1.【Date.h】部分
2.【Date.cpp】部分
3.【Test.cpp】部分 总结 前言
本篇是日期类的编写望多多指正。 一、【什么是日期类】 在学习完类和对象之后可以试着编写一个日期类进行练习使得该类能够加减天数计算两个日期之间相差的天数还可以比较两个类之间的大小。 二、【代码实现】 1.【Date.h】部分 #define _CRT_SECURE_NO_WARNINGS
#include stdio.h
#include iostream
#include stdbool.h
#include assert.h
using namespace std;
class Date
{
public:friend ostream operator(ostream out, const Date d);friend istream operator(istream in, Date d);int GetMonthDay(int year, int month);Date(int year 1, int month 1, int day 1);Date(const Date d);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);bool operator!(const Date d);Date operator(int day);Date operator(int day)const;Date operator-(int day)const;Date operator-(int day);Date operator();Date operator(int);Date operator--(int);Date operator--();int operator-(const Date d);
private:int _year;int _month;int _day;
}; 2.【Date.cpp】部分 #include Date.h
int Date::GetMonthDay(int year, int month)
{const static int MonthDay[13] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if ((month2)(year % 4 0 year % 100 ! 0 ) || (year % 400 0)){return 29;}return MonthDay[month];
}
Date::Date(int year, int month, int day)
{_year year;_month month;_day day;if (year 1 || month 1 || month12 || day1 || dayGetMonthDay(year,month)){cout error date endl;exit(-1);}
}
Date::Date(const Date d)
{_year d._year;_month d._month;_day d._day;
}
bool Date::operator(const Date d)
{if (_year d._year){return true;}else if (_year d._year _month d._month){return true;}else if (_year d._year _month d._month _day d._day){return true;}else{return false;}
}
bool Date::operator(const Date d)
{return (_year d._year) (_month d._month) (_day d._day);
}
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);
}
bool Date::operator!(const Date d)
{return !(*this d);
}
Date Date::operator(int day)
{if (day 0){return *this - (-day);}_day day;while(_day GetMonthDay(_year, _month)){_day - GetMonthDay(_year, _month);_month;if (_month 12){_year;_month 1;}}return *this;
}
Date Date::operator(int day)const
{Date tmp(*this);tmp day;return tmp;
}
//Date Date::operator-(int day)
//{
// Date tmp(*this);
// tmp._day - day;
// while (tmp._day 0)
// {
// tmp._month--;
// if (tmp._month 1)
// {
// tmp._year--;
// tmp._month 12;
// }
// tmp._day tmp.GetMonthDay(tmp._year, tmp._month);
// }
// return tmp;
//}
//Date Date::operator-(int day)
//{
// *this *this - day;
// return *this;
//}Date Date::operator-(int day)
{if (day 0){return *this (-day);}_day - day;while (_day 0){--_month;if (_month 0){--_year;_month 12;}_day GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day) const
{Date tmp(*this);tmp - day;return tmp;
}
Date Date::operator()
{*this 1;return *this;
}Date Date::operator(int)
{Date tmp(*this);*this 1;return tmp;
}Date Date::operator--()
{*this - 1;return *this;
}Date Date::operator--(int)
{Date tmp(*this);*this - 1;return tmp;
}
int Date::operator-(const Date d)
{Date Max *this;Date Min d;int flag 1;if (*this d){Max d;Min *this;flag -1;}int n 0;while (Min ! Max){Min;n;}return flag * n;}
ostream operator(ostream out, const Date d)
{out d._year / d._month / d._day endl;return out;
}istream operator(istream in, Date d)
{in d._year d._month d._day;return in;
} 3.【Test.cpp】部分 #define _CRT_SECURE_NO_WARNINGS
#include Date.h
int main()
{Date d1(2023,7,1);Date d2(2023, 7, 28);cout d1 endl;cout d2 endl;cout (d2-d1) endl;cout (d2) endl;cout (d2) endl;cout d2 endl; cout (d1--) endl;cout (--d1) endl;Date sum d2 260;cout sum endl;cout (d2480) endl;return 0;
} 总结
本片到这里就结束了感谢观看 .......................................................................................................你过得好吗会想起我吗 ————《你过得好吗》