栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实现Win11万年历

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实现Win11万年历

✨ 写在前面

 哈喽大家好
 作为一个初入编程的大学生,知识浅薄
写文章的同时也是在巩固自己,同时希望我的文章对你有所帮助!

 我的其他文章
       1.【数据结构】时间复杂度&&空间复杂度
       2.【数据结构】顺序表接口实现及详解
       2.【数据结构】带你手撕单链表!

初入编程的世界 前方"路漫漫"️ 每天我们都要进步一点点
 希望分享知识的同时可以和你们一起进步

文章目录
  • 实现目标
  • 效果
  • 计算上一个月的最后一天是周几
  • 打印日历函数
  • 完整代码

实现目标


我们想要的效果:
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;
	
	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);
	
	int num = daysbefore % 7;
	
	if (num != 6)
	{
		for (int i = 0; i < num+1; i++)
			cout << ' '<<'t';
	}
	
	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
#include
using namespace std;


void PrintCalendar(int daysBefore, int year, int month);

int DaysOfMonth(int year, int month);

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;

		
		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;
	
	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);
	
	int num = daysbefore % 7;
	
	if (num != 6)
	{
		for (int i = 0; i < num+1; i++)
			cout << ' '<<'t';
	}
	
	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;
	
}


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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/893383.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号