其他分享
首页 > 其他分享> > C语言—万年历打印(详细易懂)

C语言—万年历打印(详细易懂)

作者:互联网

目录

01、实现效果

02、过程步骤

03、代码


01、实现效果

在这里插入图片描述

02、过程步骤

03、代码

#include<stdio.h>
int Leap_year[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31 };
int Com_year[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 
char Week[7][5] = { "一","二","三","四","五","六","日" };
int year, month, day;
//用户输入日期
int User_Scanf()
{
	printf("请输入年-月-日:");
	while (scanf("%d-%d-%d", &year, &month, &day) != EOF)
	{
		//是否在有效范围内检查
		if (year <= 0 || year >= 10000 || month < 0 || month >= 13 || day < 0 || day >= 31)
		{
			printf("Error!\n");
			continue;
		}
		return 0;
	}
}
//打印星期
void Print_Week()
{
	printf("********************************************************\n");
	printf("\t\t\t万年历\t\t\t\n");
	for (int i = 0; i < 7; i++)
	{
		printf("%s\t", Week[i]);
	}
	printf("\n");
}
//判断闰/平年
int is_Leap_Com(int year)
{
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
	{
		return 1;//闰年
	}
	else
		return 0;//平年
}
//计算天数
int Count_Day(int year,int month,int day,int Leap_year[],int Com_year[])
{
	int sum = 0;
	//计算年天数
	if (is_Leap_Com(year) == 1)
	{
		sum += 366;//闰年
	}
	else
		sum += 365;//平年
	
	//计算月天数
	if (is_Leap_Com(year) == 1)
	{
		for (int i = 1; i <=month; i++)
		{
			sum += Leap_year[i];//闰月
		}
	}
	else
	{
		for (int i = 1; i <=month; i++)
		{
			sum += Com_year[i];//平月
		}
	}
	return sum;
}
//打印日历
void Print_Day(int num, int year, int month)
{
	int result = 0, temp = 0;
	result = num % 7;//打印空格
	for (int i = 0; i < result; i++)
	{
		printf("\t");
	}
	temp = 7 - result;//打印除空格外的日历
	if (is_Leap_Com(year) == 1)
	{
		for (int i = 1; i <=Leap_year[month]; i++)
		{
			printf("%d\t", i);
			if (i == temp || (i - temp) % 7 == 0)//判断是否换行
				printf("\n");
		}
	}
	else
	{
		for (int i = 1; i <=Com_year[month]; i++)
		{
			printf("%d\t", i);
			if (i == temp || (i - temp) % 7 == 0)
				printf("\n");
		}
	}
	printf("\n");
	printf("********************************************************\n");
}
int main()
{
	User_Scanf();//用户输入日期
	Print_Week();//打印星期
	int num=Count_Day(year,month,day, Leap_year, Com_year);计算天数
	Print_Day(num,year, month);//打印日历
	return 0;
}

标签:printf,万年历,int,31,30,C语言,year,易懂,month
来源: https://blog.csdn.net/m0_65592409/article/details/122816307