💞💞 前言
目录
通过下面的学习我们将构建简单日期计算器的各种功能实现:
1.日期类Date的构造
#include<iostream>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
inline int GetMonthDay(int year, int month)
{
int montharray[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;
}
return montharray[month];//月份天数数组设置为13,方便直接按月份返回
}
// 全缺省的构造函数
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)
{
if (*this != d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
}
// 析构函数
~Date();
//打印日期
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
// 日期+=天数
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;
};
2.日期类Date的实现
2.1获取某年某月的天数
// 获取某年某月的天数
inline int GetMonthDay(int year, int month)
{
int montharray[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;
}
return montharray[month];//月份天数数组设置为13,方便直接按月份返回
}
2.2默认成员函数的实现
2.2.1全缺省的构造函数
// 全缺省的构造函数
Date:: Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
//这里可以判断日期是否合理
if (month >= 13 && day > GetMonthDay(year, month))
{
cout << "日期非法输入" << endl;
}
}
2.2.2拷贝构造函数
// 拷贝构造函数
// d2(d1)
Date:: Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
2.2.3赋值运算符重载
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d)
{
if (*this != d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
}
2.2.4析构函数
//析构函数
Date::~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
2.3日期计算类函数
2.3.1日期+=天数
// 日期+=天数
Date& Date::operator+=(int day)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month >= 13)
{
_year++;
_month = 1;
}
}
return *this;
}
2.3.2日期+天数
// 写法1:日期+天数
Date Date::operator+(int day)
{
Date tmp = *this;
tmp += day;//利用之前实现过的+=实现
return tmp;
}
//写法2:日期+天数
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++;
if (tmp._month >= 13)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
2.3.3日期-=天数
// 日期-=天数
Date& Date::operator-=(int day)
{
_day -= day;
while (_day < 0)
{
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
2.3.4日期-天数
// 日期-天数
Date Date::operator-(int day)
{
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);
}*/
tmp -= day;
return tmp;
}
2.3.5日期-日期 返回天数
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
//先找较大的日期
Date max = *this;
Date min = d;
//如果*this的日期比d大的话就正常计数
int flag = 1;
//如果*this的日期比d小的话就先正常计数之后再*(-1)即可,-1用flag来标识
if (*this < d)
{
min = *this;
max = d;
flag = -1;
}
int CountDay = 0;
while (min < max)
{
++min;
CountDay++;
}
return flag * CountDay;
}
2.4运算符重载类函数
2.4.1 >运算符重载
// >运算符重载
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;
}
2.4.2 ==运算符重载
// ==运算符重载
bool Date::operator==(const Date& d)
{
if ((_year == d._year) && (_month == d._month) && (_day == d._day))
return true;
return false;
}
2.4.3 >=运算符重载
// >=运算符重载
bool Date::operator >= (const Date& d)
{
if (*this > d || *this == d)
return true;
return false;
}
2.4.4 <运算符重载
// <运算符重载
bool Date::operator < (const Date& d)
{
return !(*this >= d);
}
2.4.5 <=运算符重载
// <=运算符重载
bool Date::operator <= (const Date& d)
{
return !(*this > d);
}
2.4.6 !=运算符重载
// !=运算符重载
bool Date::operator != (const Date& d)
{
return !(*this == d);
}
2.4.7 前置++与后置++
- 前置++
// 前置++
Date& Date::operator++()
{
/*_day++;
if (_day > GetMonthDay(_year, _month))
{
_month++;
if (_month >= 13)
{
_year++;
}
}*/
*this += 1;
return *this;
}
- 后置++
// 后置++
Date Date::operator++(int)
{
Date tmp = *this;
/*_day++;
if (_day > GetMonthDay(_year, _month))
{
_month++;
if (_month >= 13)
{
_year++;
}
}*/
*this += 1;
return tmp;
}
2.4.8 前置–与后置–
- 前置–
// 前置--
Date& Date::operator--()
{
return *this -= 1;
}
- 后置–
// 后置--
Date Date:: operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
3.完整代码+运行结果
- date.h
#pragma once
#include<iostream>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
// 获取某年某月的天数
inline int GetMonthDay(int year, int month)
{
int montharray[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;
}
return montharray[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)
{
if (*this != d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
}
// 析构函数
~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);
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
- date.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"date.h"
// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
//这里可以判断日期是否合理
if (month >= 13 && day > GetMonthDay(year, month))
{
cout << "日期非法输入" << endl;
}
}
// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// 日期+=天数
Date& Date::operator+=(int day)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month >= 13)
{
_year++;
_month = 1;
}
}
return *this;
}
// 日期+天数
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++;
// if (tmp._month >= 13)
// {
// tmp._year++;
// tmp._month = 1;
// }
//
//}
tmp += day;
return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
_day -= day;
while (_day < 0)
{
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 日期-天数
Date Date::operator-(int day)
{
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);
}*/
tmp -= day;
return tmp;
}
// 前置++
Date& Date::operator++()
{
/*_day++;
if (_day > GetMonthDay(_year, _month))
{
_month++;
if (_month >= 13)
{
_year++;
}
}*/
*this += 1;
return *this;
}
// 后置++
Date Date::operator++(int)
{
Date tmp = *this;
/*_day++;
if (_day > GetMonthDay(_year, _month))
{
_month++;
if (_month >= 13)
{
_year++;
}
}*/
*this += 1;
return tmp;
}
// 后置--
Date Date:: operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
// 前置--
Date& Date::operator--()
{
return *this -= 1;
}
// >运算符重载
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)
{
return !(*this >= d);
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{
return !(*this > d);
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
return !(*this == d);
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
//先找较大的日期
Date max = *this;
Date min = d;
//如果*this的日期比d大的话就正常计数
int flag = 1;
//如果*this的日期比d小的话就先正常计数之后再*(-1)即可,-1用flag来标识
if (*this < d)
{
min = *this;
max = d;
flag = -1;
}
int CountDay = 0;
while (min < max)
{
++min;
CountDay++;
}
return flag * CountDay;
}
//析构函数
Date::~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
// //流输出
//ostream operator<<(ostream& out, const Date d)
//{
// out << d._year << "/" << d._month << "/" << d._day << endl;
// return out;
//}
//
//
流插入
//istream operator>>(istream& in, const Date d)
//{
// in >> d._year;
//}
//流插入
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "/" << d._month << "/" << d._day << endl;
return out;
}
//流提取
istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
- test.cpp
int main()
{
Date d1(2024, 4, 20);
cout << "Today is " << endl;
d1.Print();
cout << "往后100天是:" << endl;
Date(d1 + 100).Print();
cout << "往前100天是:" << endl;
Date(d1 - 100).Print();
cout << "今天与2004年12月14日相差:" << endl;
int day = d1 - Date(2004, 12, 14);
cout << day << endl;
return 0;
}
结果如下:
4.结语
以上只是一个简单的日期类示例,实际的日期类可能还包括其他功能,例如日期的格式化等操作。这里只是提供了一个起点,大家可以根据自己的需求对日期类进行扩展。以上就是简单日期类的所有内容啦 ~ 完结撒花 ~🥳🎉🎉