> 文章列表 > [C++]日期类计算器的模拟实现

[C++]日期类计算器的模拟实现

[C++]日期类计算器的模拟实现

目录

日期类计算器的模拟实现::

                                                1.获取某年某月的天数

                                                2.构造函数

                                                3.拷贝构造函数

                                                4.赋值运算符重载

                                                5.析构函数

                                                6.日期+=天数

                                                7.日期+天数

                                                8.日期-天数

                                                9.日期-=天数

                                               10.前置++的运算符重载

                                               11.后置++的运算符重载

                                               12.前置--的运算符重载

                                               13.后置--的运算符重载

                                               14.>的运算符重载

                                               15.<的运算符重载

                                               16.==的运算符重载

                                               17.>=的运算符重载

                                               18.<=的运算符重载

                                               19.!=的运算符重载

                                               20.<<的运算符重载

                                               21.>>的运算符重载

                                               22.日期-日期


日期类计算器的模拟实现::

1.获取某年某月的天数

int GetMonthDay(int year, int month)
{static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}else{return monthDayArray[month];}
}

2.构造函数

Date(int year = 1, int month = 1, int day = 1)
{_year = year;_month = month;_day = day;//检查日期是否合法if (!((year >= 1)&& (month >= 1 && month <= 12)&& (day >= 1 && day <= GetMonthDay(year, month)))){cout << "非法日期" << endl;}
}

3.拷贝构造函数

// 拷贝构造函数 形参加const 防止写反了 问题就可以检查出来了
Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}

4.赋值运算符重载

//d1 = d2 
//注:1.要注意两个参数的顺序 2.这里面参数不加引用不会导致无穷递归 但为了避免拷贝构造最好加引用
Date& operator=(const Date& d)
{//为了支持链式赋值 if是为了避免自己给自己赋值 d1 = d1if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}

5.析构函数

~Date()//可不写
{;
}

日期类因为没有申请资源,所以无需写析构函数,编译器默认生成的析构函数就可以。

6.日期+=天数

//d1 += 100
//天满了进月 月满了进年 
Date& operator+=(int day)
{//避免 d1 += -1000的情形if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){++_year;_month = 1;}}return *this;
}

7.日期+天数

//d1 + 100
Date operator+(int day) const
{Date ret(*this);ret += day;//ret.operator+=(day)return ret;
}

8.日期-天数

//d1 - 100
Date operator-(int day) const
{Date ret(*this);ret -= day;return ret;
}

9.日期-=天数

//d1 -= 100
Date& operator-=(int day)
{//避免 d1 -= -1000if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}

10.前置++的运算符重载

//前置++
Date& operator++()
{//会调用 operator+=(int day)*this += 1;return *this;
}

11.后置++的运算符重载

//后置++ —多一个int参数主要是为了和前置++进行区分 构成函数重载
Date operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}

12.前置--的运算符重载

//前置--
Date& operator--()
{//复用运算符重载-=*this -= 1;return *this;
}

13.后置--的运算符重载

//后置--
Date operator--(int)
{Date tmp = *this;*this -= 1;return tmp;
}

14.>的运算符重载

//d1 > d2
bool operator>(const Date& d) const
{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;}return false;
}

15.<的运算符重载

//d1 < d2
bool operator<(const Date& d) const
{return !(*this >= d);
}

16.==的运算符重载

//d1 == d2
bool operator==(const Date& d) const
{ 	return _year == d._year&& _month == d._month&& _day == d._day;
}

17.>=的运算符重载

//d1 >= d2
bool operator>=(const Date& d) const
{return *this > d || *this == d;
}

18.<=的运算符重载

//d1 <= d2
bool operator<=(const Date& d) const
{return !(*this > d);
}

19.!=的运算符重载

//d1 != d2
bool operator!=(const Date& d) const
{return !(*this == d);
}

20.<<的运算符重载

//内联函数和静态成员一样 调用处展开 不进符号表
inline ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}

21.>>的运算符重载

//cin >> d1 编译器转化成operator(cin,d1) 形参中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}

22.日期-日期

//日期-日期
int operator-(const Date& d) const
{Date max = *this;Date min = d;int flag = 1;if (*this < d)//总结:凡是内部不改变成员变量 也就是不改变*this数据的 这些成员函数都应该加const//if (d > *this){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++n;//复用++ ++到和d1日期相等 就是相差多少天++min;}return n * flag;
}

Date.h

