c语言实现万年历(公元1年以后),包括查询具体年份,查询某年某月

目录

1.在test.c文件中设置全局变量和menu()函数,进行查询选项

2.在calenda.h头文件中声明函数

3.在game.c文件中编写Calendar函数查询年份

3.1编写判断闰年的is_leap函数

3.2编写计算查询年份和公元1年之间天数的days函数

3.3编写打印查询年份12个月日期的display函数

4.在game.c文件中编写查询年月的M_calendar函数

4.1编写查询某年某月距离公元1年的m_days函数

4.2编写打印查询的某年某月的show函数

5.完整代码

5.1 calendar.h

5.2 calendar.c

5.3 test.c


先上图

 c语言实现万年历(公元1年以后),包括查询具体年份,查询某年某月

思路:

1.创建calendar.h文件,声明函数

2.创建calendar.c文件,编写查询函数(先编写查询年份的函数,再编写查询年月的函数)

3.创建test.c文件,运行主函数

步骤:

1.在test.c文件中设置全局变量和menu()函数,进行查询选项

#include "calendar.h"//两个源文件都要引用头文件

void menu()
{
	printf("  ***********************************************\n");
	printf("  ****************** 1.查询某年     *************\n");
	printf("  ****************** 2.查询某年某月 *************\n");
	printf("  ****************** 0.退出查询     *************\n");
	printf("  ***********************************************\n");
}

int main()
{
	int input;
	int year, mon;
	do
	{
		menu();
		printf("\n请选择:>");
		scanf("%d", &input);
		system("cls");
		switch (input)
		{
		case 1:
			printf("请输入需要查询的年份:>");
			while (~scanf("%d", &year))
			{
				system("cls");
				if (year >= 1)
				{
					//Calendar(year);//查询年份的函数
					printf("请输入需要查询的年份:>");
				}
				else
				{
					printf("查询超出范围,请重新输入:>");
				}
			}
			break;
		case 2:
			printf("请输入需要查询的年月:>");
			while(~scanf("%4d%2d", &year, &mon))
			{
				if (year >= 1)
				{
					//M_calendar(year, mon);//查询年月的函数
					printf("\n请输入需要查询的年月:>");
				}
				else
				{
					printf("查询超出范围,请重新输入:>");
				}
			}
			break;
		case 0:
			break;
		default:
			printf("输入错误,请重新选择:>");
			break;
		}
	} while (input);
	return 0;
}

2.在calenda.h头文件中声明函数

#include <stdio.h>
#include <stdlib.h>

int is_leap(int year);//判断闰年

long days(int year);//计算某年与公元1年之间天数

long m_days(int year, int mon, int leap[], int common[]);//计算某年某月与公元1年之间天数

void display(int year, long sum, int leap[], int common[], char month[12][8]);//查询年份打印

void show(int year, int mon, long sum, int leap[], int common[], char month[12][8]);//查询年月打印

3.在game.c文件中编写Calendar函数查询年份

3.1编写判断闰年的is_leap函数

int is_leap(int year)
{
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
	{
		return 1;
	}
	return 0;
}

3.2编写计算查询年份和公元1年之间天数的days函数

long days(int year)
{
	int i;
	long sum = 0;
	for (i = 1; i < year; i++)
	{
		if (is_leap(i) == 1)
		{
			sum += 366;
		}
		else
		{
			sum += 365;
		}
	}
	return sum;
}

3.3编写打印查询年份12个月日期的display函数

