当前位置:网站首页>虫子 日期类 上 太子语言
虫子 日期类 上 太子语言
2022-06-23 03:44:00 【华为云】
日期类
我们要有一个习惯就是写代码或者是写项目的时候我们要==写一点编一点==,要养成良好的习惯,万万不可以写完了再编,然后出了一堆错误自己都不想看,我们先自己编个最小项目或者系统框架在加其他功能
Date.h
Date.h就是一些头文件,声明啥的
#pragma once#include <iostream>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);private: int _year; int _month; int _day;};
Date.cpp
Date.cpp这里面就是写日期类的函数了
#include "Date.h"Date::Date(int year, int month, int day){ _year = year; _month = month; _day = day;}void Date::Print(){ cout << _year << "年" << _month << "月" << _day << "日" << endl;}Date Date::operator+(const int& day){ return *this;}Date Date::operator-(const int& day){ return *this;}
test.cpp
test.cpp
#include "Date.h"int main(){ Date d(2022,1,1); d.Print(); return 0;}
测试情况
功能添加
当我们能基本丐版跑出来了那我们接下来的路还是要走的,我们要考虑到数据是否合理啊什么的
==所以我们需要检查日期的合法性==
==上面的确是排除了百分之99的滤掉了,但是闰年2月是特殊考虑的反而没有代码实现==
==我们可不可以优化一下,比如我就输错了一个日期你就直接程序死了是不是有点太霸道了,你应该抛出异常,或隔一下啥的==
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){ //我们先不管,我们先直接把天加上去 _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;}
日期++,++日期
==日期加加和加加日期都是++,但是operator运算符重载怎么区分呢,==
前置++
==前置++返回运算以后的值==
//++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){ //啥也不多说,先把天减掉 _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;}
日期–,--日期
==日期减减和减减日期都是–,但是operator运算符重载怎么区分呢,==
前置–
==前置–返回运算以后的值==
//前置减减Date& Date::operator--(){ *this -= 1; return *this;}
后置–
==后置–返回运算前的值==
//后置减减Date& Date::operator--(int){ Date tmp(*this); *this -= 1; return tmp;}
边栏推荐
- What is the potential of dmail based on Web3.0? First round financing of $10 million?
- 仿360桌面悬浮球插件
- Firewall and IP security policy configuration
- Apicloud native module, H5 module and multi terminal component tutorial
- 背景彩带动画插件ribbon.js
- What is the difference between the poll () method and the remove () method?
- Centos7 installing MySQL and configuring InnoDB_ ruby
- 直接插入排序
- redisTemplate和cacheManager操作redis有什么不同
- Talk about memory model and memory order
猜你喜欢

如何处理大体积 XLSX/CSV/TXT 文件?

Google Earth Engine(GEE)——长时间序列逐月VCI数据提取分析和面积计算(墨西哥为例)

MySQL common instructions

怎么使用Shell脚本实现监测文件变化

1058 multiple choice questions (20 points)

嵌入式软件测试工具TPT18更新全解析

The new version of Kali switches the highest account

【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现

Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph

bubble sort
随机推荐
The new version of Kali switches the highest account
An implementation of warning bombing
RTOS system selection for charging point software design
粒子动画背景登录页面particles.js
mysql,字段问题
Centos7 installing MySQL and configuring InnoDB_ ruby
Apicloud native module, H5 module and multi terminal component tutorial
如何处理大体积 XLSX/CSV/TXT 文件?
[OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe
Select sort method
How to realize data transaction
SwiftUI 组件大全之使用 ScrollView 和 GeometryReader 创建动画 3D卡片 滚动效果
折半查找法
Twitter与Shopify合作 将商家产品引入Twitter购物当中
MySQL data recovery (.Ibdata1, bin log)
怎么用好MySQL索引
MySQL optimization, the SQL execution is very stuck, and the SQL structure will not be changed until it ends in 10 seconds
How to solve the problem that the web page fails to log in after the easycvr service is started?
This point (II)
[binary tree] 993 Cousins in Binary Tree


















