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

用C++实现日期类

在上学的时候,总是在计算还有多少天放假;在上班的时候,总是在计算还有多久发工资?我们一般通过日历得到结果,那自己能不能实现一些基本的功能呢?答案是可以的!

需要实现内容:

1. 日期加减天数

//d1+=50
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month > 12){_year++;_month = 1;//_day -= GetMonthDay(_year, _month); // error - _month已经发生改变}}return *this;
}//d1+50(不会改变d1)
Date Date::operator+(int day)
{//创建临时对象,改变贫道不改变己身Date tmp(*this);tmp += day;return tmp;
}// ++d1
Date& Date::operator++()
{*this += 1;return *this;
}
// d1++
Date Date::operator++(int x)
{Date tmp(*this);*this += 1;return tmp;
}

// d1-=50
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){_month--;if (_month <= 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);;}return *this;
}
// d1-50
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}// --d1
Date& Date::operator--()
{*this -= 1;return *this;
}
// d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}

2. 日期减日期

int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;int count = 0;if (*this < d){max = d;min = *this;flag = -1;}int day1 = max._day;int day2 = min._day;int n = max._year - min._year;if (max._year == min._year){n = 0;}// 2025 1 31while (max._month != 1 || max._day != 1){max._month--;if (max._month <= 0){max._month = 12;max._year--;break;}max._day = GetMonthDay(max._year, max._month);day1 += max._day;}//2024 1 1while (min._month != 1 || min._day != 1){min._month--;if (min._month <= 0){min._month = 12;min._year--;break;}min._day = GetMonthDay(min._year, min._month);day2 += min._day;}int x = min._year;while (x < max._year){count += Getleapyear(min._year + 1);x++;}return day1 - day2 + 365 * n + count+1;
}

第二种就是直接暴力计算,如下:

int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int count = 0;while (min != max){min++;count++;}return count * flag;
}

3. 完整代码

Date.h 文件

#pragma once
#include <iostream>
using namespace std;
#include <assert.h>class Date
{
public:Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}int GetMonthDay(int year, int month){assert(month > 0 && month < 13);// 为了避免每次调用都开辟空间,可以加static存放到全局static int arr[13] = { -1,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];}int Getleapyear(int year){if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return 1;}elsereturn 0;}Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}void Print(){cout << _year << "年" << _month << "月" << _day << "日" << endl;}// 日期+=dayDate& operator+=(int day);Date operator+(int day)const;Date& operator-=(int day);Date operator-(int day)const;// ++d1Date& operator++();// d1++Date operator++(int);// --d1Date& operator--();// d1--Date operator--(int);int operator-(const Date& d)const;bool operator==(const Date& d)const;bool operator!=(const Date& d)const;bool operator>(const Date& d)const;bool operator>=(const Date& d)const;bool operator<(const Date& d)const;bool operator<=(const Date& d)const;~Date(){}
private:int _year;int _month;int _day;
};

Date.cpp 文件

