实现日期间的运算——C++

news/2025/2/16 0:59:13/

在这里插入图片描述
请添加图片描述

😶‍🌫️Take your time ! 😶‍🌫️
💥个人主页:🔥🔥🔥大魔王🔥🔥🔥
💥代码仓库:🔥🔥魔王修炼之路🔥🔥
💥所属专栏:🔥魔王的修炼之路–C++🔥
如果你觉得这篇文章对你有帮助,请在文章结尾处留下你的点赞👍和关注💖,支持一下博主。同时记得收藏✨这篇文章,方便以后重新阅读。

  • 学习完C++入门以及类和对象,我们已经迫不及待的想写一个自己的小项目了,下面这个计算日期的小项目就是运用类和对象写出来的。

Date.h

#pragma once#include <iostream>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>using namespace std;class Date
{
public://获取某年某月的天数
int GetMonthDay(int year, int month);//全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);//拷贝构造函数
//d2(d1)
Date(const Date& d);//赋值运算符重载
//d2 = d3 -> d2.operator = (&d2,d3)
Date& operator= (const Date& d);//析构函数
~Date();// >运算符重载
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 += (int day);//日期+天数
Date operator+(int day);//日期-天数
Date operator-(int day);//日期-=天数
Date& operator-=(int day);//前置++
Date& operator++();//后置++
Date operator++(int);//后置--
Date operator--(int);//前置--
Date& operator--();//日期-日期 返回天数
int operator-(const Date& d);//打印
void PrintDate();private:int _year;int _month;int _day;
};

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1#include "Date.h"//获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{static int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)){return 29;}elsereturn arr[month];
}//全缺省的构造函数
Date::Date(int year, int month, int day)
{this->_year = year;this->_month = month;_day = day;
}//拷贝构造函数
//de(d1)
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}//赋值运算符重载
Date& Date::operator=(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;return *this;
}//析构函数
Date:: ~Date()
{_year = 0;_month = 0;_day = 0;
}// >
bool Date::operator>(const Date& d)
{if (_year > d._year)return true;if (_year == d._year && _month > d._month)return true;if (_year == d._year && _month == d._month && _day > d._day)return true;return false;
}// ==
bool Date::operator==(const Date& d)
{if (_year == d._year && _month == d._month && _day == d._day)return true;return false;
}// >=
bool Date::operator>=(const Date& d)
{if (*this > d || *this == d)return true;return false;
}// <
bool Date::operator<(const Date& d)
{if (*this > d || *this == d)return false;return true;
}// <=
bool Date::operator<=(const Date& d)
{if (*this < d || *this == d)return true;return false;}// !=
bool Date::operator!=(const Date& d)
{if (*this == d)return false;return true;
}//下面这两组一共调用两次拷贝构造
// 日期+=天数
Date& Date::operator+=(int day)
{this->_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)
{//Date temp = *this;//与下面的等效,都是用一个已经存在的对象初始化正在创建的对象,//编译器会调用拷贝构造函数,而不是赋值运算符重载,不要被表象迷惑。Date temp(*this);temp += day;return temp;
}//下面这两组一共调用四次拷贝构造
// 日期-天数
Date Date::operator-(int day)
{Date temp(*this);while (temp._day<day){temp._month--;if (temp._month == 0){temp._year--;temp._month = 12;}temp._day += GetMonthDay(_year, _month);}temp._day -= day;return temp;
}//日期-=天数
Date& Date::operator-=(int day)
{*this = *this - day;return *this;
}//前置++
Date& Date::operator++()
{*this += 1;return *this;
}//后置++
Date Date::operator++(int)
{Date temp(*this);*this += 1;return temp;
}//后置--
Date Date::operator--(int)
{Date temp(*this);*this -= 1;return temp;
}//前置--
Date& Date::operator--()
{*this -= 1;return *this;
}//日期 - 日期 -> 返回天数
int Date::operator-(const Date& d)
{int year_day = 0;int month_day = 0;int day_day = 0;if (_year != d._year){int a = abs(_year - d._year);while (a--){if (_year > d._year){if (GetMonthDay(d._year + a, 2) == 29)year_day += 366;elseyear_day += 365;}else{if (GetMonthDay(_year + a, 2) == 29)year_day += 366;elseyear_day += 365;}}}if (_year < d._year)year_day = -year_day;if (1){int a = _month - 1;int _month_day = 0;int d_month_day = 0;while (a){a--;_month_day += GetMonthDay(_year, a);}a = d._month - 1;while (a){a--;d_month_day += GetMonthDay(d._year, a);}month_day = _month_day - d_month_day;}if (_day != d._day);{day_day += _day - d._day;}return year_day + month_day + day_day;
}//打印
void Date::PrintDate()
{cout << _year << ' ' << _month << ' ' << _day << endl;
}

