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

做背景图 网站网站 默认首页

做背景图 网站,网站 默认首页,陕西网站备案 多久,学校网站内容目录 1.日期类的成员 2.日期类的成员函数 2.1构造和析构函数 2.2检查日期合法 2.3日期的打印 2.4操作符重载 2.4.1小于号 2.4.2等于号 2.4.3小于等于号 2.4.4大于号 2.4.5大于等于号 2.4.6不等号 2.4.7加等的实现 2.4.8加的实现 2.4.9减去一个天数的减等实现 2.4.10…目录 1.日期类的成员 2.日期类的成员函数 2.1构造和析构函数 2.2检查日期合法 2.3日期的打印 2.4操作符重载 2.4.1小于号 2.4.2等于号 2.4.3小于等于号 2.4.4大于号 2.4.5大于等于号 2.4.6不等号 2.4.7加等的实现 2.4.8加的实现 2.4.9减去一个天数的减等实现 2.4.10减去一个天数的减实现 2.4.11两个日期相减的实现 2.4.12前后置的实现 2.4.13前后置--的实现 2.5流插入/流提取操作符 1.日期类的成员 实现一个日期类内部的成员自然是年、月、日 class Date {private:int _year;int _month;int _day; }; 在日期类中我们应当是已知每个月份有多少天的因此我们还需要在日期内中写一个成员函数来获得当月的天数。 //获得天数int GetMonthDay(int year,int month){static int monthDayArray[13] { -1, 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];}} 此外我们的日期类还应当能够实现对日期的打印、对日期类的相关计算、输入输出的重载等成员函数。 因此我们完整的日期类应是如下 #pragma once #include iostream using namespace std; #include assert.h class Date { public://流插入or输出friend ostream operator(ostream out, const Date d);friend istream operator(istream in, Date d);//构造Date(int year 1900, int month 1, int day 1);//获取月份天数int GetMonthDay(int year, int month){assert(month 0 month 13);static int monthDayArray[13] { -1, 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];}}//检查日期bool CheckDate();//打印日期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; }; //输入流重载 ostream operator(ostream out, const Date d); //输出流重载 istream operator(istream in, Date d);2.日期类的成员函数 2.1构造和析构函数 由于日期类的成员都是内置类型因此我们可以显式的写一个构造函数但不用显式定义析构函数。 Date::Date(int year 2024, int month 7, int day 2) {_year year;_month month;_day day;//检查日期合法性if (!CheckDate()){cout 日期非法 endl;} } 由于我们的日期具有范围因此我们需要在构造函数中检查日期是否合法。也正是因此我们需要实现一个检查日期合法性的函数。 2.2检查日期合法 检查日期合法首先要确保我们输入的数是正数 //检查日期合法性 bool Date::CheckDate() {if (_month 1 || _month12|| _day1 || _dayGetMonthDay(_year, _month)||_year 1){return false;}else{return true;} } 2.3日期的打印 void Date::Print() const {cout _year - _month -_day; } 2.4操作符重载 下面我们就需要重载一些对日期类计算有用的操作符 2.4.1小于号 由于我们不想要我们传入的参数会被修改因此我们需要传递常量。权限可以缩小 另外我们传引用有两个原因 可以少一次构造形参的过程可以提高性能。d在函数运行结束后不会销毁不会返回一个空引用。 因此我们的函数名为 bool operator(const Date d) const判断一个日期是否小于另一个日期我们需要分别判断年、月、日是否小于另一个日期。 在判断日的时候我们可以直接使用原生的操作符判断。 bool Date::operator(const Date d) const {//年if (_year d._year){return true;}else if (_year d._year){ //月if (_month d._month){return true;}else if (_month d._month){//日return _day d._day;//if (_day d._day)//{// return true;//}//else//{// return false;//}}} } 2.4.2等于号 直接判断年月日是否相等即可  bool Date::operator(const Date d) const {return _year d.year _month d._month _day d._day; } 2.4.3小于等于号 我们传入的第一个参数是this因此我们解引用this即可得到第一个参数的值。  bool Date::operator(const Date d)const {return *this d || *this d; } 2.4.4大于号 判断是否大于就是是否小于等于取反  bool Date::operator(const Date d)const {return !(*this d); }2.4.5大于等于号 判断是否大于等于就是对判断是否小于取反 bool Date::operator(const Date d)const {return !(*this d); } 2.4.6不等号 判断是否不相等就是对判等取反 bool Date::operator!(const Date d)const {return !(*this d); } 2.4.7加等的实现 对于加等是给定一个日期和一个天数计算日期加上这个天数之后的日期。 这里我们采取的思路是先将原天数加上需要加的天数。 之后我们一直减去当月的天数并让月份加1如果月份为13则年份加1月份赋为1。一直到天数没有当月天数大为止。 Date Date::operator(int day) {//日期加_day day;//月份加while (_day GetMonthDay(_year, _month)){if (_day GetMonthDay(_year, _month)){_month;_day - GetMonthDay(_year, _month);if (_month 13){_month 1;_year 1;}}}return *this; } 2.4.8加的实现 首先我们实现两数相加是不能改变我们的原数的。 因此我们的第一个形参为const修饰的变量。 //加 //cab; Date Date::operator(int day) const {Date tmp *this;tmp day;return tmp; }这里我们采取的思路是创建一个临时变量来保存*this然后返回tmp加等day的结果即可。 这里需要注意的是由于这个函数运行结束之后tmp会先被销毁掉再进行返回因此我们如果返回值为引用的话则会出错。 2.4.9减去一个天数的减等实现 //减去一个天数 //减等 Date Date::operator-(int day) {//减去一个负数if (day 0){return *this (-day);}//减去一个整数_day - day;while (_day 0){--month;if (_month 0){_month 12;_year--;}_day GetMonthDay(_year, _month);} } 与实现加等类似的是这里我们也是类似的步骤通过一个循环来不断的更新年月日。  2.4.10减去一个天数的减实现 //减去一个天数 Date Date::operator-(int day)const {Date tmp *this;tmp - day;return tmp; } 这里和上面的加等相似。  2.4.11两个日期相减的实现 首先我们要判断出哪个日期大 之后我们让小的日期不断加1直到他们相同。 加了多少次1两个日期就相隔多少天。  //两个日期相减 Date Date::operator-(const Date d)const {//假设法判断谁大Date max *this;Date min d;if (min max){max d;min *this;}//小的日期不断加一天直到二者相等//设置一个计数器计数器的值就是两个日期的差值int n 0;while (min! max){min;n;}return n; } 2.4.12前后置的实现 由于我们重载操作符时都是这么写的 Date::operator()//前 Date::operator(int)//后 这样便无法判断到底是调用前置还是后置了。 因此我们规定调用后置时形参写一个int。 Date::operator()//前 Date::operator(int)//后 前置非常容易实现这里不再赘述。  //前后置-- Date Date::operator()//前 {*this 1;return *this; } Date Date::operator(int)//后 {Date tmp *this;*this 1;return tmp; } 后置是先使用后的。因此我们创建一个临时变量来保存*this并在返回tmp前对*this1。 2.4.13前后置--的实现 Date Date::operator--()//前 {*this - 1;return *this; } Date Date::operator--(int)//后 {Date tmp *this;*this - 1;return tmp; } 2.5流插入/流提取操作符 观察下面两行代码我们发现这两个操作符的第二个操作数才是this。 但是成员函数默认第一个操作数为this这就产生了问题。 因此我们不能够将这两个函数声明为成员函数。 cout n endl; cin n ; 我们需要将这两个函数声明在类外之后通过友元在类内访问即可。 //类内 friend ostream operator(ostream out, const Date d); friend istream operator(istream in, Date d); //定义 ostream operator (ostream out, const Date d) {cout d._year - d._month - d._day;return out; } istream operator(istream in, Date d) {in d._year d._month d._day;if (!d.CheckDate()){cout 日期非法 endl;}return in; }
http://www.w-s-a.com/news/255062/

