1. 头文件
cpp
#pragma once
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1);
int GetMonthDay();
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& operator+=(int day);
Date operator+(int day) const;
Date& operator-=(int day);
Date operator-(int day) const;
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
int operator-(const Date& d) const;
friend ostream& operator<<(ostream& _out, const Date& d);
friend istream& operator>>(istream& _in, Date& d);
void Print() const;
private:
int _year;
int _month;
int _day;
};
2. 主函数的实现
cpp
#include "Date.h"
Date::Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{}
int Date::GetMonthDay()
{
int day[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 day[2] + 1;
}
return day[_month];
}
bool Date::operator>(const Date & d)const
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if (_month == d._month)
{
return _day > d._day;
}
}
return 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);
}
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);
}
Date& Date::operator+=(int day)
{
_day += day;
while (_day > GetMonthDay())
{
_day -= GetMonthDay();
++_month;
if (_month > 12)
{
_month = 1;
++_year;
}
}
return *this;
}
Date Date::operator+(int day) const
{
Date tmp = *this;
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
_day -= day;
while (_day < 1)
{
--_month;
if (_month < 1)
{
_month = 12;
--_year;
}
_day += GetMonthDay();
}
return *this;
}
Date Date::operator-(int day) const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
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;
}
int Date::operator-(const Date& d) const
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
flag = -1;
max = d;
min = *this;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return (n * flag);
}
void Date::Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
ostream& operator<<(ostream& _out, const Date& d)
{
_out << d._year << "/" << d._month << "/" << d._day;
return _out;
}
istream& operator>>(istream& _in, Date& d)
{
_in >> d._year >> d._month >> d._day;
return _in;
}
3. 目录文件
cpp
#include "Date.h"
int main()
{
int input = 0;
do {
cout << " ********** 欢迎进入日期计算器 ****************" << endl;
cout << " ********** 1.计算两日期差几天 ****************" << endl;
cout << " ********** 2.计算某日往后n天 ****************" << endl;
cout << " ********** 3.计算某日往前n天 ****************" << endl;
cout << " ********** 0. 退出系统 ****************" << endl;
printf("\n\n\n");
cout << "请选择:";
cin >> input;
if (input == 1)
{
Date d1;
Date d2;
cout << "请输入第一个日期:";
cin >> d1;
cout << "请输入第二个日期:";
cin >> d2;
cout << d1 << "和" << d2 << "相差" << (d2 - d1) << "天" << endl;
printf("\n\n\n");
}
else if (input == 2)
{
Date d1;
int day = 0;
cout << "请输入某日日期:";
cin >> d1;
cout << "请输入往后多少天:";
cin >> day;
cout << d1 << "往后" << day << "天是" << (d1 + day) << endl;
printf("\n\n\n");
}
else if (input == 3)
{
Date d1;
int day = 0;
cout << "请输入某日日期:";
cin >> d1;
cout << "请输入往前多少天:";
cin >> day;
cout << d1 << "往前" << day << "天是" << (d1 - day) << endl;
printf("\n\n\n");
}
else if (input == 0)
{
break;
}
else
{
cout << "请输入正确的选项" << endl;
printf("\n\n\n");
}
} while (input);
}
有需要自取 gitee 链接 :日期计算器------有趣的中国人的gitee