Date .h
#pragma once
#include <iostream>
using namespace std;
class Date
{
friend void PrintCalendar(int year, int month); //打印日历
friend istream& operator>>(istream& is, Date& d);
friend ostream& operator<<(ostream& os, Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
if (Illegal())
{
cout << "输入日期不合法" << endl;
_year = 1600;
_month = 1;
_day = 1;
}
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator=(const Date& d);
public:
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);
//日期计算器
Date operator+(int day);
Date operator+=(int day);
Date operator-(int day);
Date operator-=(int day);
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
int operator-(const Date& d); //计算两个日期之间相隔的天数
private:
bool Illegal(); //检查日期是否非法
void ChangeLegal(Date& date); //将日期化为合法日期
bool IsLeap(int year);//检查闰年
int DayOfMonth(int year, int month); //计算该日期中当月的天数
private:
int _year;
int _month;
int _day;
};
void PrintCalendar(int year, int month); //打印日历
istream& operator>>(istream& is, Date& d);
ostream& operator<<(ostream& os, Date& d);
Date.cpp
#include"Date.h"
bool Date::operator==(const Date& d)
{
return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
bool Date::operator<(const Date& d)
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month < d._month)
{
return true;
}
else if (_month == d._month)
{
if (_day < d._day)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
bool Date::operator<=(const Date& d)
{
return operator==(d) || operator<(d);
}
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if (_month == d._month)
{
if (_day > d._day)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
bool Date::operator>=(const Date& d)
{
return operator==(d) || operator>(d);
}
Date& Date::operator=(const Date& d)
{
if (*this == d)
{
return *this;
}
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
Date Date::operator+(int day)
{
Date add(*this);
add._day += day;
ChangeLegal(add);
return add;
}
Date Date::operator+=(int day)
{
_day += day;
ChangeLegal(*this);
return *this;
}
Date Date::operator-(int day)
{
Date del(*this);
del._day -= day;
ChangeLegal(del);
return del;
}
Date Date::operator-=(int day)
{
_day -= day;
ChangeLegal(*this);
return *this;
}
Date& Date::operator++()
{
_day++;
ChangeLegal(*this);
return *this;
}
Date Date::operator++(int) //后置++,返回值没+1,其实本身已经+1
{
Date ret(*this);
_day++;
ChangeLegal(*this);
return ret;
}
Date& Date::operator--()
{
_day--;
ChangeLegal(*this);
return *this;
}
Date Date::operator--(int)
{
Date ret(*this);
_day--;
ChangeLegal(*this);
return ret;
}
int Date::operator-(const Date& d)
{
int day = 0;
Date small(d);
Date big(*this);
if (*this < d)
{
small = *this;
big = d;
}
else
{
small = d;
big = *this;
}
while (small != big)
{
small++;
day++;
}
return day;
}
bool Date::Illegal() //检查日期是否非法
{
if ((_year < 0) ||
(_month < 0 || _month>12) ||
(_day<0 || _day>DayOfMonth(_year, _month)))
{
return true;
}
else
{
return false;
}
}
void Date::ChangeLegal(Date& date) //将日期转为合法日期
{
if (date._day < 1)
{
while (date._day < 1)
{
date._month--;
date._day += DayOfMonth(date._year, date._month);
if (date._month < 1)
{
date._year--;
date._month = 12;
}
}
}
else if (date._day > DayOfMonth(date._year, date._month))
{
while (date._day > DayOfMonth(date._year, date._month))
{
date._day -= DayOfMonth(date._year, date._month);
date._month++;
if (date._month > 12)
{
date._year++;
date._month = 1;
}
}
}
}
bool Date::IsLeap(int year) //检查该年是否为闰年
{
if (year % 400 == 0)
return true;
else
{
if (year % 4 == 0 && year % 100 != 0)
return true;
else
return false;
}
}
int Date::DayOfMonth(int year, int month) //返回该月的天数
{
static int day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (IsLeap(year) && month == 2)
{
return 29;
}
return day[month];
}
int MonthOfOneDay(int year, int month) //计算当前月的第一天为星期几
{
if ((year > 1) && (month > 1 && month < 12))
{
int week = 1;//1.1.1 为周一
int day = 0;
Date d1(1, 1, 1);
Date d2(year, month, 1);
day = d2 - d1;
week = day % 7 + week;
if (week > 7)
{
week -= 7;
}
return week;
}
}
void PrintCalendar(int year, int month) //打印当前月的日历
{
cout << year << "年" << month << "月" << endl;
cout << "日 " << "一 " << "二 " << "三 " << "四 " << "五 " << "六 " << endl;
int week = MonthOfOneDay(year, month);
if (week != -1)
{
Date d(year, month, 1);
int day = 1;
int i = 0;
for (i = 0; i < week; i++)
{
cout << " ";
}
for (int j = 0; j < 6; j++)
{
for (; i < 7; i++)
{
if (day <= d.DayOfMonth(d._year, d._month))
{
printf("%2d ", day);
day++;
}
else
{
break;
}
}
i = 0;
cout << endl;
}
}
}
istream& operator>>(istream& is, Date& d)
{
is >> d._year >> d._month >> d._day;
return is;
}
ostream& operator<<(ostream& os, Date& d)
{
os << d._year << "-" << d._month << "-" << d._day;
return os;
}
Test1.cpp
#include<iostream>
#include "Date.h"
void fun1() //日期加减天数
{
Date date;
int day;
cout << "请输入日期" << endl;
cin >> date;
cout << "请输入加减的天数(减法输入负数即可)" << endl;
cin >> day;
cout << date + day << endl;
}
void fun2() //计算两个日期相差天数
{
Date d1;
Date d2;
cout << "请输入第一个日期" << endl;
cin >> d1;
cout << "请输入第二个日期" << endl;
cin >> d2;
cout << "相差天数为:" << d1 - d2 << endl;
}
void fun3()
{
int year = 0;
int month = 0;
cout << "请输入要打印的年份和月份" << endl;
cin >> year >> month;
PrintCalendar(year, month);
}
void Print()
{
cout << "*****************************" << endl;
cout << "***请选择您要使用的功能:" << endl;
cout << "*** 1. 日期加减天数" << endl;
cout << "*** 2. 计算两个日期相差天数" << endl;
cout << "*** 3. 打印某年某月的日历" << endl;
cout << "*** 0. 退出" << endl;
cout << "*****************************" << endl;
}
void Menu()
{
while (1)
{
Print();
int choose = 0;
cin >> choose;
switch (choose)
{
case 1:
fun1();
break;
case 2:
fun2();
break;
case 3:
fun3();
break;
case 0:
exit(EXIT_SUCCESS);
break;
default:
cout << "输入有误,请重新输入" << endl;
break;
}
}
}
int main()
{
Menu();
return 0;
}