相关文章:

  • 什邡建设局网站sem推广是什么意思
  • 西安分类信息网站网站敏感关键词
  • 黑彩网站怎么做建设网站费用分析
  • 网站关键词选取的步骤和方法小程序商城哪家好排行榜
  • 儿童产品网站建设网站建设优化排名推广
  • 做网站的硬件无锡招标网官方网站
  • 做推送好用的网站合肥网站推广培训
  • 网站开发团队简介贵阳双龙区建设局网站
  • 新乡做网站公司哪家好wordpress侧边栏文件
  • 小白建站怎么撤销网站备案
  • 哪个网站做调查问卷赚钱短视频制作神器
  • 上海企业响应式网站建设推荐汕头网络优化排名
  • 怎么建立公司网站平台怎么将网站做成公司官网
  • 培训学校网站怎样快速建设网站模板
  • 建设电子商务网站论文云服务器安装wordpress
  • 做展板好的网站学校的网站开发过程
  • 宁波搭建网站价格西部数码网站正在建设中是什么意思
  • 吉林省建设项目招标网站苏州网络推广定制
  • 网站域名所有权证明引流推广接单
  • 做网站百度百科孟州网站建设
  • 服务网站建设企业广州模板建站系统
  • 怎么做属于自己的免费网站浏览器游戏网址
  • 上海城乡住房建设厅网站西安网站推广慧创科技
  • 做策划网站推广怎么写简历互联网公司手机网站
  • 怎么做宣传网站网站建设采购项目合同书
  • 网站的空间和域名备案做网站要会写什么
  • wap 网站源码企业网站被转做非法用途
  • 下载网站模板怎么使用做物流网站的公司
  • 网站 商城 app 建设建设银行江苏省行网站
  • 广州网站开发建设西安广告公司联系方式