打印任意年份/月份日历(C++)

要求:

写一个程序可用实现打印任意年份/月份的日历,年历三月一层,分四层打印。

实现代码:

提示:本代码不同编译器下缩进显示可能有略微差别,自行调整。

/*************************************************************************
	> File Name: calendar.cpp
	> Author: 念念
	> Mail: 2845906049@qq.com 
	> Created Time: 2021年08月17日 星期二 15时27分03秒
    > Function: 根据输入的年份打印该年日历
 ************************************************************************/
 
#include <iostream>
#include <iomanip>
using namespace std;

class Calendar
{
public:
    
    void setYear(int y);
	void setMonth(int m);
	int dayOfYear(int year);
	int dayOfMonth(int month);
	int getWeek(int year,int month);
	void showMonth();
	void showYear();
	void showYearr();
private:
    int year = 1990; 
	int month = 1;
};

void Calendar::setYear(int y)
{
	year = y;
}
void Calendar::setMonth(int m)
{
	month = m;
}

int Calendar::dayOfYear(int year)
{
	if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
	{
		return 366; //闰年366天
	}
	else
	{
		return 365;
	}
}

int Calendar::dayOfMonth(int month)
{
	if(month == 4 || month == 6 || month == 9 || month == 11)
	{
        return 30;
	}
	else if(month == 2 && dayOfYear(year) == 366)
	{
        return 29;
	}
	else if(month == 2 && dayOfYear(year) == 365)
	{
		return 28;
	}
	return 31;
}

int Calendar::getWeek(int year,int month)
{
	//1990-01-01是周一,以此为标尺
	int week = 1;
	if(year >= 1990)
	{
		for(int i = 1990; i < year; i++)
		{
			week += dayOfYear(i);
		}
		for(int i = 1; i < month; i++)
		{
			week += dayOfMonth(i);
		}
		week %= 7; 
	}
	return week+1;//该月第一天的星期,0是周末
}

void Calendar::showMonth()
{
	cout << "\033[035m\033[1m";
	cout << year << "年" << month << "月\033[0m\n";
	cout << "  日  一   二  三  四  五  六\n";
	for(int i = 1; i < getWeek(year,month); i++)
	{
		cout << setw(4) << "";
	}
	for(int i = 1,j= getWeek(year,month); i<= dayOfMonth(month); i++,j++)
	{
		cout << setw(4) << i;
		if(i % 7 == 0){cout << endl;}
	}
}

void Calendar::showYear()
{
	//分四层打印,每层左中右三个月。
	int left_month = 1,middle_month = 2,right_month = 3;//月份
	int left_day= 1,middle_day= 1,right_day = 1;//日期
	int week;
	cout << "\033[035m\033[1m";
	cout << year << "年\n";
	for(;left_month < 12;left_month+=3,middle_month+=3,right_month+=3)
	{
    //打印月历头
		cout << "\033[34m\033[1m  " << left_month <<"月";
		cout << setw(36) << middle_month <<"月";
		cout << setw(36) << right_month <<"月\033[0m\n";
		for(int i = 0; i < 3; i++)
		{
			cout << setw(45) << " 日  一   二  三  四  五  六         ";
		}
		cout << endl;
	//打印第一行
		for(int i= 1; i < getWeek(year,left_month); i++)
		{
			cout << setw(4) << "";
		}
		for(week = getWeek(year,left_month)-1;week < 7; left_day++,week++)
		{
			week %= 7;	
			cout << setw(4) << left_day;
		}
		cout << setw(10) << "";
		for(int i = 1; i < getWeek(year,middle_month); i++)
		{
			cout << setw(4) << "";
		}
		for(week = getWeek(year,middle_month)-1;week < 7; middle_day++,week++)
		{
			week %= 7;
			cout << setw(4) << middle_day;
		}
		cout << setw(10) << "";
		for(int i = 1; i < getWeek(year,right_month); i++)
		{
			cout << setw(4) << "";
		}
		for(week = getWeek(year,right_month)-1;week <7; right_day++,week++)
		{
			week %= 7;
			cout << setw(4) <<right_day;
		}
		cout << endl;
	//打印后续行
		for(int t = 0; t < 5; t++)
		{
			for(week = 0; week < 7; left_day++,week++)
			{ 
				week %= 7;	
				left_day<= dayOfMonth(left_month) ? cout << setw(4) << left_day: cout << setw(4) <<"";
			}
			cout << setw(10) << "";
			for(week = 0; week < 7; middle_day++,week++)
			{
				week %= 7;
				middle_day<= dayOfMonth(middle_month) ? cout << setw(4) << middle_day: cout << setw(4) <<"";
			}
			cout << setw(10) << "";
			for(week = 0; week < 7 && right_day <= dayOfMonth(right_month);right_day++,week++)
			{
				week %= 7;
				cout << setw(4) << right_day;
			}
			cout << endl;	
		}
		left_day= 1;middle_day= 1;right_day = 1;
	}
}


int main()
{
	Calendar c;
	c.setYear(2021);
	c.setMonth(9);

	c.showYear();//展现设置年日历
	//c.showMonth();//展现设置年设置月日历
	return 0;
}

运行结果:

Ubuntu20.04打印效果

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

念念⁡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值