日期是我们日常生活中经常需要处理的数据类型之一。在编程中,我们经常需要对日期进行各种操作,比如计算两个日期之间的天数差、日期加减天数、比较日期先后等。本文将介绍一个完整的日期类实现,它封装了这些常用功能。
日期类的设计思路
我们的日期类主要包含以下功能:
-
存储年、月、日信息
-
日期合法性检查
-
日期加减运算
-
日期比较
-
输入输出支持
头文件(Date.h)
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date& d); //重载流插入运算符
friend istream& operator>>(istream& in, Date& d); //重载流提取运算符
public:
// 获取某年某月的天数
static int GetMonthDay(int year, int month);
// 检查日期是否正确
bool CheckDate() const;
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
Date(const Date& d);
// 赋值运算符重载
Date& operator=(const Date& d);
// 析构函数
~Date();
// 日期+=天数
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);
private:
int _year;
int _month;
int _day;
};
源文件(Date.cpp)
#include "Date.h"
// 获取某年某月的天数
int Date::GetMonthDay(int year, int month) {
//定义为静态变量,避免频繁访问不断创建数组
static int _arr[13] = {-1, 31,28,31,30,31,30,31,31,30,31,30,31 };
//闰年的情况
if (month == 2) {
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
return _arr[month] + 1; //闰年多一天
}
}
return _arr[month];
}
// 检查日期是否正确
bool Date::CheckDate() const {
//非法日期检查
if (_year < 1 || _month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month)) {
return false;
}
return true;
}
// 全缺省的构造函数,头源文件分离,源文件不能带缺省值
Date::Date(int year, int month, int day)
: _year(year),
_month(month),
_day(day) {
//检查日期是否合法
if (!CheckDate()) {
cout << "非法日期" << endl;
}
}
// 拷贝构造函数
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;
}
// <<流插入运算符重载
ostream& operator<<(ostream& out,const Date& d) {
out << d._year << '/' << d._month << '/' << d._day;
return out;
}
// <<流提取运算符重载
istream& operator>>(istream& in, Date& d) {
while (1) {
cout << "请输入年月日:";
in >> d._year >> d._month >> d._day;
if (!d.CheckDate()) {
cout << "非法日期:" << d << endl;
cout << "请重新输入" << endl;
}
else {
break;
}
}
return in;
}
// 日期+=天数
Date& Date::operator+=(int day) {
//加的日期为负数时
if (day < 0) {
day = -day;
return *this -= day;
}
_day = _day + day;
while(_day>GetMonthDay(_year,_month)){
//每月满了则月份进1
_day -= GetMonthDay(_year, _month);
_month += 1;
//满一年则年进1,月份重置为1
if (_month > 12) {
_year += 1;
_month = 1;
}
}
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-=(int day) {
//减的日期为负数时
if (day < 0) {
day = -day;
return *this += day;
}
_day -= day;
while (_day < 1) {
_month -= 1;
//当月份小于1时,向年借位,年减1,月重置为12
if (_month < 1) {
_year -= 1;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 前置++
Date& Date::operator++() {
*this += 1; //复用+=
return *this;
}
// 后置++(为了区分前置和后置,c++规定后置必须多一个参数,调用时编码器会自动识别补上该形参)
Date Date::operator++(int) {
Date tmp = *this;
*this += 1;
return tmp; //传值返回
}
// 前置--
Date& Date::operator--() {
*this -= 1; //复用-=
return *this;
}
// 后置--
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 {
//再比较月
if (_month > d._month) return true;
else if (_month < d._month) return false;
else {
//最后比较日
if (_day > d._day) return true;
else if (_day < d._day) return false;
else return false; //相等的情况
}
}
}
// ==运算符重载
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) || (*this == d);
}
// <运算符重载
bool Date::operator < (const Date& d) {
return !(*this >= d);
}
// <=运算符重载
bool Date::operator <= (const Date& d) {
return (*this < d) || (*this == d);
}
// !=运算符重载
bool Date::operator != (const Date& d) {
return !(*this == d);
}
// 日期-日期 返回天数
int Date::operator-(const Date& d) {
//先比较两个日期的大小,用大的减小的
Date big = *this;
Date small = d;
int flag = -1; //-1为过去,1为倒计时
int day = 0; //计算日期
if (*this < d) {
big = d;
small = *this;
flag = 1;
}
while (big != small) {
--big;
++day;
}
return flag * day;
}
关键功能详解
1. 获取月份天数
GetMonthDay 函数用于获取某年某月的天数,特别处理了闰年二月的情况。使用静态数组存储各月份天数,提高效率。
2. 日期合法性检查
CheckDate 函数检查日期是否合法,包括年份、月份和日期的有效性,以及特定月份的天数是否正确。
3. 日期加减运算
通过重载 +、-、+=、-= 等运算符,实现了日期的加减运算。处理了跨月、跨年的情况,并支持负数运算(负数的加减会转换为相反操作)。
4. 自增自减运算
实现了前置和后置的自增自减运算符,符合C++的惯例。
5. 日期比较
重载了所有比较运算符,可以方便地比较两个日期的先后关系。
6. 日期差值计算
通过重载 - 运算符,可以计算两个日期之间的天数差,返回值为正表示过去的天数,为负表示未来的天数。
7. 输入输出支持
通过友元函数重载了 << 和 >> 运算符,使得日期对象可以直接用于输入输出流。
使用示例
#include "Date.h"
#include <iostream>
int main() {
Date d1(2023, 5, 15);
Date d2 = d1 + 30; // 加30天
std::cout << "d1: " << d1 << std::endl;
std::cout << "d2: " << d2 << std::endl;
int days = d2 - d1; // 计算两个日期之间的天数差
std::cout << "Days between d1 and d2: " << days << std::endl;
return 0;
}
3万+

被折叠的 条评论
为什么被折叠?