test.cpp

#define _CRT_SECURE_NO_WARNINGS 1#include "Date.h"int main()
{//Date d1(2020, 1, 1);//Date d2(d1);//d2 += 2;//Date d4(++d1);//d1.PrintDate();//d2.PrintDate();//d4.PrintDate();//cout << d1 - d2 << endl;//Date d3(2018, 8, 23);//cout << d1- d3 << endl;//cout << (d1 < d2) << endl;Date d1(2004, 4, 6);Date d2(2023, 10, 13);cout << d2 - d1 << endl;return 0;
}
  • 以上就是我们通过学习类和对象编写的第一个cpp小项目,可以用来比较日期大小、相减得出两个日期间相差多少天、一个日期加上一个天数所得的另一个日期等等。


  • 博主长期更新,博主的目标是不断提升阅读体验和内容质量,如果你喜欢博主的文章,请点个赞或者关注博主支持一波,我会更加努力的为你呈现精彩的内容。

🌈专栏推荐
😈魔王的修炼之路–C语言
😈魔王的修炼之路–数据结构初阶
😈魔王的修炼之路–C++
😈魔王的修炼之路–Linux
更新不易,希望得到友友的三连支持一波。收藏这篇文章,意味着你将永久拥有它,无论何时何地,都可以立即找到重新阅读;关注博主,意味着无论何时何地,博主将永久和你一起学习进步,为你带来有价值的内容。

请添加图片描述


http://www.ppmy.cn/news/1168502.html

相关文章

SpringBoot + MyBatis 在 jar 中可以启动但是 Idea中无法启动的原因

现象 在idea中启动始终卡住&#xff0c;查看线程堆栈发现一直在mybatis的处理过程中&#xff0c;细究了一下堆栈发现mybatis有使用远程方式加载类的情况&#xff0c;并且此时cpu会飙升&#xff0c; 在命令行中使用java -jar 的形式可以正常启动&#xff0c;但是在idea中启动始…

SAP-QM-动态检验规则

Dynamic Modification Rule &#xff08;动态修改规则&#xff09; 1、决定样本大小的方式有3种&#xff1a; 手动输入比例大小采样过程 物料主数据质量视图 2、采样过程的创建方式有2种 跟批量大小有关系&#xff1a;百分比/AQL跟批量大小没有关系&#xff1a;固定值 而当…

Power BI 傻瓜入门 4. Power BI:亮点

本章内容包含&#xff1a; 在Power BI Desktop上学习诀窍摄入数据使用模型试用Power BI服务 就像评估一个由多种成分组成的蛋糕一样&#xff0c;Power BI要求其用户熟悉商业智能&#xff08;BI&#xff09;解决方案中的功能。几乎所有与Power BI交互的用户都是从桌面版开始的…

操作系统——进程互斥的软件实现算法(王道视频p27、课本ch6)

1.总结概览&#xff1a; 2.单标志[turn]法——算法代码&#xff1a; 可能违反“空闲让进” 3.双标志[flag[2]]先检查法——算法代码&#xff1a; 如果不能利用硬件的原语的话&#xff0c;就可能出现违反“忙则等待”的问题: 4.双标志[flag[2]]后检查法——算法代码&#xff1…

leetcode_2316 统计无向图中无法互相到达点对数

1. 题意 给定一个无向图&#xff0c; 统计无法互相到达的点对数。 统计无法互相到达点对数 2. 题解 其实还是求联通块&#xff0c;求联通块可以使用搜索进行标记。还要求得联通块中元素的大小。 联通块其实也就是不相交集合&#xff0c;也可以用并查集来做。 每求得一个联…

01-初识HTML和CSS

1.HTML与CSS 1.1.什么是HTML&#xff1f;什么是CSS&#xff1f; HTML是HyperText Markup Language(超文本标记语言) ​ 它不是一种编程语言&#xff0c;而是一种标记语言&#xff0c;用于告诉浏览器如何构造你的页面。它可以由一系列HTML元素组合成web开发人员想要的简单或者…

Python语言:元组的使用

元组是存放一个有序的不可改变内容的的容器。 元组的特点&#xff1a; 他不能修改元素。元组的元素由小括号括起来&#xff0c;元素之间用逗号隔开。元组可以保存许多相同内容的元素。元组元素里可以嵌套元组也可以嵌套其他类型的容器。 元组的定义与创建 # 创建一个元组&am…

N-128基于springboot,vue酒店管理系统

开发工具&#xff1a;IDEA 服务器&#xff1a;Tomcat9.0&#xff0c; jdk1.8 项目构建&#xff1a;maven 数据库&#xff1a;mysql5.7 系统分前后台&#xff0c;项目采用前后端分离 前端技术&#xff1a;vueelementUI 服务端技术&#xff1a;springbootmybatis 本系统功…