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

网站整体设计最近三天的国际新闻大事

网站整体设计,最近三天的国际新闻大事,3d建模网站,深圳航空人工客服电话日期类的实现 一,声明二,函数成员定义2.1构造函数2.2获取月份天数2.3比较运算符2.3.1等于和大于2.3.2其他 2.4计算运算符2.4.1 &&2.4.2-&&- 2.5日期-日期 一,声明 class Date { public:Date(int year 1, int month 1, int…

日期类的实现

  • 一,声明
  • 二,函数成员定义
    • 2.1构造函数
    • 2.2获取月份天数
    • 2.3比较运算符
      • 2.3.1等于和大于
      • 2.3.2其他
    • 2.4计算运算符
      • 2.4.1 +=&&+
      • 2.4.2-=&&-
    • 2.5日期-日期

一,声明

class Date
{
public:Date(int year = 1, int month = 1, int day = 1);//打印void Print();//获取月份天数int GetMonthDay(int year, int month);//比较运算符bool operator==(const Date& y);bool operator!=(const Date& y);bool operator>(const Date& y);bool operator<(const Date& y);bool operator>=(const Date& y);bool operator<=(const Date& y);//计算运算符int operator-(const Date& d);Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);
private:int _year;int _month;int _day;
};

二,函数成员定义

2.1构造函数

Date::Date(int year,int month,int day)
{_year = year;_month = month;_day = day;if (_year < 1 || _month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month)){Print();cout << "日期非法" << endl;}
}

这里要注意,构造函数的声明定义分离,给缺省值的时候,只在声明的地方给,不然会出错。

2.2获取月份天数

//获取月份天数
int Date::GetMonthDay(int year, int month)
{assert(year >= 1 && month >= 1 && month <= 12);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;return monthArray[month];
}

2.3比较运算符

2.3.1等于和大于

bool Date::operator==(const Date& y)
{return _year == y._year && _month == y._month&& _day == y._day;
}
bool Date::operator>(const Date& y)
{if (_year > y._year){return true;}else if (_year == y._year){if (_month > y._month){return true;}else if (_month == y._month){if (_day > y._day){return true;}}}return false;
}

写完了这两个那么其他的运算符我们都可以复用来简化代码。

2.3.2其他

bool Date::operator!=(const Date& y)
{return !(*this == y);
}
bool Date::operator>=(const Date& y)
{return (*this > y || *this == y);
}
bool Date::operator<(const Date& y)
{return !(*this>=y);
}
bool Date::operator<=(const Date& y)
{return!(*this > y);
}

2.4计算运算符

2.4.1 +=&&+

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){_month = 1;_year++;}}return *this;
}

+可以复用+=

Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}

补充:这里除了+去复用+=,可以反过来吗?
这里我们就要从效率的角度去看待这个问题。在这里插入图片描述
我们分别对比,他们的拷贝构造,可以看出用+=去复用+资源更浪费。

2.4.2-=&&-

Date& Date::operator-=(int day)
{if (day < 0){return *this += (-day);}_day -= day;while (_day<=0){_month--;if (_month < 1){_month = 12;_year--;}_day += GetMonthDay(_year, _month);}return *this;
}

一样的复用

Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return tmp;
}

2.4.3(前置++&&后置++)&&(前置–&&后置–)

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

为例区分前置和后置,我们会在后置的参数部分加一个参数类型。

2.5日期-日期

int Date::operator-(const Date& d)
{int flag = 1;Date Max = *this;Date Min = d;if (*this < d){Max = d;Min = *this;flag = -1;}int n = 0;while (Max != Min){++Min;++n;}return n * flag;
}

找出两个天数中大的那个,然后让小的天数一直++,直到相等。

http://www.hrbkazy.com/news/47751.html

相关文章:

  • 建行网站查询密码是什么东西google谷歌搜索引擎入口
  • 做诱惑类cpa网站经验广州网站优化公司排名
  • 原创 网站 源码免费正规大数据查询平台
  • 网站建设的策划方案网站网络推广公司
  • 网站建设 课程 说明一站式媒体发稿平台
  • 公众号开发视频监听播放完成台州seo优化公司
  • 福建富通建设有限公司网站如何建立网址
  • 网站怎么优化推广搜索引擎和浏览器
  • 网站文章怎么做内链网站搜索引擎优化方案
  • 高密做网站哪家强代理企业网络规划设计方案
  • 深圳网站建设黄浦网络-骗钱快速排名seo软件
  • 浦口区网站建设技术指导百度人工智能开放平台
  • 如何做微信小程序步骤贺贵江seo教程
  • 唯艾迪 wordpressseo中介平台
  • 江门有什么网站推广品牌seo推广
  • chplayer wordpress公众号seo排名软件
  • 在哪找可以做网站的搜索引擎优化的常用方法
  • 网站关键词收费百度指数怎么查询
  • 网站建设添加视频广州线上教学
  • 运城微信网站建设网络推广渠道排名
  • 免费建站免费网站申请网站seo课程
  • 在哪个彩票网站是小黄人做头像的网络推广的目标
  • 网站推广平台排行seo建设
  • 常州中小企业网站制作哈尔滨百度推广联系人
  • 福州网络营销网站昆明seo关键词
  • 做网站备案是承诺书是啥浏览器下载安装2023版本
  • 企业解决方案网站seo技术优化整站
  • 嘉兴哪里可以做淘宝网站推广管理
  • 建设工程执业注册中心网站百度快照手机版
  • 新疆网络干部学院盐城网站优化