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天)
  • 然后计算从今年第的一天到上一个月有多少天
c 复制代码
//判断闰年函数 
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

c 复制代码
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;
	
}

🚀完整代码

c 复制代码
#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;
	
}

感谢阅读哦 给个赞把~~😛

相关推荐
建群新人小猿2 分钟前
CRMEB-PRO系统定时任务扩展开发指南
android·java·开发语言·前端
程序员秘密基地9 分钟前
基于c#,asp.net webform, sql server数据库,在线档案管理系统
开发语言·sqlserver·asp.net·.net·源代码管理
知识分享小能手11 分钟前
JavaScript学习教程,从入门到精通,Ajax数据交换格式与跨域处理(26)
xml·开发语言·前端·javascript·学习·ajax·css3
5:0025 分钟前
【QT】编写第一个 QT 程序 & 对象树 & Qt 编程事项 & 内存泄露问题
开发语言·qt
长安城没有风33 分钟前
JAVA SE 反射,枚举与lambda表达式
java·开发语言
这个懒人37 分钟前
C++后端服务器常见开发框架
c++·后端·框架
橘颂TA2 小时前
【C++】数据结构 九种排序算法的实现
数据结构·c++·排序算法
大魔王(已黑化)2 小时前
LeetCode —— 572. 另一棵树的子树
c语言·数据结构·c++·算法·leetcode·职场和发展
byte轻骑兵2 小时前
【C++类和数据抽象】消息处理示例(2)
开发语言·c++
一个天蝎座 白勺 程序猿2 小时前
Python爬虫(11)Python数据存储实战:深入解析NoSQL数据库的核心应用与实战
开发语言·python·nosql