C language implementation of perpetual calendar (with code) The first C language program completed by Xiaobai, I hope everyone will pay more attention and like it

Preface to realize perpetual calendar in C language
: This article introduces how to use C language code to realize perpetual calendar usage examples, explains the method of writing perpetual calendar, and teaches you to easily learn to write perpetual calendar. This small program is the first relatively complete small program written by myself. It is a summary of the basics of C language programming learned in the first semester of freshman year.

knowledge emphasis

Special Note

Description of each function

1. Print asterisk function

2. Print calendar header function

3. Judgment of leap year average year function

4. Calculate the total number of days function

5. Print calendar function

6.main function (main function)

full code

Summarize

Knowledge Emphasis
1. Due to the decree of Pope Gregor VIII on February 24, 1582, the period from October 5, 1582 to October 14, 1582 was permanently erased. So these 10 days never existed in history. February 24, 1582 was a Thursday, and its next day was October 5, 1582, which was a Friday.
Therefore, considering the impact of these ten days, this article uses January 1, 1900 for calculations, which happens to be Monday, which is convenient for our program calculations.
2. The method of judging leap year and normal year
Leap year: the number of years is a multiple of 4 but not a multiple of 100, or the number of years is a multiple of 400.
Ordinary years: All years except leap years are ordinary years.
3. There are 366 days in a leap year and 365 days in an ordinary year. February in a leap year has 29 days, and February in an ordinary year has 28 days.

Special Notes :
1. The first day of the calendar written in this article starts from Monday, which is different from that of some paper calendars that start from Sunday.
2. The calendar written in this article is the calendar of a certain month in a certain year, not a complete annual calendar. Description of each function
as shown in the figure below 1. Print asterisk functionperpetual calendar

void printStar()
{
    
    
	printf("*****************************************************\n");	
} 

The purpose is to modify the output perpetual calendar to make it more beautiful and beautiful.

2. Print calendar header function

char weekday[7][10]={
    
    "一","二","三","四","五","六","日"};
void printBegin()
{
    
    
	printf("\t\t\t万年历\t\t\t\n");
	printStar();
	for(int i=0;i<7;i++)
	{
    
    
		printf("%s\t",weekday[i]);	
	}
	printf("\n"); 
} 

The header and package of the calendar are combined for easy output from Monday to Sunday.

3. Judgment of leap year average year function

int leap(int year)
{
    
    
	if(year%4==0&&year%100!=0||year%400==0)
	{
    
    
		return 1;
	}
	else
	{
    
    
		return 0;
	}
	/*int four,hundred,fourhundred;
	four=year%4;
	hundred=year%100;
	fourhundred=year%400;
	if(four==0&&hundred==0&&fourhundred==0)
	{
		return 1;
	}
	else if(four==0&&hundred!=0&&fourhundred!=0)
	{
		return 1;
	}
	return 0;*/
}

Encapsulate the function of judging leap year and normal year into one function, which is more convenient to call. Here, there are two methods for calculating common years and leap years, and the second method is a bit troublesome to understand. I prefer to choose the first option.

4. Calculate the total number of days function

//1900年1月1日是星期一,比较方便后面的计算 
//我们需要算我们输入的年份离1900年有多少天
int  daySum(int year,int month)
{
    
    
	//1900 1 1 是星期一
	int i,sum=0;
	for(i=1900;i<year;i++)	
	{
    
    
		//判断当前年份是闰年还是平年
		{
    
    	
			if(leap(i))
			{
    
    
				sum+=366;
			}
			else
			{
    
    
				sum+=365;	
			} 
		} 
	}
	for(i=0;i<month-1;i++)//i<month-1是因为我们不能将我们输入的月份计算在内 
	{
    
    
		if(leap(year)==1)
		{
    
    
			sum+=runnian[i];	
		} 
		else
		{
    
    
			sum+=pingnian[i];
		}
	}
	return sum;
} 

This function is to calculate how many days the month we input is from January 1, 1900, which is convenient for us to calculate below.

5. Print calendar function

//打印日历 
void printResult(int sum,int year,int month)
{
    
    
	int result,temp,i;//result为余数,temp为中间变量,i为循环变量
	result=sum%7;//日历上前面空余的天数,就是总天数除以7的余数
	//把当前月份空余的天数用空格表示
	for(i=0;i<result;i++)
	{
    
    
		printf("\t");	
	} 
	temp=7-result;//从第几格开始打印一号
	if(leap(year)==1) 
	{
    
    
		//数组下标从0开始
		//月份只能从1开始 
		for(i=1;i<=runnian[month-1];i++)
		{
    
    
			printf("%d\t",i);
			if(i==temp||(i-temp)%7==0)	
			{
    
    
				printf("\n");
			}
		}
		printf("\n");	
	} 
	else 
	{
    
    
		for(i=1;i<=pingnian[month-1];i++)
		{
    
    
			printf("%d\t",i);
			if(i==temp||(i-temp)%7==0)	
			{
    
    
				printf("\n");
			}
		}	
	} 
	printf("\n");
} 

