下面是用前两篇C++类和对象的知识写的日期计算器:
废话不多说,我们直接上源码!
Date.h :
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year, int month, int day);//构造函数
int GetMonthDay(int year, int month) const;//获取year年month月的天数
void Print() 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;//判断当前日期是否在传入日期之后或相同
//计算day天后的日期
Date& operator+=(int day);//将当前日期修改为day天后的日期
Date operator+(int day) const;//返回day天后的日期
Date& operator++();//让日期前置++
Date operator++(int);//让日期后置++(这里的int只是用来占位,与前置++构成重载)
//计算day天前的日期
Date& operator-=(int day);//将当前日期修改为day天前的日期
Date operator-(int day) const;//返回day天前的日期
Date& operator--();//让日期前置--
Date operator--(int);//让日期后置--(这里的int只是用来占位,与前置--构成重载)
//计算两个日期之间的天数
int operator-(const Date& d) const;
private:
int _year;
int _month;
int _day;
};
Date.cpp :
#include"Date.h"
int Date::GetMonthDay(int year, int month) const
{
int MonthDay[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//用数组来依次存储平年1到12月的天数
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))//判断是否为闰年的2月
{
return 29;
}
return MonthDay[month - 1];
}
Date::Date(int year, int month, int day)
{
if (month<1||month>12||(day<1 || day>GetMonthDay(year, month)))//判断日期是否合法
{
cout << "Illegal date!" << endl;
}
else
{
_year = year;
_month = month;
_day = day;
}
}
void Date::Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
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
{
return _year < d._year || (_year == d._year && _month < d._month) || (_year == d._year && _month == d._month && _day < d._day);
}
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);
}
Date& Date::operator+=(int day)//这里使用传引用返回是因为函数结束后this指针所指向的空间还存在,此时使用传引用返回会减少临时空间的开辟
{
if (day < 0)//防止传入负数
{
*this -= -day;
}
_day += day;//先将day天全部加在_day上
while (_day > GetMonthDay(_year, _month))//如果_day超过当年当月的天数就开始进位
{
_day -= GetMonthDay(_year, _month);//扣除当年当月的天数
++_month;//月份加一
if (_month == 13)//防止月份超过12
{
_month = 1;
++_year;
}
}
return *this;
}
Date Date::operator+(int day) const
{
Date temp = *this;//使用临时变量,防止this所指向的空间数据被修改
temp += day;//直接复用+=运算重载
return temp;
}
Date& Date::operator++()
{
*this += 1;//直接复用+=运算重载
return *this;
}
Date Date::operator++(int)//(这里的int只是用来占位,与前置++构成重载,这里编译器会随机传入一个int类型的值但并没有实际意义)
{
Date temp = *this;//使用临时变量,作为没被修改时的返回值
*this += 1;//直接复用+=运算重载
return temp;
}
Date& Date::operator-=(int day)
{
if (day < 0)//防止传入负数
{
*this += -day;
}
_day -= day;//先将_day减去day天
while (_day <= 0)//如果_day是负值就像当年当月的前一个月的天数就开始借位
{
if (--_month == 0)//防止月份小于0
{
_month = 12;
--_year;
}
_day += GetMonthDay(_year, _month);//借位补给_day
}
return *this;
}
Date Date::operator-(int day) const
{
Date temp = *this;
temp -= day;//直接复用-=运算重载
return temp;
}
Date& Date::operator--()
{
*this -= 1;//直接复用-=运算重载
return *this;
}
Date Date::operator--(int)//(这里的int只是用来占位,与前置--构成重载,这里编译器会随机传入一个int类型的值但并没有实际意义)
{
Date temp = *this;//使用临时变量,作为没被修改时的返回值
*this -= 1;//直接复用-=运算重载
return temp;
}
int Date::operator-(const Date& d) const
{
Date min = d;
Date max = *this;
int flag = 1;
if (*this < d)
{
min = *this;
max = d;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
test.cpp :
#include"Date.h"
void Test1()
{
Date d1(2020, 1, 1);
Date d2(2012, 5, 19);
cout << (d1 == d2) << endl;
cout << (d1 != d2) << endl;
cout << (d1 < d2) << endl;
cout << (d1 <= d2) << endl;
cout << (d1 > d2) << endl;
cout << (d1 >= d2) << endl;
}
void Test2()
{
Date d1(2020, 1, 1);
Date d2(2012, 5, 19);
d1 += 506;
d1.Print();
Date ret1 = d1 + 50;
ret1.Print();
++d1;
d1.Print();
Date ret2 = d1++;
ret2.Print();
d1.Print();
d2 -= 582;
d2.Print();
Date ret3 = d2 + 50;
ret3.Print();
++d2;
d2.Print();
Date ret4 = d2++;
ret4.Print();
d2.Print();
}
void Test3()
{
Date d1(1949, 10, 1);
Date d2(2023, 3, 23);
cout << d1 - d2 << endl;
cout << d2 - d1 << endl;
}
int main()
{
Test1();
Test2();
Test3();
return 0;
}
运行效果: