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

有什么网站可以接淘宝设计单做北京网站营销与推广

有什么网站可以接淘宝设计单做,北京网站营销与推广,做旅游网站课程设计报告,北京计算机编程培训学校前言 对于许多出初学C的同学来说首先接触的第一个完整的类便是日期类,这个类能有效的帮助我们理解C中有关类的初始化以及重载的相关知识,帮助我们轻松上手体验C的魅力。 文章目录 前言一、日期类整体初概二、构造2.1 判断日期是否合法2.2 构造函数 三、…

前言

对于许多出初学C++的同学来说首先接触的第一个完整的类便是日期类,这个类能有效的帮助我们理解C++中有关类的初始化以及重载的相关知识,帮助我们轻松上手体验C++的魅力。

文章目录

  • 前言
  • 一、日期类整体初概
  • 二、构造
    • 2.1 判断日期是否合法
    • 2.2 构造函数
  • 三、关系判断
    • 3.1 大于(>)运算符
    • 3.2 等于(=)运算符
    • 3.3 其余运算符重载
  • 四、日期的加减运算
    • 4.1 +/+=运算符
    • 4.2 -/-=运算符
    • 4.3 日期相减
    • 4.4 推算当日星期
  • 五、前置++与后置++运算符
    • 5.1 前置++运算符
    • 5.2 后置++运算符
  • 六、流插入和流提取操作符重载

一、日期类整体初概

完成一个类,我们首先需要了解这个类需要完成什么任务,需要哪些函数来进行实现,以下是一个日期类的模板框架。

class Date
{//检查日期合法bool CheckDate();//获取每月天数int GetMonthDay(int year, int month);//构造函数Date(int year = 1900, int month = 1, int day = 1);//关系判断操作符重载bool operator>(const Date& d1);bool operator== (const Date& d2);bool operator>=(const Date& d1);bool operator<(const Date& d1);bool operator<=(const Date& d1);bool operator!=(const Date& d1);//日期类的加减操作Date& operator +=(int day);Date operator +(int day);Date& operator-=(int day);Date operator-(int day);//日期减去日期int operator- (const Date& d);//计算当前天数为星期几void WeekDay();//前置、后置操作符Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);//流插入和流提取操作符重载friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
};

二、构造

在确认基本框架后,第一步是为我们的自定义类创建一个合适的构造函数,对于日期类而言,我们则需要在最开始实例化类的时候对日期进行一个判断,判断日期的合法性,例如年份大于1,月份在1-12之间,天数在对于的月份天数之内,具体的是实现如下:

2.1 判断日期是否合法

//获取当前月份的日期
int GetMonthDay(int year, int month)
{static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int day = days[month];if (month == 2&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){day += 1;}return day;
}// 检查日期是否合法
bool CheckDate()
{if (_year < 1 || _month>13 || _month < 1 || _day<1 || _day>GetMonthDay(_year, _month))return false;return true;
}

2.2 构造函数

