C++初阶的应用-日期管理系统的设计与实现

发布于:2025-04-21 ⋅ 阅读:(14) ⋅ 点赞:(0)

这个代码我只有一部分需要讲,其他的会在类和对象下讲解!

#include<iostream>
using namespace std;
//定义Date类
class Date
{
public:
	//友元函数
	friend ostream& operator<<(ostream& out,const Date& d );
	friend istream& operator>>(istream& in, Date d);
	//成员函数的定义
	//判断日期是否合法
	bool panduan(int year, int month, int day)
	{
		if (month < 1 || month > 12 || day > GetDay(year, month) || day < 0)
		{
			return false;
		}
		return true;
	}
	//初始化列表
	Date(int year1, int month1, int day1)
		:year(year1)
		, month(month1)
		, day(day1)
	{
		//int a, b, c;
		//a = year;
		//b = month;
		//c = day;
		//while(panduan(a, b, c) == false)
		//{
		//	cout << "日期输入不合法,请重新输入!" << endl;
		//	cin >> a >> b >> c;
		//}
		//year = a;
		//month = b;
		//day = c;
	}
	//得到日期
	int GetDay(int year, int month)
	{
		int arr[13] = { -1,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 arr[2]+ 1;
		}
		return arr[month];
	}
	//重载运算符的声明
	Date& operator-=(int day1);
	Date operator-(int day1) const;
	Date& operator+=(int day1);
	Date operator+(int day1) 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;
	int operator-(const Date& d) const;
	bool operator!=(const Date& d) const;
	Date operator++(int);
	Date& operator++();
private:
	//成员变量的定义
	int year = 1 ;
	int month = 1;
	int day = 1;
};
//-=运算符的重载
Date& Date::operator-=(int day1)
{
	if (day1 < 0)
	{
		return *this += (-day1);
	}
	day -= day1;
	while (day <= 0)
	{
		//借位
		--month;
		if (month == 0)
		{
			--year;
			month = 12;
		}
		day += GetDay(year, month);
	}
	return *this;
}
//-运算符重载
Date Date::operator-(int day1) const
{
	Date tmp = *this;
	tmp -= day1;
	return tmp;
}
//后置++运算符重载
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
//另一个-运算符重载
int Date::operator-(const Date& d) const
{
     Date max = *this > d ? *this : d;
	 Date min = *this < d ? *this : d;
	//Date max = *this;
	//Date min = d;
	//if (*this < d)
	//{
	//	max = d;
	//	min = *this;
	//}
	int n = 0;
	//while (max != min)
	//{
	//	min += 1;
	//	++n;
	//}
	while (max != min)
	{
		++min;
		++n;
	}
	return n;
}
//+=运算符重载
Date& Date::operator+=(int day1)
{
	if (day1 < 0)
	{
		return *this -= (-day1);
	}
	day += day1;
	while (day > GetDay(year, month))
	{
		day -= GetDay(year, month);
		//月进位
		++month;
		if (month == 13)
		{
			++year;
			month = 1;
		}
	}
	return *this;
}
//+运算符重载
Date Date::operator+(int day1) const
{
	Date tmp = *this ;
	tmp += day1;
	return tmp;
}
//前置++运算符重载
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//==运算符重载
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 year != d.year
		|| month != d.month
		|| day != d.day;
}
//>运算符重载
bool Date::operator>(const Date& d) const
{
	if (year < d.year)
	{
		return false;
	}
	else if (year == d.year && month < d.month)
	{
		return false;
	}
	else if (year == d.year && month == d.month && day <= d.day)
	{
		return false;
	}
	else
	{
		return true;
	}
}
//>=运算符重载
bool Date::operator>=(const Date& d) const
{
	return *this > d || *this == d;
}
//<运算符重载 
bool Date::operator<(const Date& d) const
{
	return !(*this >= d);
	//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;
}
//<<运算符重载
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;
}
//打印菜单
void menu()
{
	int n;
	while(1)
	{
		cout << "*****************************************" << endl;
		cout << "***** 0 日期加天数运算 ******************" << endl;
		cout << "***** 1 日期减天数运算 ******************" << endl; 
		cout << "***** 2 日期减日期运算 ******************" << endl;
		cout << "***** 3 退出计算器     ******************" << endl;
		cout << "*****************************************" << endl;
		cout << "请选择你要进行的操作:>" << endl;
		cin >> n;
		if (n == 0)
		{
			int year, month, day, _day;
			cout << "请输入日期:>" << endl;
			cin >> year >> month >> day;
			Date d1(year, month, day);
			if(d1.panduan(year, month, day))
			{
				cout << "请输入你想加的天数:>" << endl;
				cin >> _day;
				Date d2 = d1 + _day;
				cout << "该日期过" << _day << "天后的日期为" << d2;
			}
			else
			{
				cout << "由于你输入的日期不合理,请重新选择操作!" << endl;
			}
		}
		else if (n == 1)
		{
			int year, month, day, _day;
			cout << "请输入日期:>" << endl;
			cin >> year >> month >> day;
			Date d1(year, month, day);
			if (d1.panduan(year, month, day))
			{
				cout << "请输入你想减的天数:>" << endl;
				cin >> _day;
				Date d2 = d1 - _day;
				//d1 -= _day;
				cout << "该日期前" << _day << "天的日期为" << d2;
				//cout << "该日期前" << _day << "天的日期为" << d1;
			}
			else
			{
				cout << "由于你输入的日期不合理,请重新选择操作!" << endl;
			}
		}
		else if (n == 2)
		{
			int year, month, day, year1, month1, day1;
			cout << "请输入日期:>" << endl;
			cin >> year >> month >> day;
			Date d1(year, month, day);
			if (d1.panduan(year, month, day))
			{
				cout << "请输入另一个日期:>" << endl;
				cin >> year1 >> month1 >> day1;
				Date d2(year1, month1, day1);
				if (d2.panduan(year1, month1, day1))
				{
					int _day = d1 - d2;
					cout << d1 << "与" << d2 << "相差的天数为" << _day << "天" << endl;
				}
			}
			else
			{
				cout << "由于你输入日期的不合理,请重新选择操作!" << endl;
			}
		}
		else if (n == 3)
		{
			return;
		}
		else
		{
			cout << "你输入的数字不规范,请重新输入!" << endl;
		}
	}
}
int main()
{
	//Date d1(2025, 5, 1);
	//Date d2(2025, 4, 18);
	//int _day = d1 - d2;
	cout << "欢迎进入日期管理系统" << endl;
	int n;
	while(1)
	{
		cout << "************************" << endl;
		cout << "***** 0 退出系统 *******" << endl;
		cout << "***** 1 进入系统 *******" << endl;
		cout << "************************" << endl;
		cout << "请选择你的操作:>" << endl;
		cin >> n;
		if (n == 0)
		{
			return 0;
		}
		else if (n == 1)
		{
			menu();
			break;
		}
		else
		{
			cout << "输入数字不规范,请重新输入一个数字!" << endl;
		}
	}
	return 0;
}

那些注释的代码有些是能跑通的,有些是会陷入死循环的,所以我就改进了一下,只有两个函数:<<和>>运算符重载有一个注意的点:

我们如果把<<和>>置为成员函数,那么就会写为d1<<cout不符合我们的使用规范,至于返回值是这个的原因,我现在也没弄懂,但是只要会实现就可以了,主要是看懂代码,其他的东西基本上会到类和对象下来进行讲解。所以我没有过多的讲了,千万不要取关了啊!


网站公告

今日签到

点亮在社区的每一天
去签到