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

vs做网站出现显示bug咸阳网络推广

vs做网站出现显示bug,咸阳网络推广,企业网站做优化,开发安卓app用什么语言上一篇文章只实现了operator操作符重载&#xff0c;由于运算符较多&#xff0c;该篇文章单独实现剩余所有的运算符重载。继续以Date类为例&#xff0c;实现运算符重载&#xff1a; 1.Date.h #pragma once#include <iostream> #include <assert.h>using namespace …

在这里插入图片描述

上一篇文章只实现了operator==操作符重载,由于运算符较多,该篇文章单独实现剩余所有的运算符重载。继续以Date类为例,实现运算符重载:
1.Date.h

#pragma once#include <iostream>
#include <assert.h>using namespace std;class Date
{
private:int _year;int _month;int _day;
public:void Print();Date(int yaer, int month, int day);bool operator<(const Date& d);bool operator<=(const Date& d);bool operator>(const Date& d);bool operator>=(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);//单独的用一个函数把每个月多少天,封装起来int GetMonthDays(int year, int month){assert(month > 0 && month < 13);static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month==2&&(year % 4 == 0 && year % 100 != 0)){return 29;}return MonthDay[month];}Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);//++d,前置++Date& operator++();//d++,后置++Date operator++(int);//前置--Date& operator--();//后置--Date operator--(int);//两个日期相减:d1-d2int operator-(const Date& d);
};

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1 
#include <stdio.h>#include "Date.h"void Date::Print()
{cout << _year << "/" << _month << "/" << _day << endl;
}Date::Date(int year=1, int month=1, int day=1)
{_year = year;_month = month;_day = day;
}
//   写好一个直接复用!!!
bool Date::operator<(const Date& d)
{if (_year < d._year){return true;}else if (_year == d._year){if (_month < d._month)return true;else if ((_month == d._month) && (_day < d._day))return true;elsereturn false;}elsereturn false;
}bool Date::operator==(const Date& d)
{if ((_year == d._year) && (_month == d._month) && (_day == d._day))return true;elsereturn false;
}bool Date::operator<=(const Date& d)
{return *this == d || *this < d;
}bool Date::operator>(const Date& d)
{return !(*this <= d);
}bool Date::operator>=(const Date& d)
{return (*this > d || *this == d);
}bool Date::operator!=(const Date& d)
{return !(*this == d);
}Date& Date::operator+=(int day)
{_day += day;//先加//这里是while,因为如果是if的话,如果一次加了很大的数据减一次不一定能减得完!!!while(_day > GetMonthDays(_year, _month)){_day -= GetMonthDays(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}return *this;
}Date Date::operator+(int day)
{Date tmp=*this;tmp += day;return tmp;
}Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDays(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}Date& Date::operator++()
{return *this += 1;
}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;
}
//日期-日期,计算两个日期之间相差多少天int Date::operator-(const Date& d)
{int flag = 1;Date max = *this;Date min = d;if (*this < d){//赋值为-1的原因:因为这个函数是有顺序的d1-d2,如果遇到d1<d2,也就是小减大,最终返回的结果是个负数,所以说这里要变成-1。flag = -1;max = d;min = *this;}//定义一个变量int n = 0;// 用循环算两个日期中间的差值while (min != max){++min;++n;}return n * flag;
}

3.Test.cpp

#define _CRT_SECURE_NO_WARNINGS 1 
#include <stdio.h>
#include "Date.h"int main()
{Date d1(2024, 2, 15);Date d2 = d1 + 20;d1.Print();d2.Print();bool ret=d1 > d2;if (ret){d1.Print();}d2 += 100;d2.Print();d2 -= 100;d2.Print();Date d3 = d2 - 10;d3.Print();Date d4(2024, 1, 29);Date d5(2024, 8, 1);cout << d5 - d4 << endl;++d5;d5.Print();d5++;d5.Print();--d5;d5.Print();d5--;d5.Print();return 0;
}
http://www.zhongyajixie.com/news/46363.html

相关文章:

  • 网站方案设计软文营销是什么
  • 重庆今天最新新闻seo实战密码
  • 电子商务论文宁波网站推广优化公司电话
  • 超低价网站维护网站托管网站标题算关键词优化吗
  • 加强政协机关网站建设磁力蜘蛛
  • 小程序开发查询seo推广是什么意怿
  • 菏泽市建设银行网站seo1视频发布会
  • 北仑seo排名优化技术谷歌seo优化排名
  • 做店标 做店招的网站百度品牌专区
  • 如何建立公司网站链接开发一个平台需要多少钱
  • 昆山快速建设网站方案广安seo外包
  • 专业的做网站的百度搜索排行榜
  • 怎么做m开头的网站网页设计制作网站图片
  • 东营市招投标信息网上海百网优seo优化公司
  • wordpress做社交网站吗优帮云排名自动扣费
  • 网站流量怎么做今日国际新闻最新消息
  • 网站建设公司华网天下买赠两年建设公司最佳磁力链ciliba
  • dw做的网站能直接使用吗2023疫情第三波爆发时间
  • 清溪仿做网站怎样申请网站注册
  • 网站源码怎么获取南昌网站优化公司
  • 建立网站分为几阶段百度竞价点击工具
  • 图书类网站建设策划书网店营销策略有哪些
  • 网站推广的意义和方法seo接单平台有哪些
  • 做代购 需要独立网站合肥网站排名推广
  • 网站开发器手机360优化大师官网
  • 上海做兼职网站有吗网络维护
  • php网站建设原码百度一下首页极简版
  • 个人主页网站模板免费百度打广告多少钱
  • 米枫网站怎么做分页百度收录平台
  • 可以做围棋题的网站全国人大常委会委员长