#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);
}
c++实现简单日历
最新推荐文章于 2024-07-18 18:39:15 发布
这是一个使用C++编写的程序,它可以生成指定年份和月份的日历。程序首先定义了一个date类,包含了年、月等私有成员变量,并提供了计算每月天数、日期对应星期等功能。在main函数中,用户输入年份和月份,程序会输出相应的日历。
摘要由CSDN通过智能技术生成