日历---C++封装一个Date类,Calendar类,实现简单的日历+日期计算器程序

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kevin980123 https://blog.csdn.net/kevin980123/article/details/84433252

C++封装一个Date类,实现简单的日历程序


程序代码如下:


Date.h


#include<iostream>
using namespace std;

class Date
{
public:

	Date(int year = 2000, int month = 1, int day = 1);
	Date(const Date& d);
	//赋值运算符重载
	Date& operator=(const Date& d);
	//前置++
	Date& operator++();
	//后置++
	Date operator++(int);
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);
	//计算日期是那一年的第几天
	int DaysInYear(int year, int month, int day);
	//计算当前日期向后n天的日期
	Date Date::operator+(int days);
	//计算当前日期向前n天的日期
	Date Date::operator-(int days);
	//计算两个日期相差天数
	int operator-(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;
	bool operator<(const Date& d)const;
	bool operator<=(const Date& d)const;

private:
	//交换函数
	void Swap(int& left, int& right);
	//判断是否为闰年
	bool IsLeap(int year);
	//获取当月天数
	int GetDaysOfMonth(int year, int month);
	//判断日期是否合法
	bool IsDateValid(int year, int month, int day);

private:
	int _year;
	int _month;
	int _day;
	friend ostream& operator<<(ostream& _cout, const Date& d);
	friend istream& operator>>(istream& _cin, Date& d);

};

class Calendar
{
public:
	Calendar(int year = 2000);

	bool IsLeap(int year);
	int GetDaysOfMonth(int year, int month);
	void PrintCalendar();
	void PrintTitle(int m);
private:
	int _year;
	int _weekday;
};

Date.c

#define _CRT_SECURE_NO_WARNINGS

#include "Date.h"

//日期类
Date::Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{
	if (!IsDateValid())
	{
		cout << "日期非法!使用默认值2000.1.1" << endl;
		_year = 2000;
		_month = 1;
		_day = 1;
	}
}

Date::Date(const Date& d)
: _year(d._year)
, _month(d._month)
, _day(d._day)
{}

//赋值运算符重载
Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

//交换函数
void Date::Swap(int& left, int& right)
{
	int tmp = left;
	left = right;
	right = tmp;
}

//判断是否为闰年
bool Date::IsLeap(int year)
{
	if (((0 == year % 4) && (0 != year % 100)) ||
		(0 == year % 400))
		return true;
	else
		return false;
}

//获取当月天数
int Date::GetDaysOfMonth(int year,int month)
{
	//方法一
	int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	if (month == 2 && IsLeap(year))
		return days[month] + 1;
	else
		return days[month];
	//方法二
	/*switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		return 31;
	case 4:
	case 6:
	case 9:
	case 11:
		return 30;
	case 2:
		if (IsLeap(year))
			return 29;
		else
			return 28;
	}*/
}

//判断日期是否合法
bool Date::IsDateValid()
{
	if (_year <1 || (_month < 1 || _month > 12) ||
		(_day<1 || _day>GetDaysOfMonth(_year, _month)))
		return false;
	else
		return true;
}

//前置++
Date& Date::operator++()
{
	if (_day < GetDaysOfMonth(_year,_month))
		++_day;
	else
	{
		if (_month < 12)
		{
			++_month;
			_day = 1;
		}
		else
		{
			++_year;
			_month = 1;
			_day = 1;
		}
	}
	return *this;
}

//后置++
Date Date::operator++(int)
{
	Date tmp(*this);
	if (_day < GetDaysOfMonth(_year, _month))
		++_day;
	else
	{
		if (_month < 12)
		{
			++_month;
			_day = 1;
		}
		else
		{
			++_year;
			_month = 1;
			_day = 1;
		}
	}
	return tmp;
}

//前置--
Date& Date::operator--()
{
	if (_day > 1)
		--_day;
	else
	{
		if (_month > 1)
		{
			--_month;
			_day = GetDaysOfMonth(_year, _month);
		}
		else
		{
			--_year;
			_month = 12;
			_day = 31;
		}
	}
	return *this;
}
//后置--
Date Date::operator--(int)
{
	Date tmp = *this;
	if (_day > 1)
		--_day;
	else
	{
		if (_month > 1)
		{
			--_month;
			_day = GetDaysOfMonth(_year, _month);
		}
		else
		{
			--_year;
			_month = 12;
			_day = 31;
		}
	}
	return tmp;
}

