当前位置:网站首页>虫子 日期类 下 太子语言
虫子 日期类 下 太子语言
2022-06-23 03:44:00 【华为云】
修一修bug
若是-=里面的日期是负数的话
==我们有时候是需要考虑日期里面是有负数的==
==所以需要我们改一下代码,需要+=的代码,直接挪用==
==复用即可==
//日期减天数同时赋回去Date& Date::operator-=(const int& day){ //每月天数超过他本身了也是违法的 //复用+=即可 if (day < 0) { *this += -day; } else { //啥也不多说,先把天减掉 _day -= day; //不合法就等他合法 while (_day <= 0) { //先把月减了 _month--; //先判断月是不是零,是的话就操作年了 if (_month <= 0) { _year--; _month = 12; } //然后让他加上正确的月的天数 _day += GetMonthDay(_year, _month); } } return *this;}
若是+=里面的日期是负数的话
==同样的+=里面有负数也是一样的处理==
==所以我们也把-=的代码拎过来==
==同理复用即可==
//日期加天数同时赋回去Date& Date::operator+=(const int& day){ //负数复用-=的 if (day < 0) { *this -= -day; } else { //我们先不管,我们先直接把天加上去 _day += day; //然后再判断合不合法 while (_day > GetMonthDay(_year, _month)) { //先把当月的天数减掉 _day -= GetMonthDay(_year, _month); //然后月++ _month++; //假如月也过了就年++ if (_month > 12) { _year++; _month = 1; } } } return *this;}
比较运算符
大于 >
==d1 > d2==
//比较//大于 d1 > d2转换成d1.operator>(&d1,d2)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) { if (_day > d._day) return true; } } //不是上面情况就false return false;}
等于 ==
==d1 == d2==
//等于bool Date::operator==(const Date& d){ return _year == d._year && _month == d._month && _day == d._day;}
大于等于 >=
==d1 >= d2==
//大于等于bool Date::operator>=(const Date& d){ return *this > d || *this == d;}
小于<
==d1 < d2==
//小于bool Date::operator<(const Date& d){ return !(*this >= d);}
小于等于 <=
==d1 <= d2==
//小于等于bool Date::operator<=(const Date& d){ return !(*this > d);}
不等于 !=
==d1 != d2==
//不等于bool Date::operator!=(const Date& d){ return !(*this == d);}
生活实际
我们思考一个问题就是日期减日期有没有意义
日期减日期是不是就是相差多少天,那你这个怎么操作呢
//日期减日期//通过计数的方式来 要是年减年 月减月就是无法找到实际多少天了int Date::operator-(const Date& d){ //首先假设前面的大 Date max = *this; //所以后面的就小 Date min = d; //符号标记 int flag = 1; //如果错了就交换 if (max < min) { max = d; min = *this; flag = -1; } //当min与max相同时就是停止计数的时候 int count = 0; while (min != max) { min++; count++; } //出来就把计数返回出去顺带着标记 return count * flag;}
代码
Date.h
Date.h#pragma once#include <iostream>#include <assert.h>using std::cout;using std::cin;using std::endl;class Date{public: Date(int year = 0, int month = 1, int day = 1); void Print(); //像析构,拷贝构造,赋值重载可以不需要写,因为默认生成的就够用了, //像Stack才需要自己写这三个 //日期加 减天数 然后同时赋值赋过去 Date& operator+=(const int& day); Date& operator-=(const int& day); //日期加 减天数 不需要赋值 Date operator+(const int& day); Date operator-(const int& day); //日期前置后置加加 Date& operator++(); Date& operator++(int); //日期前置后置减减 Date& operator--(); Date& operator--(int); //比较 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 operator-(const Date& d);private: int _year; int _month; int _day;};
Date.cpp
Date.cpp#include "Date.h"inline int GetMonthDay(int year, int month){ //数组存放平年每个月的天数 刚好对应的下标是月 里面的元素是天 static int dayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; //该月天数 int day = dayArray[month]; //闰年是4年一润百年不润或者四百年一润 if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { //闰年的2月是29天 day = 29; } return day;}//构造函数Date::Date(int year, int month, int day){ //检查日期的合法性 if (year > 0 && month > 0 && month <13 && day > 0 && day <= GetMonthDay(year,month)) { _year = year; _month = month; _day = day; } else { cout << endl; cout << "非法日期" << endl; cout << year << "年" << month << "月" << day << "日" << endl; }}//日期打印void Date::Print(){ cout << _year << "年" << _month << "月" << _day << "日" << endl;}//日期加天数同时赋回去Date& Date::operator+=(const int& day){ //负数复用-=的 if (day < 0) { *this -= -day; } else { //我们先不管,我们先直接把天加上去 _day += day; //然后再判断合不合法 while (_day > GetMonthDay(_year, _month)) { //先把当月的天数减掉 _day -= GetMonthDay(_year, _month); //然后月++ _month++; //假如月也过了就年++ if (_month > 12) { _year++; _month = 1; } } } return *this;}//日期加天数 不赋回去Date Date::operator+(const int& day){ //首先创建一个临时对象 先把之前的拷贝复制给他 Date ret(*this); //复用+= ret += day; return ret;}//++d 日期前置++ 被转换成d.operator++(&d)Date& Date::operator++(){ //返回运算后的值 *this += 1; return *this;}//d++ 日期后置++ 被转换成d.operator++(&d,0)//这里的int仅仅是占位,不需要给实参,起到函数重载的作用Date& Date::operator++(int){ //后置++返回运算前的值 //所以需要一个临时对象先存起来 Date tmp(*this); *this += 1; return tmp;}//日期减天数同时赋回去Date& Date::operator-=(const int& day){ //每月天数超过他本身了也是违法的 //复用+=即可 if (day < 0) { *this += -day; } else { //啥也不多说,先把天减掉 _day -= day; //不合法就等他合法 while (_day <= 0) { //先把月减了 _month--; //先判断月是不是零,是的话就操作年了 if (_month <= 0) { _year--; _month = 12; } //然后让他加上正确的月的天数 _day += GetMonthDay(_year, _month); } } return *this;}//日期减天数 不赋回去Date Date::operator-(const int& day){ Date ret(*this); //-=的复用 ret -= day; return ret;}//前置减减Date& Date::operator--(){ *this -= 1; return *this;}//后置减减Date& Date::operator--(int){ Date tmp(*this); *this -= 1; return tmp;}//比较//大于 d1 > d2转换成d1.operator>(&d1,d2)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) { if (_day > d._day) return true; } } //不是上面情况就false return false;}//等于bool Date::operator==(const Date& d){ return _year == d._year && _month == d._month && _day == d._day;}//大于等于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);}//不等于bool Date::operator!=(const Date& d){ return !(*this == d);}//日期减日期//通过计数的方式来 要是年减年 月减月就是无法找到实际多少天了int Date::operator-(const Date& d){ //首先假设前面的大 Date max = *this; //所以后面的就小 Date min = d; //符号标记 int flag = 1; //如果错了就交换 if (max < min) { max = d; min = *this; flag = -1; } //当min与max相同时就是停止计数的时候 int count = 0; while (min != max) { min++; count++; } //出来就把计数返回出去顺带着标记 return count * flag;}
test.cpp
test.cpp#include "Date.h"void test1(){ //前置++和后置++都完成++,不同的地方在于是返回值不同 Date d(2022, 1, 1); d ++; //后置++返回++以前的值 d.Print(); ++d; //前置++返回++以后的值 d.Print();}void test2(){ //前置--和后置--都完成--,不同的地方在于是返回值不同 Date d(2022, 1, 1); d--; //后置--返回--以前的值 d.Print(); --d; //前置--返回--以后的值 d.Print();}void test3(){ Date d1(2022, 1, 2); Date d2(2022, 1, 2); cout << (d1 != d2) << endl; d2 -= 1; cout << (d1 != d2) << endl;}void test4(){ Date d1(2024, 1, 2); Date d2(2022, 5, 7); cout << d1 - d2 << endl; cout << d2 - d1 << endl;}int main(){ test1(); test2(); test3(); test4(); Date d(2022,1,1); d += -40; d.Print(); Date d2(2022, 1, 1); d2 += 4000; d2.Print(); Date d3(2022, 1, 1); d3 = d3 + 3000; d3.Print(); Date d4(2022, 1, 1); d4 -= -3000; d4.Print(); Date d5(2022, 1, 1); d5 = d5-2000; d5.Print(); return 0;}
边栏推荐
- 数据加密技术之源代码加密
- This point (II)
- mysql如何删除表的一行数据
- How e-commerce makes use of small programs
- 软件项目管理 8.4.软件项目质量计划
- Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph
- How to process large volume xlsx/csv/txt files?
- [advanced Android] entrusted by kotlin
- The compatibility of remotefx schemes is related to multiple factors
- 【owt】owt-client-native-p2p-e2e-test vs2017构建 3 : 无 测试单元对比, 手动生成vs项目
猜你喜欢

