c++通过运算符重载实现的日期类(源码)

devtools/2024/10/16 2:27:46/

通过我们对运算符重载的学习,我们可以由此实现一个日期类。

头文件:

#pragma once
#include<iostream>
#include<assert.h>
using std::endl;
using std::cout;
using std::cin;
using std::ostream;
using std::istream;class Date{//声明友元friend ostream& operator<<(ostream& out, Date& d1);friend istream& operator>>(istream& in, Date& d1);
public:// 全缺省的构造函数Date(int year=-1, int month=-1, int day=-1);// 拷贝构造函数// d2(d1)Date(const Date& da);// 析构函数~Date();//打印日期函数void Print();//判断日期是否合法bool DateCheck();// 赋值运算符重载// d2 = d3 -> d2.operator=(&d2, d3)Date& operator=(const Date&da);// 日期+=天数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--();// >运算符重载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);void operator()(istream& in, Date& d1);private:int _year;int _month;int _day;};//流插入重载
ostream& operator<<(ostream& out, Date& d1);//流提取重载
istream& operator>>(istream& in, Date &d1);

函数实现文件:

#include"Date.h"// 获取某年某月的天数
int GetMonthDay(int year, int month)
{assert(month > 0 && month < 13);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 arr[2] + 1;return arr[month];
}bool Date::DateCheck()
{if (_month <= 0 || _month > 12 ||_day<=0|| _day > GetMonthDay(_year, _month)){return false;}return true;
}// 全缺省的构造函数
Date::Date(int year, int month, int day)
{//cout << "Date::Date(int year, int month, int day)" << endl;_year = year;_month = month;_day = day;
}// 拷贝构造函数
// d2(d1)
Date::Date(const Date& da)  
{_day=da._day;_year=da._year ;_month=da._month ;
}Date::~Date()
{/*cout << "Date::~Date()" <<endl ;*/_year = -1;_day = -1;_month = -1;
}void Date::Print()
{cout << _year << " " << _month << " " << _day << " " << endl;
}// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const  Date& da)
{_year = da._year;_month = da._month;_day = da._day;return *this;
}// 日期+=天数Date& Date::operator+=(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+(int day)
{Date temp = *this;temp += day;return temp;
}// 日期-天数
Date Date::operator-(int day)
{Date temp = *this;*this -= day;return *this;
}// 日期-=天数
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){_month--;if (_month == 0){_month = 12;_year -= 1;}_day += GetMonthDay(_year, _month);}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;
}// >运算符重载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;}}}}}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)){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)){return true;}return false;
}// 日期-日期 返回天数int Date::operator-(const Date& d)
{int falg = 1;Date max =*this;Date min = d;if (*this < d){max = d;min = *this;falg = -1;}int n = 0;while (min < max){++min;n++;}return n * falg;
}//流提取
ostream& operator<<(ostream& out, Date&d1)
{out << d1._year << " " << d1._month << " " << d1._day << " " ;return cout;
}//流插入
istream& operator>>(istream& in, Date& d1)
{cout << "请输入合法日期喵~O(∩_∩)O" << endl;in >> d1._year >> d1._month >> d1._day;while(!d1.DateCheck()){cout << "日期不合法喵~/(ㄒoㄒ)/~~" << endl;cout << "请输入合法日期喵~o(=^ェ^=)m" << endl;in >> d1._year >> d1._month >> d1._day;}return in;
}

gitee网站:https://gitee.com/zgw486/cjj


http://www.ppmy.cn/devtools/4573.html

相关文章

口型动画论文2:《基于语音驱动的表情动画设计与实现》

说明 本文是北京邮电大学的硕士毕业论文&#xff0c;作者是郭梦婷。由于是艺术硕士&#xff0c;所以本文没有罗列很多公式&#xff0c;而是从动画创作的角度来写如何根据语音设计动画人物的嘴型及表情。本文作者行文缜密、轻松&#xff0c;举得例子都是一些热播的动画和电影&a…

ChatGPT深度科研应用、数据分析及机器学习、AI绘图与高效论文撰写

2022年11月30日&#xff0c;可能将成为一个改变人类历史的日子——美国人工智能开发机构OpenAI推出了聊天机器人ChatGPT3.5&#xff0c;将人工智能的发展推向了一个新的高度。2023年4月&#xff0c;更强版本的ChatGPT4.0上线&#xff0c;文本、语音、图像等多模态交互方式使其在…

Vue3: toRefs与toRef的基本使用

一、前言 本文主要介绍toRefs与toRef的基本使用。 二、内容 1、基本概念 作用: toRefs与toRef可以将一个响应式对象中的每一 个属性&#xff0c;转换为ref对象&#xff1b;不同 toRefs与toRef功能一致&#xff0c;但toRefs可以批量转换。 2、toRefs 如果把reactive定义的…

产品推荐 | 基于Lattice用于原型和FPGA设计和开发的Avant-E 评估板

01 产品概述 莱迪思半导体Avant-E评估板使设计人员能够快速进行原型设计和FPGA设计测试。它提供对所有 I/O 的访问&#xff0c;以及广泛的内存选项&#xff0c;以实现更快的原型设计和开发。 Avant-E评估板采用LFG1156封装的Avant-E FPGA。该板可以通过 FMC HPC、PMAD 和 Ras…

Python机器学习项目开发实战:可视化数据

注意&#xff1a;本文的下载教程&#xff0c;与以下文章的思路有相同点&#xff0c;也有不同点&#xff0c;最终目标只是让读者从多维度去熟练掌握本知识点。 下载教程&#xff1a;Python机器学习项目开发实战_可视化数据_编程案例解析实例详解课程教程.pdf 在Python机器学习项…

爬虫 //获取元素中的数据

// <!--jsoup解析工具所需依赖--> // <dependency> // <groupId>org.jsoup</groupId> // <artifactId>jsoup</artifactId> // <version>1.10.3</version> // </depende…

通过PyCharm平台开发Django应用程序

学会使用命令行工具开发Django应用程序是基础&#xff0c;不过更多的时候还是要借助平台开发工具。目前&#xff0c;最好的Django应用程序开发工具就是jetBrains公司推出的PyCharm平台了。 借助PyCharm开发平台&#xff0c;可以极大提高开发Django应用程序的效率&#xff0c;同…

【即插即用】空间注意力机制(附源码)

简单讲解&#xff1a; 一个简单的空间注意力机制模块的实现通常包含一个基于平均池化和最大池化的特征提取过程&#xff0c;然后通过卷积操作和 Sigmoid 激活函数生成注意力权重。 空间注意力机制的作用和优势&#xff1a; 特征增强&#xff1a;在这段代码中&#xff0c;通过…