void display(int year, long sum, int leap[], int common[], char month[12][8])
{
	int ret = sum % 7;
	int temp = 7 - ret;
	int i;
	int mon = 0;
	printf("                万年历——%d年\n",year);
	printf("  ***********************************************\n");
	do
	{
		printf("                     %6s\n", month[mon]);
		printf("--------------------------------------------------\n");
		char week[7][5] = { "周一","周二","周三","周四","周五","周六","周日" };
		for (i = 0; i < 7; i++)
		{
			printf("|%5s ", week[i]);
		}
		printf("|\n");
		for (i = 0; i < 7; i++)
		{
			printf("|------");//打印出'|'和'------'符号是为了美观,可省略
		}
		printf("|\n");
		for (i = 0; i < ret; i++)
		{
			printf("|      ");
		}
		if (is_leap(year) == 1)
		{
			for (i = 1; i <= leap[mon]; i++)
			{
				printf("|%4d  ", i);
				if (i == temp || (i - temp) % 7 == 0)
				{
					printf("|\n");
					int j;
					for (j = 0; j < 7; j++)
					{
						printf("|------");
					}
					printf("|\n");
				}
			}
			ret = (leap[mon] % 7 + ret) % 7;
			temp = 7 - ret;
			if ((ret + leap[mon]) % 7 != 0)
			{
				printf("|\n");
			}
			for (i = 0; i < ret; i++)
			{
				printf("|------");
			}
			if (ret != 0)
			{
				printf("|\n");
			}
		}
		else
		{
			for (i = 1; i <= common[mon]; i++)
			{
				printf("|%4d  ", i);
				if (i == temp || (i - temp) % 7 == 0)
				{
					printf("|\n");
					int j;
					for (j = 0; j < 7; j++)
					{
						printf("|------");
					}
					printf("|\n");
				}
			}
			if ((ret + common[mon]) % 7 != 0)
			{
				printf("|\n");
			}
			ret = (common[mon] % 7 + ret) % 7;
			temp = 7 - ret;
			for (i = 0; i < ret; i++)
			{
				printf("|------");
			}
			if (ret != 0)
			{
				printf("|\n");
			}
		}
		printf("\n");
		mon++;
	} while (mon < 12);
	printf("  ***********************************************\n");
	printf("                万年历——%d年\n", year);
}

4.在game.c文件中编写查询年月的M_calendar函数

4.1编写查询某年某月距离公元1年之间天数的m_days函数

long m_days(int year, int mon, int leap[], int common[])
{
	long sum = days(year);
	int i;
	if (is_leap(year) == 1)
	{
		for (i = 0; i < mon - 1; i++)
		{
			sum += leap[i];
		}
	}
	else
	{
		for (i = 0; i < mon - 1; i++)
		{
			sum += common[i];
		}
	}
	return sum;
}

4.2编写打印查询的某年某月的show函数

void show(int year, int mon, long sum, int leap[], int common[], char month[12][8])
{
	int ret = sum % 7;
	int temp = 7 - ret;
	int i;
	printf("\n                     %6s\n", month[mon - 1]);
	printf("--------------------------------------------------\n");
	char week[7][5] = { "周一","周二","周三","周四","周五","周六","周日" };
	for (i = 0; i < 7; i++)
	{
		printf("|%5s ", week[i]);
	}
	printf("|\n");
	for (i = 0; i < 7; i++)
	{
		printf("|------");
	}
	printf("|\n");
	for (i = 0; i < ret; i++)
	{
		printf("|      ");
	}
	if (is_leap(year) == 1)
	{
		for (i = 1; i <= leap[mon - 1]; i++)
		{
			printf("|%4d  ", i);
			if (i == temp || (i - temp) % 7 == 0)
			{
				printf("|\n");
				int j;
				for (j = 0; j < 7; j++)
				{
					printf("|------");
				}
				printf("|\n");
			}
		}
		if ((ret + leap[mon - 1]) % 7 != 0)
		{
			printf("|\n");
		}
		ret = ((leap[mon - 1]) % 7 + ret) % 7;
		for (i = 0; i < ret; i++)
		{
			printf("|------");
		}
		if (ret != 0)
		{
			printf("|\n");
		}
	}
	else
	{
		for (i = 1; i <= common[mon - 1]; i++)
		{
			printf("|%4d  ", i);
			if (i == temp || (i - temp) % 7 == 0)
			{
				printf("|\n");
				int j;
				for (j = 0; j < 7; j++)
				{
					printf("|------");
				}
				printf("|\n");
			}
		}
		if ((ret + common[mon - 1]) % 7 != 0)
		{
			printf("|\n");
		}
		ret = ((common[mon - 1]) % 7 + ret) % 7;
		for (i = 0; i < ret; i++)
		{
			printf("|------");
		}
		if (ret != 0)
		{
			printf("|\n");
		}
	}
}

