当前位置: 首页 > wzjs >正文

容桂营销网站建设网站建设官方网站

容桂营销网站建设,网站建设官方网站,公司做营销型网站,怎么做淘宝客网站目录 前言: 1.显示日期 2.构造函数与获取某年某月的日期的函数 3.日期比较 4.日期加减天数 5.日期减日期 6.前置后置与-- 7.完整代码 8.测试 总结: 感谢支持! 前言: 结合了前面的内容的学习,本篇来对之前的…

目录

前言:

1.显示日期

2.构造函数与获取某年某月的日期的函数

3.日期比较

4.日期加减天数

5.日期减日期

6.前置后置++与--

7.完整代码

8.测试

总结:

感谢支持!


前言:

结合了前面的内容的学习,本篇来对之前的内容像运算符重载等进行应用,完成日期类的实现,具体功能大致包括对日期的加减比较,显示日期等。

1.显示日期

调用成员函数打印日期:

void Date::Print() const
{cout << _year << "/" << _month << "/" << _day << endl;
}

对流插入<<进行重载,完成对日期类的打印:

 定义文件中:

ostream& operator<<(ostream& out, const Date& d) 
{out << d._year << "/" << d._month << "/" << d._day << endl;return out;
}

声明文件中:

	friend ostream& operator<<(ostream& out, const Date& d);

 为什么这里要用友元函数:

由于我们要使用的格式为例如,cout<<d1,如果放到成员函数中,由于对象调用成员函数会传对象的地址并在成员函数中用this指针接收,所以第一个参数只能是Date* this,也只能d1<<cout这样调用。

但是我们如果仅仅定义为一个普通函数就没法访问到类中的私有成员变量了,而且不能为了一个函数就让成员函数放为私有的,所以我们这里使用友元函数,既可以访问成员变量,也可以调整参数的顺序。

2.构造函数与获取某年某月的日期的函数

声明文件中:

	Date(int year = 1900, int month = 1, int day = 1);int GetMonthDay(int year, int month) const;

注意构造函数的缺省要在声明中给(如果声明和定义的缺省值不同,编译器无法确定,放在声明中也减少了其他源文件重复定义)。

定义文件中:

int Date::GetMonthDay(int year, int month) const
{assert(month > 0 && month < 13);int MonthArray[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 MonthArray[month];}
}Date::Date(int year, int month, int day)
{if (year > 0 && (month > 0 && month < 13) && (day > 0 && day <= GetMonthDay(month, day))){_year = year;_month = month;_day = day;}else{cout << "初始化日期非法" << endl;}}

1.因为有闰年平年之分,还有不同的月的天数不同,所以我们要定义一个函数来获取具体到哪年哪月的具体的天数。

2.获取天数用一个数组来确定,方便下标与内容对应,多加一位;其次就是单独判断闰年的二月是29天,如果是,那就直接返回,如果不是,就返回对应下标的天数。

3.构造函数判断一下日期的合法性。 

3.日期比较

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 (_year < d._year)|| ((_year == d._year) && (_month < d._month))|| ((_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);
}
bool Date::operator>(const Date& d) const
{return !(*this <= d);
}bool Date::operator!=(const Date& d) const
{return !(*this == d);
}

可以先实现等于和小于的逻辑,其它的逻辑直接复用即可。 

4.日期加减天数