#pragma once
#include <iostream>
using namespace std;
class Date
{//友元声明(类的任意位置)声明友元时可以不用加inlinefriend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:int GetMonthDay(int year, int month){static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}else{return monthDayArray[month];}}Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;//检查日期是否合法if (!((year >= 1)&& (month >= 1 && month <= 12)&& (day >= 1 && day <= GetMonthDay(year, month)))){cout << "非法日期" << endl;}}// 拷贝构造函数 形参加const 防止写反了 问题就可以检查出来了Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}//d1 == d2bool operator==(const Date& d) const;//d1 > d2bool operator>(const Date& d) const;//d1 >= d2bool operator>=(const Date& d) const;//d1 <= d2bool operator<=(const Date& d) const;//d1 < d2bool operator<(const Date& d) const;//d1 != d2bool operator!=(const Date& d) const;//d1 += 100Date& operator+=(int day);//d1 + 100Date operator+(int day) const;//d1 = d2 注:1.要注意两个参数的顺序 2.这里面参数不加引用不会导致无穷递归 但为了避免拷贝构造最好加引用Date& operator=(const Date& d){//为了支持链式赋值 if是为了避免自己给自己赋值 d1 = d1if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;}//d1 -= 100Date& operator-=(int day);//d1 - 100Date operator-(int day) const;//++的操作数只有一个 不传参//前置++Date& operator++();//编译器为了区分前置++和后置++ 规定在后置的函数上加了一个参数//后置++Date operator++(int);//允许成员函数加const 此时this指针的类型为:const Date* const thisvoid Print() const{cout << _year << "/" << _month << "/" << _day << endl;}//前置--Date& operator--();//后置--Date operator--(int);//日期-日期int operator-(const Date& d) const;//流插入//d1 << cout编译器会转化成d1.operator<<(cout) this指针抢了左操作数d1的位置//<<和>>的重载一般不写成成员函数 因为this默认抢了第一个参数的位置 Date类对象就是左操作数 不符合使用习惯和可读性/*void operator<<(ostream& out){out << _year << "年" << _month << "月" << _day << "日" << endl;}*///取地址重载Date* operator&(){return this;}//const成员取地址重载const Date* operator&() const{return this;}//取地址重载和const成员取地址重载不实现 编译器会默认生成
private:int _year;int _month;int _day;
};
//结论:对于自定义类型,尽量用前置,减少拷贝,提高效率
//全局函数调用:cout << d1转化成operator<<(cout,d1)
//全局函数的定义和全局变量不能放在.h文件中 因为函数的定义在Date.cpp和test.cpp都会展开 函数地址进入符号表 链接器链接两个.cpp文件时相同的函数地址会报错
//解决方法:1.改成静态 2.声明和定义分离
//static修饰函数只在当前文件可见 不会进入符号表
//static void operator<<(ostream& out,const Date& d)
//{
//	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//}
//ostream& operator<<(ostream& out, const Date& d);
//内联函数和静态成员一样 调用处展开 不进符号表
inline ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}
//cin >> d1 编译器转化成operator(cin,d1) 形参中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}

Date.cpp

#include"Date.h"
//d1 == d2
bool Date::operator==(const Date& d) const
{ 	return _year == d._year&& _month == d._month&& _day == d._day;
}
//d1 > d2
bool Date::operator>(const Date& d) const
{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;}return false;
}
//d1 >= d2
bool Date::operator>=(const Date& d) const
{return *this > d || *this == d;
}
//d1 <= d2
bool Date::operator<=(const Date& d) const
{return !(*this > d);
}
//d1 < d2
bool Date::operator<(const Date& d) const
{return !(*this >= d);
}
//d1 != d2
bool Date::operator!=(const Date& d) const
{return !(*this == d);
}
//d1 += 100
//天满了进月 月满了进年 
Date& Date::operator+=(int day)
{//避免 d1 += -1000的情形if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){++_year;_month = 1;}}return *this;
}
//d1 + 100
Date Date::operator+(int day) const
{Date ret(*this);ret += day;//ret.operator+=(day)return ret;
}
//d1 -= 100
Date& Date::operator-=(int day)
{//避免 d1 -= -1000if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}
//d1 - 100
Date Date::operator-(int day) const
{Date ret(*this);ret -= day;return ret;
}
//前置++
Date& Date::operator++()
{//会调用 operator+=(int day)*this += 1;return *this;
}
//后置++ —多一个int参数主要是为了和前置++进行区分 构成函数重载
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) const
{Date max = *this;Date min = d;int flag = 1;if (*this < d)//总结:凡是内部不改变成员变量 也就是不改变*this数据的 这些成员函数都应该加const//if (d > *this){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++n;//复用++ ++到和d1日期相等 就是相差多少天++min;}return n * flag;
}
//为了支持链式流插入 cout<< d1 <<d2 返回cout类对象
//ostream& operator<<(ostream& out,const Date& d)
//{
//	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//	return out;
//}

最后和大家分享一段最近在emo中始终鼓舞自己的一段话,希望大家能在艰苦漫长的学习岁月中,不忘初心找寻真正的自己,把所有的美好装进独属于自己的记忆里,调整心态,继续砥砺前行!

     无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事情,而不是让烦恼和焦虑毁掉你本就不多的热情和定力。心可以碎,手却不能停,该干什么干什么,在崩溃中继续努力前行。

                                                                                                                                        ——余华

出自 "includeevey" 中的一段话

彼此分享给大家的目的一样,希望所有正在学技术被技术所困的友友们,在艰难的时候看到这句话能披荆斩棘,勇往直前!

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3ngxw7pcde80c