目录
一、头文件
#pragma once
#include <iostream>
using namespace std;
enum {
Exit,
Add,
SubDay,
Tomorrow,
Yesterday,
DaySub
};
class Date
{
public:
Date(int year=2022, int month=10, int day=9)
{
_year = year;
_month = month;
_day = day;
}
void print()
{
cout << _year<<" "<< _month <<" "<< _day << endl;
}
//逻辑运算
bool operator==(const Date& d) const;
bool operator!=(const Date& d)const;
Date& operator=(const Date& d);
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;
int operator-(const Date& d)const;
//前置
Date& operator++();
//后置
Date operator++(int);
//前置
Date& operator--();
//后置
Date operator--(int);
//类流输出
friend ostream& operator<<(ostream& out,const Date&d);
//类流输入
friend istream& operator>>(istream& in, Date& d);
private:
int _year;
int _month;
int _day;
};
二、test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
int main()
{
cout << "***************************************************" << endl;
cout << "********* 1.日期加天数 2.日期减天数 ********" << endl;
cout << "********* 3.计算后一天 4.计算前一天 ********" << endl;
cout << "********* 5.两日期相减 0.退出 ********" << endl;
cout << "***************************************************" << endl;
int input;
do
{
cout << "请输入要实现功能:" ;
cin >> input;
switch (input)
{
case Exit:break;
case Add:
{
Date d;
cout << "请输入日期:";
cin >> d;
int day;
cout <<"请输入要加天数:" ;
cin >> day;
cout<<d+day;
break;
}
case SubDay:
{
Date d;
cout << "请输入日期:" ;
cin >> d;
int day;
cout << "请输入要减天数:" ;
cin >> day;
cout<< d-day;
break;
}
case Tomorrow:
{
Date d;
cout << "请输入日期:" ;
cin >> d;
cout << ++d;
break;
}
case Yesterday:
{
Date d;
cout << "请输入日期:";
cin >> d;
cout << --d;
break;
}
case DaySub:
{
Date d1, d2;
cout << "请输入第一个日期:";
cin >> d1;
cout<< "请输入第二个日期:" ;
cin >> d2;
cout << d1 - d2<<endl;
break;
}
default:
{
cout << "输入错误,请重新输入"<<endl;
break;
}
}
} while (input);
return 0;
}
三、获取每个月天数
int Getmonthday(int year, int month)
{
//静态避免每次调用重复开辟空间
static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
MonthDay[2] = 29;
}
return MonthDay[month];
}
四、逻辑运算符函数
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
{
if (_year > d._year)
return true;
else if (_year == d._year && _month > d._month)
return true;
else if (_year == d._year && _month == d._month && _day > d._day)
return true;
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);
}
五、算数运算符函数
Date& Date::operator=(const Date& d)
{
//先判断是否自己赋值给自己
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
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)const
{
Date tmp(*this);
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
_day += Getmonthday(_year, _month);
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
}
return *this;
}
int Date::operator-(const Date& d)const
{
Date max = *this;
Date min = d;
int flag = 1;
if (d<(*this))
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (max != min)
{
n++;
min++;
}
return n * flag;
}
Date Date::operator-(int day)const
{
Date tmp(*this);
tmp -= day;
return tmp;
}
//前置
Date& Date::operator++()
{
return *this += 1;
}
//后置
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
//前置--
Date& Date::operator--()
{
return *this -= 1;
}
//后置--
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
六、类对象的流输入、输出
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;
}