Date& Date::operator+=(int day) 
{if (day < 0){*this -= -day;//+=一个负的,就是让它走-=一个正的逻辑return *this;}_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 tmp(*this);tmp += day;//复用,变得更简单//tmp._day += day;//while (tmp._day < GetMonthDay(tmp._year, tmp._month))//{//	tmp._day -= GetMonthDay(tmp._year, tmp._month);//	++tmp._month;//	if (tmp._month == 13)//	{//		tmp._year++;//		tmp._month = 1;//	}//}return tmp;
}Date& Date::operator-=(int day)
{Date tmp(*this);if (day < 0){*this += -day;//-=一个负的天数,就是走+=一个正的天数的逻辑return *this;}_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;
}

 1.首先区分+=和-=与+和-的区别,+=和-=是会对原内容进行修改,也就是对对象进行修改,所以用引用返回,直接返回this指针,还能减少返回时产生的拷贝;而对于+和-来说,不能改变原有的对象,所以先调用编译器默认生成的拷贝构造来对对象进行一次拷贝(这里只有浅拷贝,不会出错),然后再对拷贝的对象复用+=或者-=的逻辑,再返回这个拷贝的对象即可,所以这里需要用一个返回值接收这个结果才能看出来。

2.要注意如果对天数进行操作时,天数为-怎么办。如果为负,在+=里面,就是要转换为-=了,所以写一句*this-=-day即可转换成去调用,因为day本身就是负的,所以就去调用*this-=day了;在-=则相反。

3.在+=中,我们要还要判断,当天数+的超过了这个月的天了,我们就要往下个月加了,如果加到下个月还多,那就再往下个月加,所以使用一个循环来实现,如果月加到了13,就要加到下一年去,并且让月再从一月开始,+也要考虑这个问题,但是我们复用了+=的逻辑,就不考虑了。

4.在-=中,我们要判断如果天数减的变为了负的,那就要往上一个月去借,然后让天数加上这个月的天数,如果借的还不够,继续借,所以使用循环来实现,注意如果月减到了0,就要往上一年借,并让月从12开始。-直接复用即可。

5.也可以先完成+-的逻辑,+=与-=再复用:
 

//如果先实现了+的逻辑,+=也可复用+的逻辑
//*this=*this+day;但是这个会先调operator+的函数,再调自动生成的operator赋值函数,所以建议使用第一种

这样就多了拷贝了,所以建议先实现+=与-=。

5.日期减日期

日期减日期可以有两种方法,可以先获取两个日期距离所在年的1月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;
}

flag用来确定正负,n用来计算相差多少天。先假设左边的日期大,然后比较,如果右边大,交换顺序,并且右边大减出来肯定是负的,所以再让flag设为-1,然后让n计数,看相差多少天,然后返回天数*正反号即可。

6.前置后置++与--

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;
}

1.前置复用之前+=与-=的逻辑,直接返回这个修改后的对象;而后置是先拷贝,再对原对象进+=和-=的复用,再返回没有改变的拷贝的对象,这样接收返回值的时候,接收的就是没有改变的对象,但是原来的对象已经修改了,就起到了后置的效果。

2.编译器为了区分前置后置,在调用后置的时候,会传一个参数,可能是一个整形0,可能是其他的,所以重载的时候写一个int参数,这是编译器自动传的,不用我们自己传,自己传也行。

7.完整代码

定义文件:

#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"int Date::GetMonthDay(int year, int month) const
{assert(month > 0 && month < 13);int MonthArray[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 MonthArray[month];}
}Date::Date(int year, int month, int day)
{if (year > 0 && (month > 0 && month < 13) && (day > 0 && day <= GetMonthDay(month, day))){_year = year;_month = month;_day = day;}else{cout << "初始化日期非法" << endl;}}void Date::Print() const
{cout << _year << "/" << _month << "/" << _day << endl;
}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 (_year < d._year)|| ((_year == d._year) && (_month < d._month))|| ((_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);
}
bool Date::operator>(const Date& d) const
{return !(*this <= d);
}bool Date::operator!=(const Date& d) const
{return !(*this == d);
}Date& Date::operator+=(int day) 
{if (day < 0){*this -= -day;//+=一个负的,就是让它走-=一个正的逻辑return *this;}_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 tmp(*this);tmp += day;//复用,变得更简单//tmp._day += day;//while (tmp._day < GetMonthDay(tmp._year, tmp._month))//{//	tmp._day -= GetMonthDay(tmp._year, tmp._month);//	++tmp._month;//	if (tmp._month == 13)//	{//		tmp._year++;//		tmp._month = 1;//	}//}return tmp;
}Date& Date::operator-=(int day)
{Date tmp(*this);if (day < 0){*this += -day;//-=一个负的天数,就是走+=一个正的天数的逻辑return *this;}_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;
}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& 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;
}ostream& operator<<(ostream& out, const Date& d) 
{out << d._year << "/" << d._month << "/" << d._day << endl;return out;
}

声明文件:

#pragma once
#include <iostream>
#include <assert.h>using namespace std;class Date
{friend ostream& operator<<(ostream& out, const Date& d);
public:Date(int year = 1900, int month = 1, int day = 1);int GetMonthDay(int year, int month) const;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) const; Date& operator-=(int day);Date operator-(int day) const;int operator-(const Date& d) const;Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);private:int _year;int _month;int _day;
};

这样的成员函数没对成员变量进行修改的都加上了const修饰,方便不是const的对象和const的对象都能调用。

8.测试