5.完整代码

5.1 calendar.h

#include <stdio.h>
#include <stdlib.h>

int is_leap(int year);//判断闰年

long days(int year);//计算某年与公元1年之间天数

long m_days(int year, int mon, int leap[], int common[]);//计算某年某月与公元1年之间天数

void display(int year, long sum, int leap[], int common[], char month[12][8]);//查询年份打印

void show(int year, int mon, long sum, int leap[], int common[], char month[12][8]);//查询年月打印

5.2 calendar.c

#include "Calendar.h"

int is_leap(int year)
{
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
	{
		return 1;
	}
	return 0;
}

long days(int year)
{
	int i;
	long sum = 0;
	for (i = 1; i < year; i++)
	{
		if (is_leap(i) == 1)
		{
			sum += 366;
		}
		else
		{
			sum += 365;
		}
	}
	return sum;
}

long m_days(int year, int mon, int leap[], int common[])
{
	long sum = days(year);
	int i;
	if (is_leap(year) == 1)
	{
		for (i = 0; i < mon - 1; i++)
		{
			sum += leap[i];
		}
	}
	else
	{
		for (i = 0; i < mon - 1; i++)
		{
			sum += common[i];
		}
	}
	return sum;
}

void display(int year, long sum, int leap[], int common[], char month[12][8])
{
	int ret = sum % 7;
	int temp = 7 - ret;
	int i;
	int mon = 0;
	printf("                万年历——%d年\n",year);
	printf("  ***********************************************\n");
	do
	{
		printf("                     %6s\n", month[mon]);
		printf("--------------------------------------------------\n");
		char week[7][5] = { "周一","周二","周三","周四","周五","周六","周日" };
		for (i = 0; i < 7; i++)
		{
			printf("|%5s ", week[i]);
		}
		printf("|\n");
		for (i = 0; i < 7; i++)
		{
			printf("|------");
		}
		printf("|\n");
		for (i = 0; i < ret; i++)
		{
			printf("|      ");
		}
		if (is_leap(year) == 1)
		{
			for (i = 1; i <= leap[mon]; i++)
			{
				printf("|%4d  ", i);
				if (i == temp || (i - temp) % 7 == 0)
				{
					printf("|\n");
					int j;
					for (j = 0; j < 7; j++)
					{
						printf("|------");
					}
					printf("|\n");
				}
			}
			ret = (leap[mon] % 7 + ret) % 7;
			temp = 7 - ret;
			if ((ret + leap[mon]) % 7 != 0)
			{
				printf("|\n");
			}
			for (i = 0; i < ret; i++)
			{
				printf("|------");
			}
			if (ret != 0)
			{
				printf("|\n");
			}
		}
		else
		{
			for (i = 1; i <= common[mon]; i++)
			{
				printf("|%4d  ", i);
				if (i == temp || (i - temp) % 7 == 0)
				{
					printf("|\n");
					int j;
					for (j = 0; j < 7; j++)
					{
						printf("|------");
					}
					printf("|\n");
				}
			}
			if ((ret + common[mon]) % 7 != 0)
			{
				printf("|\n");
			}
			ret = (common[mon] % 7 + ret) % 7;
			temp = 7 - ret;
			for (i = 0; i < ret; i++)
			{
				printf("|------");
			}
			if (ret != 0)
			{
				printf("|\n");
			}
		}
		printf("\n");
		mon++;
	} while (mon < 12);
	printf("  ***********************************************\n");
	printf("                万年历——%d年\n", year);
}

