淄博网站排名优化公司,旅游前 做攻略有什么网站好用,一级a做爰片啪网站,搭建外文网站【C手撕系列】——设计日期类实现日期计算器#x1f60e; 前言#x1f64c;C嘎嘎类中六大护法实现代码#xff1a;获取每一个月天数的函数源码分享构造函数源码分享拷贝构造函数源码分享析构函数源码分享赋值运算符重载函数源码分享取地址和const取地址运算符重载函数源码分… 【C手撕系列】——设计日期类实现日期计算器 前言C嘎嘎类中六大护法实现代码获取每一个月天数的函数源码分享构造函数源码分享拷贝构造函数源码分享析构函数源码分享赋值运算符重载函数源码分享取地址和const取地址运算符重载函数源码分享各种比较 , , , , !运算符重载函数源码分享各种运算的 运算符重载函数源码分享流插入cout和流提取cin运算符重载函数源码分享日期 - 日期 函数源码分享 Date 日期类头文件源码Date 日期类功能文件源码Date 日期类测试文件源码测试截图证明TestDate1函数的测试结果TestDate2函数的测试结果TestDate3函数的测试结果TestDate4函数的测试结果 小结语 总结撒花 博客昵称博客小梦 最喜欢的座右铭全神贯注的上吧 作者简介一名热爱C/C算法等技术、喜爱运动、热爱K歌、敢于追梦的小博主 博主小留言哈喽各位CSDN的uu们我是你的博客好友小梦希望我的文章可以给您带来一定的帮助话不多说文章推上欢迎大家在评论区唠嗑指正觉得好的话别忘了一键三连哦 前言 哈喽各位友友们我今天又学到了很多有趣的知识现在迫不及待的想和大家分享一下 都是精华内容可不要错过哟 最近学习了C的类和对象这部分的内容然后自己手痒痒就在空闲的时候手撕了一个日期类按照网页版日期计算器有的功能进行一个一一的实现。纯粹分享如果大家对日期计算器感兴趣的话也可以去实现一个自己的日期计算器也可以借鉴我写的代码有不懂的也可以来问我哦~废话不多说咋们开始啦
C嘎嘎类中六大护法实现代码
在C嘎嘎中有六大护法一直守护我们的类。分别是
构造函数析构函数拷贝构造函数赋值运算符重载函数取地址重载函数const取地址重载函数
获取每一个月天数的函数源码分享
这里要注意的就是闰年和平年的2月天数的确定。闰年2月是29平年是28天。
//获取每一个月的天数
int Date::GetMonthDay(int year, int month) const
{int DateArray[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;}return DateArray[month];
}构造函数源码分享
这里需要注意的就是输入非法日期的情况所以这里要给个检查当输入非法日期就报错
//构造函数
Date:: Date(int year, int month , int day):_year(year),_month(month),_day(day)
{if (month 1 || month 12 || day 1|| day GetMonthDay(_year, _month)){cout 输入日期非法 endl;}
}拷贝构造函数源码分享
这里可以不用写拷贝构造编译器可以默认生成。
//拷贝构造函数
Date:: Date(const Date d):_year(d._year), _month(d._month), _day(d._day)
{}析构函数源码分享
这里可以不用写析构编译器可以默认生成。
//析构函数
Date:: ~Date()
{}赋值运算符重载函数源码分享
这里要避免 d1 d1(自己给自己赋值情况所以加一个检查。其实也不用自己显示实现编译器默认生成的赋值重载函数就可以了满足需求了~
//赋值运算符重载
Date Date::operator(const Date d)
{if (this ! d){_year d._year;_month d._month;_day d._day;}return *this;
}
取地址和const取地址运算符重载函数源码分享
一般这两个函数不用写编译器默认生成的就可以了。这里知识闲着无聊先出来看看而已。
//取地址和const取地址运算符重载
Date* Date:: operator()
{return this;
}const Date* Date:: operator()const
{return this;
}各种比较 , , , , !运算符重载函数源码分享 //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;}else{return false;}
}//d1 d2
bool Date::operator(const Date d) const
{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);
}
各种运算的 运算符重载函数源码分享
这里写好了 然后复用 实现 同理写好了 - 我们就可以复用 - 来实现 - 。前置和后置区别是后置参数列表加一个int进行占位用于区分前置和后置同理前置- -与后置- -也是这样规定实现的。
// d1 d2
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 13){_year;_month 1;}}return *this;
}Date Date::operator(int day) const
{Date tem(*this);tem day;return tem;
}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 tem(*this);tem - day;return tem;
}Date Date::operator()
{*this 1;return *this;
}Date Date::operator(int)
{Date tem(*this);(*this);return tem;
}
Date Date::operator--()
{(*this) - 1;return *this;
}
Date Date::operator--(int)
{Date tem(*this);(*this) - 1;return tem;
}
流插入cout和流提取cin运算符重载函数源码分享 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;in d._month;in d._day;return in;
}日期 - 日期 函数源码分享
这个函数实现比较难想出来。这里先是确定出两个日期的大小关系然后让小的日期不断n记录加的次数也就是两个日期相隔的天数当两个日期相等时n*flag 就是结果。这里的flag是一个关键当一开始是小日期 - 大日期时flag 就是-1计算出的答案自然是负数当是大日期减小日期flag就是1答案自然就是正数。
int Date::operator-(const Date d) const
{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 n * flag;
}Date 日期类头文件源码
#pragma once#includeiostream
using namespace std;class Date
{// 友元声明friend ostream operator(ostream out, const Date d);friend istream operator(istream in, Date d);public:int GetMonthDay(int year, int month) const;Date(int year 1, int month 1, int day 1);//void Print() const;Date(const Date d);~Date();// 只读函数可以加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) const;Date operator-(int day);Date operator-(int day) const;// 函数重载// 运算符重载// d1 - d1.operator()Date operator();// d1 - d1.operator(0)// 加一个int参数进行占位跟前置构成函数重载进行区分// 本质后置调用编译器进行特殊处理Date operator(int);Date operator--();Date operator--(int);Date operator(const Date d);int operator-(const Date d) const;// 日常自动生成的就可以// 不想被取到有效地址Date* operator();const Date* operator() const;
private:int _year;int _month;int _day;
};ostream operator(ostream out, const Date d);
istream operator(istream in, Date d);
Date 日期类功能文件源码
#define _CRT_SECURE_NO_WARNINGS 1
#includeDate.h//获取每一个月的天数
int Date::GetMonthDay(int year, int month) const
{int DateArray[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;}return DateArray[month];
}//构造函数
Date:: Date(int year, int month , int day):_year(year),_month(month),_day(day)
{if (month 1 || month 12 || day 1|| day GetMonthDay(_year, _month)){cout 输入日期非法 endl;}
}//拷贝构造函数
Date:: Date(const Date d):_year(d._year), _month(d._month), _day(d._day)
{}//析构函数
Date:: ~Date()
{}//赋值运算符重载
//避免 d1 d1(自己给自己赋值情况
Date Date::operator(const Date d)
{if (this ! d){_year d._year;_month d._month;_day d._day;}return *this;
}//取地址和const取地址运算符重载
Date* Date:: operator()
{return this;
}const Date* Date:: operator()const
{return this;
}//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;}else{return false;}
}//d1 d2
bool Date::operator(const Date d) const
{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 d2
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 13){_year;_month 1;}}return *this;
}Date Date::operator(int day) const
{Date tem(*this);tem day;return tem;
}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 tem(*this);tem - day;return tem;
}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;in d._month;in d._day;return in;
}Date Date::operator()
{*this 1;return *this;
}Date Date::operator(int)
{Date tem(*this);(*this);return tem;
}
Date Date::operator--()
{(*this) - 1;return *this;
}
Date Date::operator--(int)
{Date tem(*this);(*this) - 1;return tem;
}int Date::operator-(const Date d) const
{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 n * flag;
}Date 日期类测试文件源码
#define _CRT_SECURE_NO_WARNINGS
#includeDate.h//6大默认成员函数测试
void TestDate1()
{//构造测试Date d1;//拷贝构造测试Date d2(2023, 8, 11);//流插入运算符重载测试cout d1;cout d2;//流提取运算符重载测试cin d1;cout d1;//取地址运算符重载测试cout d1 endl;cout d2;}//测试日期大小比较以及计算日期往后或者往前几天的日期是多少
void TestDate2()
{Date d1(2003, 8, 23);Date d2(2023, 8, 11);/*cout (d1 d2) endl;cout (d1 d2) endl;cout (d1 d2) endl;cout (d1 d2) endl;cout (d1 d2) endl;cout (d1 ! d2) endl;*///cout d1;//d1 10000;//cout d1;//d2 d1 10000;//cout d1;//cout d2;//d1 -1000;//cout d1;//d1 d2 -1000;//cout d1;/*d1 - 11000;d2 d1 - 1000;cout d1;cout d2;*/}void TestDate3()
{Date d1(2003, 8, 23);Date d2(2023, 8, 11);/*cout d1;d1;cout d1;*//*cout d1;d2 d1;cout d1;cout d2;*//*cout d1;d2 d1--;cout d1;cout d2;*//*cout d1;d2 d1;cout d1;cout d2;*//*cout d1;d2 --d1;cout d1;cout d2;*/
}void TestDate4()
{Date d1(2003, 8, 23);Date d2(2023, 8, 11);cout (d2 - d1) endl;cout (d1 - d2) endl;
}int main()
{//TestDate1();//TestDate2();//TestDate3();TestDate4();return 0;
}
测试截图证明
TestDate1函数的测试结果 TestDate2函数的测试结果
TestDate3函数的测试结果 TestDate4函数的测试结果 与网页版日期计算器测试结果一致
小结语
上述测试案例我只写了比较常规的并没有做会更加仔细的测试。大家也可以帮我用链接: 网页版日期计算器测试一下我的程序有没有bug我好及时更正
总结撒花 本篇文章旨在分享的是用C 设计日期类实现日期计算器的知识。希望大家通过阅读此文有所收获 如果我写的有什么不好之处请在文章下方给出你宝贵的意见。如果觉得我写的好的话请点个赞赞和关注哦~