当前位置: 首页 > 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.hkea.cn/news/14481774/

相关文章:

  • 潮安区建设局网站建立网站的条件
  • 外贸网站假设做百度手机网站点
  • 什么网站可以做期货新闻发布会的工作环节包括
  • 比较好的网站搭建论坛编程网课
  • 二手房网站建设方案哪个网站的地图可以做分析图
  • 网站建设公司织梦模板下载郑州网站优化外包
  • 学做美食的网站layui框架的wordpress
  • 网站建设的后期服务要包括什么软件淮南网络推广报价
  • 售卖网站建设实验报告211工程建设网站
  • 个人设计网站模板seo服务商排名
  • 不相关的网站做单项链接可以吗黑龙江省建设厅官方网站
  • 镇江网站制作教程南京网站建设 雷仁网络
  • 手机怎么注册自己的网站常用的网络营销工具有哪些?
  • 海东企业网站建设广告平台推广渠道
  • 做企业网站用什么cms上海公司注销流程及费用
  • 电子商务网站建设课程心得微博指数查询
  • 赣州市建设局网站重庆佳宇建设集团网站
  • googl浏览器做桌面版网站网站架构和网络
  • 上海教育网站官网公司官网怎么搭建
  • 手机网站模板开发工具wordpress 免费完整中文主题下载
  • 游戏下载网站模板网站建设开发ppt模板
  • 吉林平台网站建设推荐个人网站建设论文
  • 头像网站模板做网站所用的技术
  • 温州网上推广什么网站好山东建设报网站
  • 鄞州区网站建设报价绍兴金圣建设有限公司网站
  • 临平房产做网站的公司江西省地图
  • 成都建设网站标化最新表格阳泉网站建设公司
  • 哪做网站好爱爱做网站
  • 用织梦系统做的2个网站要把它都上传到服务器上吗网站诊断方法
  • 建设银行手机登陆网站网站建设插件代码大全