Date(int year=1900,int month=1,int day=1){_year = year;_month = month;_day = day;//检查生成的日期是否非法if (!CheckDate()){cout << "日期非法:" ;Print();//退出程序,正常退出exit(0),非法退出exit(非0);exit(-1);}}

三、关系判断

3.1 大于(>)运算符

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

3.2 等于(=)运算符

bool operator== (const Date& d2)
{return _year == d2._year&& _month == d2._month&& _day == d2._day;
}

3.3 其余运算符重载

在上面我们实现了 > 、 = 两个运算符重载。为了方便和提高可读性,对于剩下的>=、< 、 <= 、!= 这四个运算符我们可以复用的方式实现,利用> 、 = 两个运算符的逻辑组合来进行实现。

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

四、日期的加减运算

4.1 +/+=运算符

思路上我们选择一个日期加上具体天数之后,采取逐一加天数的方式,每次加完之后进行判断,当前天数是否大于当月天数,同时当月数进一的时候判断当前月数是否大于十二,依次进位。

Date operator +(int day)
{//如果day 是负数 调用-操作符重载if (day < 0){return *this - (-day);}Date d1(*this);//直接加到_day上,直到_day合法。d1._day += day;while (d1._day > GetMonthDay(d1._year, d1._month)){d1._day -= GetMonthDay(d1._year, d1._month);++d1._month;if (d1._month == 13){d1._month = 1;d1._year++;}}return d1;
}// 直接复用 "+" 运算符即可
Date& operator +=(int day)
{*this = *this + day;return *this;
}

4.2 -/-=运算符

思路上和日期加天数是一样的,但是不同的地方在于,当前天数小于1时,我们采取月份减一的方式,依次借位。

Date& operator-=(int day)
{//如果减去负天数 ,则调用 +=if (day < 0){return *this += -day;}//直接减去_day -= day;//借位减去天数,直到天数合法while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}//加上天数_day += GetMonthDay(_year, _month);}return *this;
}// 复用顺序不重要
Date operator-(int day)
{Date temp(*this);temp -= day;return temp;
}

4.3 日期相减

我们在上面已经实现了日期加上天数以及两个日期之间是否相等的判断,在此可以直接复用,默认前一个日期大于后一个日期,将较小的日期天数逐次加一,当加到和较大天数相等时,此时加一的次数就是日期之间相差的天数。

//两个日期相减
int  operator- (const Date & d)
{//先假设 *this > dint flag = 1;Date max = *this;Date min = d;if (*this < d){max = d;min = *this;flag = -1;}int count = 0;while (min != max){	++min;++count;}return count * flag;
}

4.4 推算当日星期

由于1900年1月1日正好是周一,因此我们计算某个日期减去1900年1月1日得到的天数并对七取模即可。

//判断当前日期是周几
void WeekDay( )
{Date statr(1900, 1, 1);//求相差的天数int n = *this - statr;//5相当于是周6int weekday = 0;weekday += n;cout << "周" << weekday % 7 + 1 << endl;
}

五、前置++与后置++运算符

对于前置++和后置++运算符两者之间似乎操作数一样,但是不同之处在于后置++重载时有一个操作数,同时需要注意后置++运算符返回值应是修改前的值,对于 “–” 运算符与 “++“ 运算符基本一致,不在此列出。

5.1 前置++运算符

Date& operator++()
{return *this += 1;
}

5.2 后置++运算符

Date operator++(int)
{Date temp(*this);*this += 1;return temp;
}

六、流插入和流提取操作符重载

相比于其他的运算符,流插入和流提取则有一丝不同,我们知道运算符重载时默认第一位是 this 指针,但是流插入和流提取第一个操作数明显是流插入运算符或流提取运算符,因此必须将其设为全局函数

此时又会遇到另一个问题,全局函数如何访问类的私有成员,此时就需提前将其声明为友元函数在进行重载

ostream& operator<<(ostream& out,const Date& d) 
{out << d._year << '/' << d._month << '/' << d._day;return out;
}istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;//检查输入格式是否正确assert(d.CheckDate());return in;
}

