<strong>万年历项目:.cpp文件如下</strong>
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)// 初始化列表进行初始化。
:_year(year),
_month(month),
_day(day)
{
if (CheckDate() == false) //对不合法日期,设置默认值。
{
_year = 1900;
_month = 1;
_day = 1;
}
}
Date(const Date& d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
Date& operator= (const Date& d)
{
if (this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
public:
bool CheckDate() //判断日期是否为合法日期
{
if ((_day<1 || _day>DayOfMonth(_year, _month)
) || (_month<1 || _month>12) || _year <1)
return false;
return true;
}
//判断平年闰年
static bool IsLeap(int year) //若是闰年,返回true,平年返回false
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
return false;
}
//判断某月的天数
static int DayOfMonth(int year, int month)
{
if (year < 1 || month<1 || month>12)
{
return 0;
}
static int month_day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//闰年二月是29天
if (IsLeap(year) == true)
month_day[2] = month_day[2] + 1;
return month_day[month];
}
// 比较运算符的重载
public:
bool operator == (const Date& d)
{
//if (this->_year == d._year&&this->_month == d._month&&this->_day == d._day)
// return true;
//else
// return false;
return (this->_year == d._year&&this->_month == d._month&&this->_day == d._day);
}
bool operator != (const Date& d)
{
return !(this->_year == d._year&&this->_month == d._month&&this->_day == d._day);
}
bool operator > (const Date& d)
{
if (this->_year > d._year)
return true;
else if (this->_year == d._month&&this->_month > d._month)
return true;
else if (this->_year == d._month&&this->_month == d._month&&this->_day > d._day)
return true;
else
return false;
}
bool operator >= (const Date& d)
{
return (*this > d || *this == d);
}
bool operator < (const Date& d)
{
return !(*this>=d);
}
bool operator <= (const Date& d)
{
return !(*this>d);
}
//计算一个日期加减一个天数以后的日期。
void ModifyDate() //将一个不合法日期改为一个合法日期
{
while (_day > DayOfMonth(_year, _month))
{
_day -= DayOfMonth(_year, _month);
_month += 1;
if (_month > 12);
{
_month = 1;
_year++;
}
}
while (_day <= 0)
{
_month--;
if (_month <1)
{
_month = 12;
_year--;
}
_day += DayOfMonth(_year, _month);
}
}
Date operator+(int day)
{
Date d(*this);
d._day += day;
d.ModifyDate();
return d;
}
Date operator-(int day)
{
Date d(*this);
d._day -= day;
d.ModifyDate();
return d;
}
Date& operator-=(int day)
{
_day -= day;
ModifyDate();
return *this;
}
Date& operator+=(int day)
{
_day -= day;
ModifyDate();
return *this;
}
const Date& operator++() // 前置++
{
++_day;
ModifyDate();
return *this;
}
Date operator++(int) // 后置++
{
Date d(*this);
d = this->_day++;
this->ModifyDate();
return d;
}
const Date& operator--() // 前置--
{
--_day;
ModifyDate();
return *this;
}
Date operator--(int) // 后置--
{
Date d(*this);
d._day= _day--;
d.ModifyDate();
this->ModifyDate();
return d;
}
// 两个日期相加没有意义
// 计算两个日期相减以后的差的天数
int operator-(const Date& d)
{
int day=0;
if (*this < d)
{
while (*this !=d)
{
*this += 1;
day++;
}
}
else if (*this>d)
{
Date tmp = d;
while (*this != tmp)
{
tmp += 1;
day++;
}
}
return day;
}
//输入输出流运算符重载
/*
为什么"operator<<"运算符重载一定要定义为友元函数?
如果重载双目操作符(即为类的成员函数),就只要设置一个参数作为右侧运算量。而左侧运算量就是对象本身……
而>>或<<左侧运算量是cin或cout,而不是对象本身,所以不满足后一点,故,只能声明为友元函数了……
*/
friend ostream& operator <<(ostream& out, Date& d)
{
out << d._year<<"-";
out << d._month<<"-";
out << d._day<<endl;
return out;
}
friend istream& operator >>(istream&in, Date& d)
{
cout<<"请依次输入年,月,日" << endl;
in >> d._year;
in >> d._month;
in >> d._day;
return in;
}
private:
int _year;
int _month;
int _day;
};
void Test()
{
Date d1,d2;
int input, day;
do{
cout << "************************************************" << endl;
cout << "***************请选择功能***********************" << endl;
cout << "*************1.推算几天后的日期*****************" << endl;
cout << "*************2.计算两个日期相差的天数***********" << endl;
cout << "*************3.计算几天之前的日期***************" << endl;
cout << "*************0.退出*****************************" << endl;
cin >> input;
switch (input)
{
case 1:
cout << "***************请输入日期***********************" << endl;
cin >> d1;
cout << "***************请输入天数***********************" << endl;
cin >> day;
cout << day << "天后的日期为:" << d1 + day << endl;
break;
case 2:
cout << "***************请输入两个日期*******************" << endl;
cout << "第一个日期为:";
cin >> d1;
cout << "第二个日期为:";
cin >> d2;
day = d1 - d2;
cout << "两日期相差天数为:" << day << endl;
break;
case 3:
cout << "***************请输入日期***********************" << endl;
cin >> d1;
cout << "***************请输入天数***********************" << endl;
cin >> day;
cout << day << "天之前的日期为:" << d1 - day << endl;
break;
default:
break;
}
} while (input);
}
int main()
{
Test();
return 0;
}