当前位置:网站首页>Daily question - input a date and output the day of the year

Daily question - input a date and output the day of the year

2022-07-05 08:28:00 Protect Xiaozhou

Hello, everyone , I'm protecting Xiao Zhou ღ, This issue brings you programming to input a certain year, a certain month, a certain day , Output it is the day of the year , Let's have a look ~

 

subject :

Multi group input , Programming to enter a certain year, a certain month, a certain day , Output it is the day of the year .

( Pay attention to the data input range )

example 1:

Input :

2021-5-1

Output :

It is not a leap year.

Today is the 121 day of the year.

example 2:

Input :

2000-5-1

Output :

It is a leap year.

Today is the 122 day of the year.

example 3:

Input :

2020-13-12

Output :

Input error, please re-enter the date.

Thinking analysis :

First we enter a date , We can define three variables year,month,day, On behalf of , You can also use a structure to describe a date . utilize switch(month) Function to determine the number of days in the month, year and day , Year of importation , If the year is a leap year , Then the 2 Monthly 29 God , otherwise 2 Month only 28 God .

There is... In a month 31 The month of days has 1 3 5 7 8 10 12

There is... In a month 30 The month of days has 4 6 9 11

The judgment condition of leap year , The year can be 4 Divisible and not divisible by 100 to be divisible by , But can be 400 A leap year is a divisible year .

The topic also involves multiple sets of inputs , We need to understand this problem scanf() function ;scanf() It is a formatted input function for standard input stream .

How to realize multi group input ? scanf()  The function has a return value ,scanf() The return value of is the number of variables that have been assigned successfully , And is an integer .

for example :

int size=scanf("%d %d",&a,&b);

Keyboard entry 1 2 when scanf() The return value of 2, therefore size==2;

If only a or b One of them was successfully read , that scanf() The return value of is 1;

If only a and b Have not been successfully read into , The return value is 0;

In addition, there is another way to write multiple groups of input, which is to use EOF,EOF yes end of file Abbreviation , Express “ Text flow ” Ending (file), It can also be the end of standard input (stdin).

for example :

int size=scanf("%d %d",&a,&b);

If Encounter errors or encounter end of file The return value is EOF.

So we can write multiple groups of input like this :

while(scanf("%d %d",&a,&b)==2)
{
   ……
}

You can also write :

while(scanf("%d %d",&a,&b)!=EOF)
{
  ……
}

Code implementation :

#include<stdio.h>、

// Judge whether it's a leap year , If it is a leap year, return to 1, If it is not a leap year, return 0
int IfLeapYear(int year)
{
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		return 1;
	else
		return 0;
}

//Days function : Judge how many days 
Days(int year, int month, int day)
{
	int d, days = 0;
	// Judge whether it's a leap year 
	int size = IfLeapYear(year);
	if (size == 0)
	{
		printf("It is not a leap year.\n");
	}
	else
	{
		printf("It is a leap year.\n");
	}
	// Traverse the months that have passed completely , And count the days of each full moon 
	for (int i = 1; i < month; i++)
	{
		switch (i)
		{
		case 2:
			if(size==1)
			{
				d = 29;
			}
			else
			{
				d = 28;
			}break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:d = 31; break;
		case 4:
		case 6:
		case 9:
		case 11:d = 30; break;
		default:printf("error\n");
		}
		days += d;
	}
	// Statistics “ waning moon ” Days of 
	days += day;
	return days;// Return total days 
}

int main()
{
	int year, month, day;
	printf(" Please enter a date , Judge him as the day of the year .\n");
	while (scanf("%d-%d-%d", &year, &month, &day)==3)//scanf("%d-%d-%d", &year, &month, &day)!=EOF
	{
        // Judge whether the month is entered correctly 
		if (month < 1 || month>12)
		{
			printf("Input error, please re-enter the date:\n");
		}
		else
		printf("Today is the %d day of the year.\n",Days(year, month, day));
	}
	return 0;
}

Describe the date with a structure :

// Define date type 
typedef struct  Date
{
	int year;
	int month;
	int day;

}Date;

int main()
{
	// Define the date type of Date1 Variable 
	Date Date1;
	printf(" Please enter a date , Judge him as the day of the year .\n");
	while (scanf("%d-%d-%d", &Date1.year, &Date1.month, &Date1.day) == 3)//scanf("%d-%d-%d", &year, &month, &day)!=EOF
	{
		if (Date1.month < 1 || Date1.month>12)
		{
			printf("Input error, please re-enter the date:\n");
		}
		else
			// So we can Date Type of Date1 Variables are passed as a whole , If you send an address, you can use Date*  Pointer reception for , Access by pointer Date1 The members of 
			printf("Today is the %d day of the year.\n", Days(&Date1);
	}
	return 0;
}

What do you don't know about the structure , You can refer to another blog of the blogger , A detailed introduction to

1. Declaration of struct type

2. Structure initialization

3. Structure member access

4. Structural parameters

http://t.csdn.cn/Uz3um

  Interested friends can use the blogger's method , Or do this problem in your own way , Optimize the code , Try how to judge if we enter the month correctly , The number of days entered is in the correct range ? Leave a comment in the comments section .

Share a similar topic on Niuke website , You can also try to do it .

link : The day of the year _ Bili Bili pen test questions _ Cattle from

Thank you for watching this article , Please look forward to : Protect Xiao Zhou ღ  **,°*:.*( ̄▽ ̄)/$:*.°** 

   If there is infringement, please contact to modify and delete !

 

原网站

版权声明
本文为[Protect Xiaozhou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050823150529.html