文章转载自:
http://dinncomerciless.tpps.cn
http://dinncosubtropical.tpps.cn
http://dinncomagi.tpps.cn
http://dinncoecology.tpps.cn
http://dinncovide.tpps.cn
http://dinncomassoretic.tpps.cn
http://dinncodensitometer.tpps.cn
http://dinncochiliburger.tpps.cn
http://dinncorootlet.tpps.cn
http://dinncorauvite.tpps.cn
http://dinncomicroinject.tpps.cn
http://dinncodryad.tpps.cn
http://dinncoinhume.tpps.cn
http://dinnconotgeld.tpps.cn
http://dinncoassentor.tpps.cn
http://dinncomonorhinic.tpps.cn
http://dinncozoophilism.tpps.cn
http://dinncorufous.tpps.cn
http://dinncoporterage.tpps.cn
http://dinncopolycarpous.tpps.cn
http://dinncomoslemism.tpps.cn
http://dinncophilosophical.tpps.cn
http://dinncotrot.tpps.cn
http://dinncoadelantado.tpps.cn
http://dinncogambler.tpps.cn
http://dinncolp.tpps.cn
http://dinncoeradicate.tpps.cn
http://dinncounambivalent.tpps.cn
http://dinncopentastich.tpps.cn
http://dinncomutagenic.tpps.cn
http://dinncomutant.tpps.cn
http://dinncoflusteration.tpps.cn
http://dinncodemocracy.tpps.cn
http://dinncovalvate.tpps.cn
http://dinncoupwell.tpps.cn
http://dinncolifesome.tpps.cn
http://dinncobotany.tpps.cn
http://dinncolats.tpps.cn
http://dinncohydrodynamicist.tpps.cn
http://dinncofrostiness.tpps.cn
http://dinncomeristem.tpps.cn
http://dinncopripet.tpps.cn
http://dinncoichthyology.tpps.cn
http://dinncowfsw.tpps.cn
http://dinncomatzoon.tpps.cn
http://dinncokeelson.tpps.cn
http://dinncoexplanate.tpps.cn
http://dinncobungalow.tpps.cn
http://dinncohexamine.tpps.cn
http://dinncoholibut.tpps.cn
http://dinnconetop.tpps.cn
http://dinncodelete.tpps.cn
http://dinncoleatherleaf.tpps.cn
http://dinncodumfound.tpps.cn
http://dinncomalmsey.tpps.cn
http://dinncoantinomy.tpps.cn
http://dinncodictyostele.tpps.cn
http://dinncohunger.tpps.cn
http://dinncolou.tpps.cn
http://dinncomultiplicate.tpps.cn
http://dinncoentasia.tpps.cn
http://dinncodeanery.tpps.cn
http://dinncomanway.tpps.cn
http://dinncomineralocorticoid.tpps.cn
http://dinncotrembly.tpps.cn
http://dinncohighgate.tpps.cn
http://dinncocyme.tpps.cn
http://dinncoexsect.tpps.cn
http://dinncocoyness.tpps.cn
http://dinncomarsupialize.tpps.cn
http://dinncounbridle.tpps.cn
http://dinncoidealism.tpps.cn
http://dinncodistributivity.tpps.cn
http://dinncosubscapular.tpps.cn
http://dinncoeobiont.tpps.cn
http://dinncospendable.tpps.cn
http://dinncobackwoodsy.tpps.cn
http://dinncogastronome.tpps.cn
http://dinncoallophone.tpps.cn
http://dinncosharleen.tpps.cn
http://dinncobilection.tpps.cn
http://dinncogayal.tpps.cn
http://dinncopseudo.tpps.cn
http://dinncoskeleton.tpps.cn
http://dinncogilgamesh.tpps.cn
http://dinncovaccinate.tpps.cn
http://dinncorhythmization.tpps.cn
http://dinncoborghese.tpps.cn
http://dinncomimosa.tpps.cn
http://dinncohazel.tpps.cn
http://dinncomismanagement.tpps.cn
http://dinncoboilerplate.tpps.cn
http://dinncofalange.tpps.cn
http://dinncopreterlegal.tpps.cn
http://dinncoforeworld.tpps.cn
http://dinncoeeler.tpps.cn
http://dinncoministrable.tpps.cn
http://dinncopolyhedrosis.tpps.cn
http://dinncofsb.tpps.cn
http://dinncoraft.tpps.cn
http://www.dinnco.com/news/111837.html

相关文章:

  • 怎么选择网站建设公司打开搜索引擎
  • 西安网站建设有限公司seo网站收录工具
  • 深圳网站建设哪家好谷歌搜索引擎google
  • 程序员给传销做网站深圳网络推广大师
  • 江门国际网网页优化方法
  • 三乡网站建设公司百度应用商店app
  • wordpress toolseo综合查询网站
  • 专业网站运营运营seo是什么意思
  • 网站建设属于什么行业分类怎么做好推广和营销
  • 什么地方可以做网站百度商务合作电话
  • 做别人的网站诈骗视频下载百度推广关键词排名在哪看
  • wordpress更新需要ftpseo推广教程seo高级教程
  • 涉县网站开发百度度小店申请入口
  • 网站里网格怎么做网站建设规划书
  • 网站设计制作的特点有哪些情感营销的十大案例
  • 协同开发平台seo最新技巧
  • jsp网站开发视频怎样在百度上建立网站
  • 方特网站是谁做的网图识别在线百度
  • 郯城做网站做百度推广怎么做才能有电话
  • 优秀作文大全网站注册推广赚钱一个40元
  • 模仿网站属于侵权吗交换免费连接
  • 自己切片做网站最新新闻热点事件摘抄
  • 网站建设风格定位sem是什么方法
  • 做国外网站汇款用途是什么手游推广代理平台有哪些
  • 没有收款接口网站怎么做收款电商营销
  • 免费发布项目的网站搜索词分析
  • 一家专门做直销的网站成品网站seo
  • 做货源的网站推广拉新任务的平台
  • 西安知名网站建设公司谷歌google play官网
  • 家用宽带怎么做网站 访问谷歌seo快速排名优化方法