软件项目管理 8.4.软件项目质量计划
![[Zeng shuge's laser slam notes] gmapping filter based slam](/img/93/b940ad95508d1c0d23642022df37f2.png)
[Zeng shuge's laser slam notes] gmapping filter based slam
![[OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe](/img/cd/7f896a0f05523a07b5dd04a8737879.png)
[OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe

直接插入排序

Using jhipster to build microservice architecture

线上MySQL的自增id用尽怎么办?

MySQL data recovery (.Ibdata1, bin log)

centos7 安装 MySQL 及配置 innodb_ruby

R tree of search tree

如何处理大体积 XLSX/CSV/TXT 文件?
随机推荐
Insérer le tri directement
What if the self incrementing IDs of online MySQL are exhausted?
redisTemplate和cacheManager操作redis有什么不同
[OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe
如何处理大体积 XLSX/CSV/TXT 文件?
[OWT] OWT client native P2P E2E test vs2017 build 3: no test unit comparison, manually generate vs projects
直接插入排序
What is the APM tool skywalking
linux下的开源数据库是什么
Talk about memory model and memory order
怎么使用Shell脚本实现监测文件变化
AI 视频云 VS 窄带高清,谁是视频时代的宠儿
Firewall and IP security policy configuration
Tcapulusdb Jun · industry news collection (IV)
1058 multiple choice questions (20 points)
SwiftUI 组件大全之使用 ScrollView 和 GeometryReader 创建动画 3D卡片 滚动效果
Questions about SQL statements
Swiftui component encyclopedia creating animated 3D card scrolling effects using Scrollview and geometryreader
背景彩带动画插件ribbon.js
页面导出excel的三种方式













