網站首頁 編程語言 正文
我們今天實現一個簡單的計算日期
我們這里先給出頭文件,后面一個一個解釋
頭文件
#pragma once
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
//定義構造函數
Date(int year = 1900, int month = 1, int day = 1);
//打印日期
void Print()
{
cout << _year <<" " <<_month<<" " << _day << endl;
}
//運算符重載
Date operator+(int day);
Date operator-(int day);
Date& operator+=(int day);
Date& operator-=(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);
Date& operator++();//前置
Date operator++(int);//后置
Date& operator--();//前置
Date operator--(int);//后置
int operator-(Date d);//計算兩個日期的差值
private:
int _year;
int _month;
int _day;
};
詳細步驟
第一步
計算閏年和判斷日期是否合法
inline int GetMonthDay(int year, int month)
{
//多次調用的時候static能極大減小內存消耗,但是也可以刪除,并不影響程序運行
static int _monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int day = _monthArray[month];
if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)//判斷是否是閏年
{
day = 29;
}
return day;
}
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!(year >= 0 && month >= 1 && month <= 12 && day > 0 && day <= GetMonthDay(year, month)))//看日期是否合法
{
cout << "fail" << endl;
exit(-1);
}
}
我們將year,month,day賦值之后,首先進行判斷,如果出現異常天數直接終結程序(如2022,1,32,這種不存在的日期)
隨后一個問題就出來了,因為閏年和非閏年的2月不同,所以我們又使用一個函數getmonthday,來判斷閏年的同時獲取當月天數;
有些同學可能直接是【0,31,59(31+28 1月+2月),89(1月+2月+3月)......+.....365】
我們稍后再解釋為什么要用我這種定義方式。
第二步
各類運算符重載
我們首先來看運算符”+”和”+=”
這是一般情況下的+
Date Date::operator+(int day)
{
Date tmp(*this);
tmp._day += day;
//判斷天數是否大于當月的天數
while (tmp._day > GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= GetMonthDay(tmp._year, tmp._month);
tmp._month++;
//判斷月數是否大于12
if (tmp._month > 12)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
這是一般情況下的+=
Date& Date::operator+=(int day)
{
Date ret(*this);
ret._day += day;
//判斷天數是否超越了當月的天數
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
//判斷月數是否超過了12
if (_month > 12)
{
_year++;
_month = 1;
}
}
}
return *this;
}
我們可以發現兩者其實都用了同一個while循環來判斷日期是否合法。
我們可以選擇將while循環打包成一個新的函數,也可以選擇直接復用
+復用+=
Date Date::operator+(int day)
{
Date tmp(*this);
tmp += day;//直接調用operator的+=
return tmp;
}
總代碼
#define _CRT_SECURE_NO_WARNINGS 1
#include"date.h"
inline int GetMonthDay(int year, int month)
{
static int _monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int day = _monthArray[month];
if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
day = 29;
}
return day;
}
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!(year >= 0 && month >= 1 && month <= 12 && day > 0 && day <= GetMonthDay(year, month)))
{
cout << "!legal" << endl;
}
}
Date& Date::operator+=(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-=(int day)
{
if (day < 0)
{
*this += -day;
}
else {
_day -= day;
while (_day <= 0)
{
if (_month == 1)
{
--_year;
_month = 12;
_day += GetMonthDay(_year, _month);
}
else
{
--_month;
_day += GetMonthDay(_year, _month);
}
}
}
return *this;
}
Date Date::operator+(int day)
{
Date tmp(*this);
tmp += day;
return tmp;
}
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--()//前置
{
return *this -= 1;
}
Date Date::operator--(int)//后置
{
Date tmp(*this);
*this -= 1;
return tmp;
}
bool Date::operator>(const Date& d)
{
if (_year > d._year)
return true;
else if (_year < d._year)
return false;
else//_year < d._year
{
if (_month > d._month)
return true;
else if (_month < d._month)
return false;
else//_month < d._month
{
if (_day > d._day)
return true;
else
return false;
}
}
}
bool Date::operator<(const Date& d)
{
return !(*this > d || *this == d);
}
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)
{
if (_year == d._year)
{
if (_month == d._month)
{
if (_day == d._day)
return true;
}
}
return false;
}
bool Date::operator!=(const Date& d)
{
//復用==
return !(*this == d);
}
int Date::operator-(Date d)
{
int count = 0;
Date tmp(*this);
if (tmp < d)
{
while (tmp != d)
{
++count;
tmp += 1;
}
}
else if (tmp > d)
{
while (tmp != d)
{
--count;
tmp -= 1;
}
}
return count;
}
原文鏈接:https://blog.csdn.net/qq_62718027/article/details/125086459
相關推薦
- 2022-02-03 ionic4 ngFor中使用ngIf
- 2022-09-25 FFmpeg源碼分析:SwsContext圖像轉換上下文
- 2022-11-23 Android10?Binder原理概述深入解析_Android
- 2022-05-05 Nginx配置ssl實現https的全過程記錄_nginx
- 2022-05-20 SpringBoot整合Mybatis演示
- 2022-08-28 ERROR 1366 (HY000): Incorrect string value: ‘\xE8\
- 2022-10-04 混合棧跳轉導致Flutter頁面事件卡死問題解決_IOS
- 2022-09-03 Golang棧結構和后綴表達式實現計算器示例_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支