//计算日期是那一年的第几天
int Date::DaysInYear(int year, int month, int day)
{
	int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (IsLeap(year))
		days[2] = 29;
	for (int i = 1; i < month; ++i)
	{
		day += days[i];
	}
	return day;
}

//计算当前日期向后n天的日期
Date Date::operator+(int days)
{
	if (days<0)
		return *this - (0 - days);

	Date tmp(*this);
	tmp._day += days;
	int DaysOfMonth = 0;

	while (tmp._day > (DaysOfMonth = GetDaysOfMonth(tmp._year, tmp._month)))
	{
		tmp._day -= DaysOfMonth;
		++tmp._month;

		if (tmp._month > 12)
		{
			++tmp._year;
			tmp._month = 1;
		}
	}
	return tmp;
}

//计算当前日期向前n天的日期
Date Date::operator-(int days)
{
	if (days<0)
		return *this + (0 - days);

	Date tmp(*this);
	tmp._day -= days;
	while (tmp._day <= 0)
	{
		--tmp._month;
		if (tmp._month < 1)
		{
			--tmp._year;
			tmp._month = 12;
		}
		tmp._day += GetDaysOfMonth(tmp._year, tmp._month);
	}
	return tmp;
}

//计算两个日期相差天数
int Date::operator-(Date& d)
{
	//同年同月
	if (_year == d._year && _month == d._month)
		return _day > d._day ? (_day - d._day) : (d._day - _day);
	//同年
	else if (_year == d._year)
	{
		int d1, d2;
		d1 = DaysInYear(_year, _month, _day);
		d2 = DaysInYear(d._year, d._month, d._day);
		return d1 > d2 ? (d1 - d2) : (d2 - d1);
	}
	//年、月都不相同
	else
	{
		int flag = 0;
		//确保_year日期比d._year大
		if (_year < d._year)
		{
			Swap(_year, d._year);
			Swap(_month, d._month);
			Swap(_day, d._day);
			flag = 1;
		}

		int d1, d2, d3 = 0;
		//1.求小的日期在该年剩余天数
		if (IsLeap(d._year))
			d1 = 366 - DaysInYear(d._year, d._month, d._day);
		else
			d1 = 365 - DaysInYear(d._year, d._month, d._day);
		//2.求大的日期是该年的第几天
		d2 = DaysInYear(_year, _month, _day);
		//3.求两个日期中间相差几个整年
		for (int i = d._year+1; i < _year; ++i)
		{
			if (IsLeap(i))
				d3 += 366;
			else
				d3 += 365;
		}

		if (1 == flag)
		{
			Swap(_year, d._year);
			Swap(_month, d._month);
			Swap(_day, d._day);
		}

		return d1 + d2 + d3;
	}

}


//判断两个日期是否相等
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 ||
		_year == d._year && _month > d._month ||
		_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) || (*this == d);
}

//输入运算符重载
istream& operator>>(istream& _cin, Date& d)
{
	_cin >> d._year >> d._month >> d._day;
	while (!d.IsDateValid())
	{
		cout << "日期非法!请重新输入:";
		_cin >> d._year >> d._month >> d._day;
	}
	return _cin;
}

//输出运算符重载
ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day;
	return _cout;
}

//日历类
#include <iomanip>
Calendar::Calendar(int year)
:_year(year)
{
	if (_year < 1)
	{
		cout << "年份非法!使用默认值2000" << endl;
		_year = 2000;
	}
}

bool Calendar::IsLeap(int year)
{
	if (((0 == year % 4) && (0 != year % 100)) ||
		(0 == year % 400))
		return true;
	else
		return false;
}

int Calendar::GetDaysOfMonth(int year, int month)
{
	//方法一
	int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	if (month == 2 && IsLeap(year))
		return days[month] + 1;
	else
		return days[month];
}

void Calendar::PrintCalendar()
{
	for (int n = 1; n < 13; n++)
	{
		PrintTitle(n);
		//基姆拉尔森计算公式---求某年某月一号对应的星期
		if (1 == n || 2 == n)
		{
			_weekday = (1 + 2 * (n + 12) + 3 * (n + 12 + 1) / 5 + _year - 1 +
				(_year - 1) / 4 - (_year - 1) / 100 + (_year - 1) / 400 + 1) % 7;
		}
		else
		{
			_weekday = (1 + 2 * n + 3 * (n + 1) / 5 + _year +
				_year / 4 - _year / 100 + _year / 400 + 1) % 7;
		}
		for (int i = 0; i < _weekday; i++)
		{
			cout << setw(4) << " ";//每月一号和对应的星期对齐
		}
		int days = GetDaysOfMonth(_year, n);
		for (int j = 1; j <= days; j++)
		{
			cout << setw(4) << j;
			_weekday = (_weekday + 1) % 7;
			if (0 == _weekday)
				cout << endl;
		}
		cout << endl/* << "____________________________" << endl*/;
	}

}