#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"void TestDate1()
{Date d1(2024, 4, 4);Date d2(2023, 4, 4);Date ret1=++d1;cout << d1;cout << ret1;Date ret2=d1++;cout << d1;cout << ret2;cout << endl;Date ret3=d1--;cout << d1;cout << ret3;Date ret4=--d1;cout << d1;cout << ret4;d1.Print();}void TestDate2()
{Date d1(2024, 4, 4);Date d2(2023, 4, 4);Date ret1=d1 + 100;cout << d1;cout << ret1;d1 += 100;cout << "d1此时为:" << d1;Date ret2=d2 - 100;cout << d2;cout << ret2;d2 -= 100;cout << "d2此时为:" << d2;cout << (d1 - d2) << endl;;cout << (d2 - d1) << endl;;}void TestDate3()
{Date d1(2024, 4, 4);Date d2(2023, 4, 4);cout << (d1 > d2) << endl;cout << (d1 < d2) << endl;cout << (d1 >= d2) << endl;cout << (d1 <= d2) << endl;cout << (d1 != d2) << endl;cout << (d1 == d2) << endl;}int main()
{TestDate3();return 0;
}

 

 

总结:

感谢支持!


文章转载自:

http://MYLL2mi8.kdtdh.cn
http://1PotA7Nm.kdtdh.cn
http://pGUJCe0r.kdtdh.cn
http://xPunn0ZP.kdtdh.cn
http://54alCHiW.kdtdh.cn
http://Lsja8K6y.kdtdh.cn
http://kHUHONdq.kdtdh.cn
http://8lqql1tv.kdtdh.cn
http://nohAldN7.kdtdh.cn
http://7tezSm0s.kdtdh.cn
http://bxCFvHme.kdtdh.cn
http://hh9YkFHB.kdtdh.cn
http://UGJIN8n0.kdtdh.cn
http://uaFvgJCV.kdtdh.cn
http://EtYC8qei.kdtdh.cn
http://PVR4f3ML.kdtdh.cn
http://0pfatgRE.kdtdh.cn
http://JkqJTPQK.kdtdh.cn
http://yut7Bb7s.kdtdh.cn
http://ETDGAMNl.kdtdh.cn
http://pniHtuHs.kdtdh.cn
http://4YG4OuNm.kdtdh.cn
http://WUUYQL1U.kdtdh.cn
http://MKlJ85Z4.kdtdh.cn
http://wFt56ubs.kdtdh.cn
http://xZeX0BAb.kdtdh.cn
http://LSPBWZMc.kdtdh.cn
http://bHxx6xVu.kdtdh.cn
http://o8s95dEY.kdtdh.cn
http://wt3ecVxX.kdtdh.cn
http://www.dtcms.com/wzjs/700070.html

相关文章:

  • 成都新线加网站建设windows 2008 搭建网站
  • 个人怎么做音乐网站国外设计师wordpress主题
  • 怎样建立和设计公司网站企业网站开发一薇
  • 怎么用阿帕奇做网站群晖 wordpress 外网访问
  • 芯港小镇建设管理中心网站网站推广seo软件
  • 设计网站需要考虑哪些什么是网站镜像
  • 物流网站方案关键词加入搜索引擎网站
  • 网站建设重要新广告发布与制作
  • 厦门图书馆网站建设网络营销图片素材
  • 专业建站推广服务网站logo图标
  • 使用angularjs的网站营销型网站的页面层级
  • 西昌市建设工程管理局网站网站title怎么修改
  • 宜春网站建设公司263企业邮箱官网登录
  • 城阳网站开发公司网页设计模板图片素材下载
  • 如何评价网站是否做的好处wordpress vul
  • 有没有一些帮做名片的网站最近在线观看免费大全电视剧
  • 潍城区建设局网站做外单阿里的网站
  • 重庆永川网站建设南京制作网站
  • 网站开发架构文档上海做网站的公
  • 建站平台步骤详解贵阳网站制作软件
  • 江苏建设行政主管部门网站平面设计职业学校
  • 网站建设费可以计入管理费用吗阿里云网站建设流程
  • 绿色设计网站泰安集团网站建设报价
  • 上海高端建站网站营销网站的成功案例
  • 南京网站设计公司有哪些公司四川省住房和城乡建设厅厅长
  • 男男床做第一次视频网站建设济南公司网站
  • 韩国网站如何切换中文猫咪mv最新地域网名怎么取
  • phpcms 网站访问统计wordpress中文视频插件下载地址
  • 临沂设计网站的公司重庆制作网站公司哪家好
  • 怎样删除网站wordpress ydg theme