c++实现简单日历

这是一个使用C++编写的程序,它可以生成指定年份和月份的日历。程序首先定义了一个date类,包含了年、月等私有成员变量,并提供了计算每月天数、日期对应星期等功能。在main函数中,用户输入年份和月份,程序会输出相应的日历。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<iomanip>
using namespace std;
const int len =20;
class  date
{
private:
	int year;
	int month;
public:
	date(int y, int m) :year{ y }, month{ m }{
		this->DispCalendar(y, m);
	}
	int DayNumofMonth(int y, int m)
	{
		int monthmax=0;
		if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) monthmax = 31;
		if (m == 4 || m == 6 || m == 9 || m == 11) monthmax = 30;
		if (m == 2)
		{
			if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)//闰年 
				monthmax = 29;
			else//非闰年
				monthmax = 28;
		}
		return monthmax;
	}
	int Date(int y, int m, int d);
	void  DispCalendar(int y, int m)
	{
		//标题模块
		cout << "    公元" << y << "年     第" << m << "月日历" << endl;
		int a = 1;
		for (a = 0; a < 7; a++)
			cout << setw(5) << a;//setW用于设置空白位
		cout << endl << "=====================================" << endl;
		//日期模块
		int b = 0, week;
		week = this->Date(y, m, 1);
		for (b = 0; b < week; b++)
			cout << setw(5) << " ";//每月一号对应的周几前面要有缩进
		int c = 1, monthmax;
		monthmax = this->DayNumofMonth(y, m);
		while (c <= monthmax)
		{
			cout << setw(5) << c;
			c++;
			week++;
			if (week % 7 == 0)
				cout << endl;
		}
		cout << endl << "=====================================" << endl;
	}
	~date()
	{
		cout <<"Over!" << endl;
	}
};
int date::Date(int y, int m, int d)
{
	int week;
	if (m == 1 || m == 2) {
		y = y - 1;
		m = m + 12;
	}
	week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;
	return week;
}
int main()
{
	int y, m;
	cout << "请输入年份:";
	cin >> y;
	cout << "请输入月份:";
	cin >> m;
	cout << endl;
	date D(y, m);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值