void show(int year, int mon, long sum, int leap[], int common[], char month[12][8])
{
	int ret = sum % 7;
	int temp = 7 - ret;
	int i;
	printf("\n                     %6s\n", month[mon - 1]);
	printf("--------------------------------------------------\n");
	char week[7][5] = { "周一","周二","周三","周四","周五","周六","周日" };
	for (i = 0; i < 7; i++)
	{
		printf("|%5s ", week[i]);
	}
	printf("|\n");
	for (i = 0; i < 7; i++)
	{
		printf("|------");
	}
	printf("|\n");
	for (i = 0; i < ret; i++)
	{
		printf("|      ");
	}
	if (is_leap(year) == 1)
	{
		for (i = 1; i <= leap[mon - 1]; i++)
		{
			printf("|%4d  ", i);
			if (i == temp || (i - temp) % 7 == 0)
			{
				printf("|\n");
				int j;
				for (j = 0; j < 7; j++)
				{
					printf("|------");
				}
				printf("|\n");
			}
		}
		if ((ret + leap[mon - 1]) % 7 != 0)
		{
			printf("|\n");
		}
		ret = ((leap[mon - 1]) % 7 + ret) % 7;
		for (i = 0; i < ret; i++)
		{
			printf("|------");
		}
		if (ret != 0)
		{
			printf("|\n");
		}
	}
	else
	{
		for (i = 1; i <= common[mon - 1]; i++)
		{
			printf("|%4d  ", i);
			if (i == temp || (i - temp) % 7 == 0)
			{
				printf("|\n");
				int j;
				for (j = 0; j < 7; j++)
				{
					printf("|------");
				}
				printf("|\n");
			}
		}
		if ((ret + common[mon - 1]) % 7 != 0)
		{
			printf("|\n");
		}
		ret = ((common[mon - 1]) % 7 + ret) % 7;
		for (i = 0; i < ret; i++)
		{
			printf("|------");
		}
		if (ret != 0)
		{
			printf("|\n");
		}
	}
}

5.3 test.c

#include "Calendar.h"

int leap[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
int common[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
char month[12][8] = { "一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月" };

void menu()
{
	printf("  ***********************************************\n");
	printf("  ****************** 1.查询某年     *************\n");
	printf("  ****************** 2.查询某年某月 *************\n");
	printf("  ****************** 0.退出查询     *************\n");
	printf("  ***********************************************\n");
}

void Calendar(year)
{
	long sum = days(year);
	display(year, sum, leap, common, month);
}

void M_calendar(int year, int mon)
{
	long sum = m_days(year, mon, leap, common);
	show(year, mon, sum, leap, common, month);
}

int main()
{
	int input;
	int year, mon;
	do
	{
		menu();
		printf("\n请选择:>");
		scanf("%d", &input);
		system("cls");
		switch (input)
		{
		case 1:
			printf("请输入需要查询的年份:>");
			while (~scanf("%d", &year))
			{
				system("cls");
				if (year >= 1)
				{
					Calendar(year);
					printf("请输入需要查询的年份:>");
				}
				else
				{
					printf("查询超出范围,请重新输入:>");
				}
			}
			break;
		case 2:
			printf("请输入需要查询的年月:>");
			while(~scanf("%4d%2d", &year, &mon))
			{
				if (year >= 1)
				{
					M_calendar(year, mon);
					printf("\n请输入需要查询的年月:>");
				}
				else
				{
					printf("查询超出范围,请重新输入:>");
				}
			}
			break;
		case 0:
			break;
		default:
			printf("输入错误,请重新选择:>");
			break;
		}
	} while (input);
	return 0;
}

上一篇:Biker's Trip Odometer


下一篇:Sicily 1240. Faulty Odometer