The function of this function is to print the calendar, and the result is the number of days left on the calendarPicture 1

The obtained result is to output these blank parts and output the number of free days in the current month.

5.main function (main function)

int main(void)
{
    
    
	//用户输入年 月 
	printf("请输入年-月:"); 
	scanf("%d-%d",&year,&month);
	printBegin();
	int sum=daySum(year,month);
	printResult(sum,year,month);
	printStar();
	system("pause");//防止闪屏 
	return 0;	
}

Call other functions in the main function to make a complete perpetual calendar.

full code

/*
	1.用户输入年,月
	2.打印一个日历 
*/
#define _CRT_SECURE_NO_WARNINGS//使用宏定义是让编译器进行操作,让编译器忽略内库存的问题 
#include<stdio.h>
#include<stdlib.h>
int year,month;
//闰年和平年   2月份天数不一样
int  runnian[12]={
    
    31,29,31,30,31,30,31,31,30,31,30,31};
int  pingnian[12]={
    
    31,28,31,30,31,30,31,31,30,31,30,31};
char weekday[7][10]={
    
    "一","二","三","四","五","六","日"}; 
//打印星号
void printStar()
{
    
    
	printf("*****************************************************\n");	
} 
//打印日历的抬头
void printBegin()
{
    
    
	printf("\t\t\t万年历\t\t\t\n");
	printStar();
	for(int i=0;i<7;i++)
	{
    
    
		printf("%s\t",weekday[i]);	
	}
	printf("\n"); 
} 
//判断当前年份是闰年还是平年 
int leap(int year)
{
    
    
	if(year%4==0&&year%100!=0||year%400==0)
	{
    
    
		return 1;
	}
	else
	{
    
    
		return 0;
	}
	/*int four,hundred,fourhundred;
	four=year%4;
	hundred=year%100;
	fourhundred=year%400;
	if(four==0&&hundred==0&&fourhundred==0)
	{
		return 1;
	}
	else if(four==0&&hundred!=0&&fourhundred!=0)
	{
		return 1;
	}
	return 0;*/
}
//1900年1月1日是星期一,比较方便后面的计算 
//我们需要算我们输入的年份离1900年有多少天
int  daySum(int year,int month)
{
    
    
	//1900 1 1 是星期一
	int i,sum=0;
	for(i=1900;i<year;i++)	
	{
    
    
		//判断当前年份是闰年还是平年
		{
    
    	
			if(leap(i))
			{
    
    
				sum+=366;
			}
			else
			{
    
    
				sum+=365;	
			} 
		} 
	}
	for(i=0;i<month-1;i++)//i<month-1是因为我们不能将我们输入的月份计算在内 
	{
    
    
		if(leap(year)==1)
		{
    
    
			sum+=runnian[i];	
		} 
		else
		{
    
    
			sum+=pingnian[i];
		}
	}
	return sum;
} 
//打印日历 
void printResult(int sum,int year,int month)
{
    
    
	int result,temp,i;//result为余数,temp为中间变量,i为循环变量
	result=sum%7;//日历上前面空余的天数	,就是总天数除以7的余数
	//把当前月份空余的天数用空格表示
	for(i=0;i<result;i++)
	{
    
    
		printf("\t");	
	} 
	temp=7-result;//从第几格开始打印一号
	if(leap(year)==1) 
	{
    
    
		//数组下标从0开始
		//月份只能从1开始 
		for(i=1;i<=runnian[month-1];i++)
		{
    
    
			printf("%d\t",i);
			if(i==temp||(i-temp)%7==0)	
			{
    
    
				printf("\n");
			}
		}
		printf("\n");	
	} 
	else 
	{
    
    
		for(i=1;i<=pingnian[month-1];i++)
		{
    
    
			printf("%d\t",i);
			if(i==temp||(i-temp)%7==0)	
			{
    
    
				printf("\n");
			}
		}	
	} 
	printf("\n");
} 
int main(void)
{
    
    
	
	//用户输入年 月 
	printf("请输入年-月:"); 
	scanf("%d-%d",&year,&month);
	printBegin();
	int sum=daySum(year,month);
	printResult(sum,year,month);
	printStar();
	system("pause");//防止闪屏 
	return 0;	
}

Here we conduct a test, when we input 2022-12, see if the running result of the program we wrote can be the same as the calendar on the computer.
This is the result of our running:
2002-12
This is the computer calendar: 2002-12-
We can observe from these two pictures that the result of our code running is similar to that of the computer calendar.

Summary
The above is based on the input of the year and month, and the calendar of the current month is output. On this basis, we can also write a calendar that outputs twelve months of the year by inputting the year. If you are interested, you can try to write it yourself.
This is the first time I have written a complete program. If there are any mistakes or doubts, please point them out in the comment area. I hope everyone can pay attention to me, a newcomer, and like it!

Guess you like

Origin blog.csdn.net/z2004cx/article/details/128251623