万年历cpp代码

<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;
}

EasyX是一个基于C++的图形库,用于快速开发游戏和界面应用程序。要实现一个万年历的完整代码,我们需要考虑以下几个步骤: 1. 包含必要的头文件和库 2. 定义窗口和基本元素 3. 初始化EasyX和时间处理 4. 创建并显示万年历界面 5. 更新和绘制日历 6. 处理用户交互 由于这是一个复杂的编程任务,我将提供一个简化的概念代码示例,但请注意,完整的万年历项目可能需要更多的细节,如事件管理、样式定制等。 ```cpp #include "easyx.h" // EasyX库 #include <iostream> #include <ctime> class CalendarView : public Shape { public: CalendarView(int width, int height, const std::string& year, const std::string& month) : Shape(width, height), year(year), month(month) {} void draw() override { // 绘制日历的基本框架和月份标题 // ... (这部分代码取决于EasyX API) // 根据当前时间和月份显示日期 // ... (使用ctime和日期计算) // 绘制星期标题 // ... (同样使用ctime) // 更新和绘制每一天的日期 // ... (遍历一周,根据实际日期绘制) } private: std::string year; std::string month; }; int main() { initEasyX(); // 初始化EasyX setCaption("EasyX万年历"); // 设置窗口标题 CalendarView calendar(800, 600, "2023", "January"); // 假设为2023年1月 calendar.setPosition(100, 100); // 窗口位置 calendar.draw(); // 显示日历 while (!keyDown()) { // 游戏主循环 update(); // EasyX更新 drawScene(); // 渲染场景 } closeEasyX(); // 关闭EasyX return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值