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

农业门户网站建设目标百度seo快排软件

农业门户网站建设目标,百度seo快排软件,游戏网站免费入口,网站如何做360优化文章目录👉日期类介绍👈👉日期类实现👈📕 成员变量📕 构造函数📕 对应月份天数📕 赋值重载📕 比较运算符重载📕 计算 运算符重载👉源代码&#x1…

文章目录

  • 👉日期类介绍👈
  • 👉日期类实现👈
    • 📕 成员变量
    • 📕 构造函数
    • 📕 对应月份天数
    • 📕 赋值重载
    • 📕 比较运算符重载
    • 📕 计算 运算符重载
  • 👉源代码👈
    • Date.h
    • Date.cpp

👉日期类介绍👈

日期类是用来描述具体日期的,涉及到 年、月、日,当然,也可以有多个日期之间的关系。

class Date
{friend istream& operator>>(istream& in, Date& d);friend ostream& operator<<(ostream& out,const Date& d);public:int GetMonthDay(int year, int month);Date(int year = 2023, int month = 2, int day = 3);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=(const Date& d);Date& operator+=(int day);Date operator+(int day);Date& operator++();    // 前置++Date operator++(int);  // 后置++ ,int 参数只是和前置++ 区分Date& operator-=(int day);Date operator-(int day);int operator-(const Date& d)const;void operator<<(ostream& out);void Print()const;private:int _year;int _month;int _day;
};

👉日期类实现👈

📕 成员变量

private:int _year;int _month;int _day;

📕 构造函数

	Date::Date(int year,int month, int day){assert(month > 0 && month < 13);_year = year;_month = month;_day = day;}

📕 对应月份天数

由于申明和定义分离,申明是在 Date 这个命名空间里面的,所以要用访问限定修饰符。
每个月天数是对应的,可以用数组存储,除了闰年的二月,可以单独判断。

int Date::GetMonthDay(int year, int month)
{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];}
}

📕 赋值重载

要考虑一个情况,就是自己赋值给自己,这种情况什么都不用做,所以直接用 if 语句跳过。
当然,传参和传返回值都是引用,提高效率。

	Date& Date::operator=(const Date& d){if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;}

📕 比较运算符重载

实际只要写两个, == 和 < (当然, == 和 > 也可以),其他的可以复用这两个。

	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 !(*this == d);}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;return false;}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);}

📕 计算 运算符重载

如下, += 重载返回值是 Date& ,是为了方便 d = d1+=x; 这样的情况,d1 先加上 x天的日期,赋值给 d 。

+的重载可以直接复用 += 。因为,例如 d1 + 99 ,是返回 d1 这个日期 99天之后的具体日期,但是 d1 本身不会改变,所以,返回值绝对不可以是 d1 的引用,而要是一个另一个变量。返回该变量的时候,要创建临时变量并进行拷贝构造。
但是,如果先写+,再让+= 复用+ ,就会导致 += 也要进行拷贝构造,有小号。

另外,-= 和 - 的重载,也和上面类似的原理,- 复用 -=
而对于后置++,编译器会自动传一个int 类型的参数,我们不用去理会。
计算两个日期的天数,有了前面的重载就很容易,只需要日期小的一直++,直到和另一个日期相等即可。

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){_month = 1;_year++;}}return *this;
}Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}Date& Date::operator++()
{*this += 1;return *this;
}// 调用后置++ 的时候,编译器自动传一个 int 类型的参数
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}Date& Date::operator-=(int day)
{if (day < 0){*this += -day;return *this;}_day -= day;while (_day <= 0){_month -= 1;if (_month == 0){_year -= 1;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}int Date::operator-(const Date& d)const
{Date max = *this;Date min = d;int flag = 1;if (max < min){max = d;min = *this;flag = -1;}int ret = 0;while (max != min){++min;++ret;}return ret*flag;}

👉源代码👈

Date.h

#pragma once
#include<iostream>
#include<assert.h>
#include<stdlib.h>
using namespace std;class Date
{friend istream& operator>>(istream& in, Date& d);friend ostream& operator<<(ostream& out,const Date& d);public:int GetMonthDay(int year, int month);Date(int year = 2023, int month = 2, int day = 3);//Date(const Date& d);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=(const Date& d);Date& operator+=(int day);Date operator+(int day);Date& operator++();    // 前置++Date operator++(int);  // 后置++ ,int 参数只是和前置++ 区分Date& operator-=(int day);Date operator-(int day);int operator-(const Date& d)const;void operator<<(ostream& out);void Print()const;private:int _year;int _month;int _day;
};// 使用内联函数
inline ostream& operator<<(ostream& out, const Date& d)
{cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}inline istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}

Date.cpp

#include"Date.h"
int Date::GetMonthDay(int year, int month)
{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){assert(month > 0 && month < 13);_year = year;_month = month;_day = day;}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 !(*this == d);}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;return false;}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);}Date& Date::operator=(const Date& d){if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;}void Date::Print()const{cout << _year << "年" << _month << "月" << _day << "日" << endl;}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){_month = 1;_year++;}}return *this;
}// + 复用 += 比较好,这样可以减少拷贝构造
// 因为 + 无论如何都是要拷贝构造的,如果 += 复用+ ,那么+= 也要拷贝构造
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}Date& Date::operator++()
{*this += 1;return *this;
}// 调用后置++ 的时候,编译器自动传一个 int 类型的参数
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}Date& Date::operator-=(int day)
{if (day < 0){*this += -day;return *this;}_day -= day;while (_day <= 0){_month -= 1;if (_month == 0){_year -= 1;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}int Date::operator-(const Date& d)const
{Date max = *this;Date min = d;int flag = 1;if (max < min){max = d;min = *this;flag = -1;}int ret = 0;while (max != min){++min;++ret;}return ret*flag;}void Date::operator<<(ostream& out)
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
http://www.khdw.cn/news/52206.html

相关文章:

  • 有公网ip 如何做一网站百度市场应用官方app
  • 展示型网站php公司网站设计制作
  • 周浦做网站知识营销案例
  • 怎么制作网站首页的代码关键词挖掘机爱站网
  • 网站的流量检测怎么做常见的网络营销模式
  • 网站开发相关职业岗位宣传推广文案
  • 广告公司名字大全免费seo排名教程
  • 做电商海报的网站湖南seo推广多少钱
  • 做自己的网站怎么购买空间名片seo什么意思
  • 哪个网站买东西是正品又便宜深圳货拉拉
  • 轻量应用云服务器谷歌seo是什么意思
  • 网站建设电话销售山西seo推广
  • 网站做的不好会有什么后果免费网站seo优化
  • 做捐款网站互联网网络推广
  • 用蜗牛做logo的网站有什么功能
  • c#做的网站怎么上传图片网站推广教程
  • 网站建设可以帮助企业宁波seo优化流程
  • 网站建设问题学生个人网页制作
  • 山东做网站网络营销是什么
  • 购物网站图标淘宝店铺运营推广
  • 做网站代理拉别人赌博百度搜索引擎盘搜搜
  • 品牌建设涉及到如何优化关键词的方法
  • 网站公安机关备案企业网站分析报告
  • 上海专业网站建设排行慧聪网
  • 浙江建设职业技术学院提前招网站app香港账号
  • 淘宝网站制作教程长沙网络推广小公司
  • 政务公开与政府网站建设的关系盐城seo优化
  • 网站推广公司兴田德润官网多少网站推广计划方案
  • 免费网站建设可信吗流量宝
  • 雅加达网站建设如何在百度推广自己的产品