#include "Date.h"//d1+=50
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month > 12){_year++;_month = 1;//_day -= GetMonthDay(_year, _month); // error - _month已经发生改变}}return *this;
}//d1+50(不会改变d1)
Date Date::operator+(int day) const
{//创建临时对象,改变贫道不改变己身Date tmp(*this);tmp += day;return tmp;
}// d1-=50
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){_month--;if (_month <= 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);;}return *this;
}
//// d1-50
//Date Date::operator-(int day) const
//{
//	Date tmp(*this);
//	tmp._day -= day;
//	while (tmp._day <= 0)
//	{
//		tmp._month--;
//		if (tmp._month <= 0)
//		{
//			tmp._year--;
//			tmp._month = 12;
//		}
//		tmp._day += GetMonthDay(tmp._year, tmp._month);//error -> 这里传的是const Date* this,权限放大
//	}
//	return tmp;
//}// d1-50
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}// d2 = ++d1
Date& Date::operator++()
{*this += 1;return *this;
}
// d2 = d1++
Date Date::operator++(int x)
{Date tmp(*this);*this += 1;return tmp;
}// --d1
Date& Date::operator--()
{*this -= 1;return *this;
}
// d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}bool Date::operator==(const Date& d) const
{return _year == d._year&& _month == d._month&& _day == d._day;
}bool Date::operator!=(const Date& d) const
{return !(*this == d);
}bool Date::operator>(const Date& d) const
{if (_year > d._year){return true;}else if (_year == d._year && _month > d._month){return true;}else if (_year == d._year && _month == d._month && _day > d._day){return true;}elsereturn false;
}
bool Date::operator>=(const Date& d) const
{return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d) const
{return !(*this >= d);
}
bool Date::operator<=(const Date& d) const
{return !(*this > d);
}int Date::operator-(const Date& d) const
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int count = 0;while (min != max){min++;count++;}return count * flag;
}//int Date::operator-(const Date& d)
//{
//	Date max = *this;
//	Date min = d;
//	int flag = 1;
//	int count = 0;
//	if (*this < d)
//	{
//		max = d;
//		min = *this;
//		flag = -1;
//	}
//	int day1 = max._day;
//	int day2 = min._day;
//	int n = max._year - min._year;
//	if (max._year == min._year)
//	{
//		n = 0;
//	}
//
//	// 2025 1 31
//	while (max._month != 1 || max._day != 1)
//	{
//		max._month--;
//		if (max._month <= 0)
//		{
//			max._month = 12;
//			max._year--;
//			break;
//		}
//		max._day = GetMonthDay(max._year, max._month);
//		day1 += max._day;
//	}
//	//2024 1 1
//	while (min._month != 1 || min._day != 1)
//	{
//		min._month--;
//		if (min._month <= 0)
//		{
//			min._month = 12;
//			min._year--;
//			break;
//		}
//		min._day = GetMonthDay(min._year, min._month);
//		day2 += min._day;
//	}
//	int x = min._year;
//	while (x < max._year)
//	{
//		count += Getleapyear(min._year + 1);
//		x++;
//	}
//
//	return day1 - day2 + 365 * n + count+1;
//}
http://www.xdnf.cn/news/20138.html

相关文章:

  • upload-labs通关笔记-第17关文件上传关卡之二次渲染jpg格式
  • 关于如何在PostgreSQL中调整数据库参数和配置的综合指南
  • Vue基础知识-脚手架开发-子传父(props回调函数实现和自定义事件实现)
  • Win11 解决访问网站525 问题 .
  • 【RK3576】【Android14】如何在Android kernel-6.1 的版本中添加一个ko驱动并编译出来?
  • Django 常用功能完全指南:从核心基础到高级实战
  • [光学原理与应用-401]:设计 - 深紫外皮秒脉冲激光器 - 元件 - 布拉格衍射在深紫外皮秒声光调制器(AOM)中的核心作用与系统实现
  • 小程序:12亿用户的入口,企业数字化的先锋军
  • Linux编程——网络编程(UDP)
  • 计算机网络模型入门指南:分层原理与各层作用
  • 对接旅游行业安全需求:旅游安全急救实训室的功能构建与育人目标
  • 网络安全初级-渗透测试
  • 用AI做TikTok影视解说,全流程全自动成片,不懂外语也能做全球矩阵!
  • 办公任务分发项目 laravel vue mysql 第一章:核心功能构建 API
  • 系统越拆越乱?你可能误解了微服务的本质!
  • 【Linux系统】线程同步
  • 正则表达式与转义符的区别。注意输入的东西经过了一次转义,一次正则表达式。\\转义是单斜杠\\在正则表达式也是单斜杠所以\\\\经过两道门才是字符单斜杠
  • MongoDB Change Streams:实时监听数据变化的实战场景
  • clickhouse迁移工具clickhouse-copier
  • Python EXCEL 小技巧:最快重新排列dataframe函数
  • 工业机器人标杆的数字化突围,珞石机器人如何以CRM实现业务重塑
  • 技术视界 | 跨域机器人通信与智能系统:打破壁垒的开源探索
  • 【Linux】环境变量与程序地址空间详解
  • ansible-角色
  • MySQL知识
  • 【C++】17. AVL树实现
  • 探索未来智能自动化,一个强大的自动化引擎
  • 苹果Vision Air蓝图或定档2027,三星/微美全息加速XR+AI核心生态布局卡位
  • 第二阶段WinForm-13:图表控件,N层架构,Dapper
  • 【数学建模学习笔记】机器学习分类:决策树分类