void Calendar::PrintTitle(int m)
{
	cout << endl;
	cout << "             " << m << "月             " << endl;
	cout << "-----------------------------" << endl;
	cout << "  日  一  二  三  四  五  六  " << endl;
	cout << "-----------------------------" << endl;

}

test.c

#define _CRT_SECURE_NO_WARNINGS

#include "Date.h"

//测试打印全年日历到桌面
void TestFunc()
{
	int year;
	cout << "请输入你要查看的年份:";
	cin >> year;
	cout << year << "年全年日历如下:" << endl;
	Calendar C(year);
	C.PrintCalendar();
}

void Menu1();
void Menu2();

void Menu0(int flag)
{
	int i;
	cout << endl;
	cout << "************************************" << endl;
	cout << "            1.返回上一页            " << endl;
	cout << "            0.退出                  " << endl;
	cout << "************************************" << endl;
	cout << "请选择:";
	cin >> i;
	switch (i)
	{
	case 0:
		cout << "退出成功,祝您生活愉快!" << endl;
		return;
	case 1:
		if (1 == flag)
			Menu1();
		else
			Menu2();
	}
}

void Menu2()
{
	int i;
	int flag = 2;
	cout << endl;
	cout << "********************************" << endl;
	cout << "      1.日期之间的间隔时间      " << endl;
	cout << "      2.增加天数                " << endl;
	cout << "      3.减去天数                " << endl;
	cout << "      4.返回上一页              " << endl;
	cout << "      0.退出                    " << endl;
	cout << "********************************" << endl;
	cout << "请选择:";
	cin >> i;
	switch (i)
	{
	case 0:
		cout << "退出成功,祝您生活愉快!" << endl;
		return;
	case 1:
	{
			  Date d1;
			  Date d2;
			  cout << "请输入第一个日期:";
			  cin >> d1;
			  cout << "请输入第二个日期:";
			  cin >> d2;
			  cout << d1 << "与" << d2 << "间隔" << d1 - d2 << "天" << endl;
			  Menu0(flag);
			  break;
	}
	case 2:
	{
			  Date d1;
			  Date d2;
			  int days;
			  cout << "请输入日期:";
			  cin >> d1;
			  cout << "请输入要增加的天数:";
			  cin >> days;
			  d2 = d1 + days;
			  cout << d1 << "的100天后的日期:" << d2 << endl;
			  Menu0(flag);
			  break;
	}
	case 3:
	{
			  Date d1;
			  Date d2;
			  int days;
			  cout << "请输入日期:";
			  cin >> d1;
			  cout << "请输入要减去的天数:";
			  cin >> days;
			  d2 = d1 - days;
			  cout << d1 << "的100天前的日期:" << d2 << endl;
			  Menu0(flag);
			  break;
	}
	case 4:
		Menu1();
		break;
	}
}

void Menu1()
{
	int i;
	int flag = 1;
	cout << endl;
	cout << "欢迎使用每天日历,记录每天的美好生活:)" << endl;
	cout << "************************************" << endl;
	cout << "   1.查看全年日历      2.日期计算   " << endl;
	cout << "   0.退出                           " << endl;
	cout << "************************************" << endl;
	cout << "请选择:";
	cin >> i;
	switch (i)
	{
	case 0:
		cout << "退出成功,祝您生活愉快!" << endl;
		return;
	case 1:
		TestFunc4();
		Menu0(flag);
		break;
	case 2:
		Menu2();
	}
}


int main()
{
	//TestFunc();
	Menu1();
	return 0;
}

程序运行结果如下:


看截图不太明白的话,可以拷代码到自己的编译器运行查看

1.查看全年日历(只展示部分)
在这里插入图片描述

2.两个日期间的间隔天数
在这里插入图片描述

3.增加天数
在这里插入图片描述

4.减去天数
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/kevin980123/article/details/84433252