C++实现简单日历(win11日历)

🚀实现目标

在这里插入图片描述
我们想要的效果:
1.布局类似
2.键盘按下←或者→会切换到下一个月(这里直接看原码就可以了)
3.可以实现自定义输入年份

🚀效果

按下→ 切换到下一个月
按下←切换到上一个月
按下Esc 退出
按下Tab 输入自定义年月
在这里插入图片描述

🚀计算上一个月的最后一天是周几

我们规定 公元1年1月1日是周一
所以我们只需要计算从1年1月1日到这一天一共有几天
比如 1年1月1日到1年1月6日一共有6天
那么 1年1月6日 就是周6 (6%7)

而计算从1年1月1日到上个月的最后一天 有几天

需要考虑:

  • 这一年之前有多少个年 (也就是前面有几个365天)
  • 然后 计算从1年到去年 有几个闰年 有几个闰年就+几(闰年366天)
  • 然后计算从今年第的一天到上一个月有多少天
//判断闰年函数 
bool IsLeapYear(int year)
{
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
	{
		return true;
	}
	return false;
}
//计算某一个月有有几天
int DaysOfMonth(int year, int month)
{
	int months[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (IsLeapYear(year))
		months[2]++;
	int days = months[month];
	
	return days;
}
//计算从1年1月1日到上个月的最后一天有多少天
int TotalDaysBefore(int year, int month)
{
	int total = 0;
	/*这一年之前的天数,(以平年计算)*/
	total += (year - 1) * 365;
	/*算出这一年之前的闰年个数 366天*/
	int leapyear = (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
	total += leapyear;
	/*还有 这一年*/
	for(int i = 1; i < month; i++)
	{
		total += DaysOfMonth(year, i);
	}
	return total;
}

🚀打印日历函数

这里我们主要考虑的是
首先需要根据上个月的最后一天是周几来判断 这个月的第一天从哪里开始打印
如果上个月的最后一天是周六 那么这个月的第一天就从周天开始
而周天是第一个位置 所以前面不需要空格
如果上个月的最后一天是周一,那么这个月的第一天需要在周二开始打印
也就是前面有两个空格
我们利用一个数num来标识 上个月的最后一天是周几,num是由上个月的最后一天与1年1月1日之间的天数 %7 得到的,范围是0-6
周天num=0–空1格
周一num=1–空两格…
周六num=6–不空格
所以 如果num != 6 那么空格数是num+1

void PrintCalendar(int datys, int year, int month)
{
	cout << "Su" << '\t' << "Mo" << '\t' << "Tu" << '\t' << "We" << '\t' << "Th" << '\t' << "Fr" << '\t' << "Sa"<< endl;
	cout << endl;
	/*根据这个年月 算出之前有多少天*/
	int daysbefore = TotalDaysBefore(year, month);
	/*公元1年1月1日是周1*/
	int num = daysbefore % 7;
	/*num是上一个月的最后一天是星期几*/
	if (num != 6)
	{
		for (int i = 0; i < num+1; i++)
			cout << ' '<<'\t';
	}
	/*接下来用num控制输出的位置,num也就是该天是周几*/
	num++;
	for (int i = 1; i <= DaysOfMonth(year,month); i++)
	{
		/*输出今天是几号*/
		cout << i << '\t';
		/*判断下一个位置是不是周天 如果是周天 那么换行*/
		if ((num+1) % 7 == 0)
		{
			cout << endl;
			cout << endl;

		}

		num++;
	}
	cout << endl;
	
}

🚀完整代码

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:6031)

#include<iostream>
#include<conio.h>
using namespace std;

/*控制输出日历,参数分别是*/
void PrintCalendar(int daysBefore, int year, int month);
/*找出某一月份的天数*/
int DaysOfMonth(int year, int month);
/*找出某年某月 的上一个月与1年1月1日差多少天*/
int TotalDaysBefore(int year, int month);
int main()
{
	/*获取当前日期 自动显示日历*/
	time_t data;
	struct tm* p;
	time(&data);
	p = localtime(&data);
	const char* eng_month[13] = { 0,"January","Feburary","March","April","May","June","July","August","September","October","November","December" };
	int input = 0;
	int year = 1900+p->tm_year;
	int month = 1+p->tm_mon;
	int ch = 0;
	do
	{
		/*每一次打印把上一次打印的给清除*/
		system("cls");
		
		cout << eng_month[month] << ' ' << year << '\t'<<'\t'<<'<' <<'\t' << '>' << endl;
		cout << endl;

		PrintCalendar(TotalDaysBefore(year, month), year, month);
		/*用户选择 然后循环上去进行下一次打印 */
		cout << endl;
		cout << '\t'<<'\t'<<'\t'<<'\t' << '\t' << '\t' <<'\t' <<'\t' << "<-custom: tab->" << endl;
		cout << '\t'<<'\t'<<'\t'<<'\t' << '\t' << '\t' <<'\t' <<'\t' << "<-exit:   Esc->" << endl;
		cout << endl;
		cout << "Option::" << endl;
		//cin >> input;

		/* 72:上 
		   80:下
		   77:左、
		   75:右*/
		while (1)
		{
			if (_kbhit())
			{
				ch = _getch();
				if (ch == 75)
				{
					
					month -= 1;
					if (month <= 0)
					{
						month += 12;
						--year;
					}
					break;
				}
				else if (ch == 77)
				{
					

					month += 1;
					if (month >= 13)
					{
						month = month % 12;
						++year;
					}
					break;
				}
				else if (ch == 9)
				{
			
					cout << "year: ";
					cin >> year;
					cout << "month: ";
					cin >> month;

					break;
				}
				else if (ch == 27)
				{
					cout << endl;
					cout << '\t' << "thanks for using!" << endl;
					cout <<'\t' << "bye~" << endl;
					break;
					
				}
				else
				{
					
					break;
				}
			}

		}
		
	} while (ch!=27);

	return 0;
}

//判断闰年
bool IsLeapYear(int year)
{
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
	{
		return true;
	}
	return false;
}
//计算某个月有多少天
int DaysOfMonth(int year, int month)
{
	int months[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (IsLeapYear(year))
		months[2]++;
	int days = months[month];
	
	return days;
}
//计算从1年1月1日 到上一个月的最后一天有多少天
int TotalDaysBefore(int year, int month)
{
	int total = 0;
	/*这一年之前的天数,(以平年计算)*/
	total += (year - 1) * 365;
	/*算出这一年之前的闰年个数 366天*/
	int leapyear = (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
	total += leapyear;
	/*还有 这一年*/
	for(int i = 1; i < month; i++)
	{
		total += DaysOfMonth(year, i);
	}
	return total;
}
//打印日历
void PrintCalendar(int datys, int year, int month)
{
	cout << "Su" << '\t' << "Mo" << '\t' << "Tu" << '\t' << "We" << '\t' << "Th" << '\t' << "Fr" << '\t' << "Sa"<< endl;
	cout << endl;
	/*根据这个年月 算出之前有多少天*/
	int daysbefore = TotalDaysBefore(year, month);
	/*公元1年1月1日是周1*/
	int num = daysbefore % 7;
	/*num是上一个月的最后一天是星期几*/
	if (num != 6)
	{
		for (int i = 0; i < num+1; i++)
			cout << ' '<<'\t';
	}
	/*接下来用num控制输出的位置,num也就是该天是周几*/
	num++;
	for (int i = 1; i <= DaysOfMonth(year,month); i++)
	{
		/*输出今天是几号*/
		cout << i << '\t';
		/*判断下一个位置是不是周天 如果是周天 那么换行*/
		if ((num+1) % 7 == 0)
		{
			cout << endl;
			cout << endl;

		}

		num++;
	}
	cout << endl;
	
}

在这里插入图片描述
     感谢阅读哦 给个赞把~~😛

在这里插入图片描述

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值