当前位置:网站首页>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
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 !
边栏推荐
- Vofa+ software usage record
- Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
- Stm32--- systick timer
- 实例009:暂停一秒输出
- Hardware 3 -- function of voltage follower
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
- MySQL之MHA高可用集群
- [trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
- Several important parameters of LDO circuit design and type selection
- STM32 single chip microcomputer - external interrupt
猜你喜欢
Management and use of DokuWiki (supplementary)
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
剑指 Offer 09. 用两个栈实现队列
99 multiplication table (C language)
如何写Cover Letter?
UE像素流,来颗“减肥药”吧!
DCDC circuit - function of bootstrap capacitor
实例009:暂停一秒输出
Array integration initialization (C language)
随机推荐
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Naming rules for FreeRTOS
關於線性穩壓器的五個設計細節
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
STM32 outputs 1PPS with adjustable phase
Talk about the function of magnetic beads in circuits
Sword finger offer 06 Print linked list from end to end
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
Sql Server的存儲過程詳解
STM32---IIC
STM32 single chip microcomputer -- volatile keyword
Sizeof (function name) =?
QEMU demo makefile analysis
Installation and use of libjpeg and ligpng
Talk about the circuit use of TVs tube
Relationship between line voltage and phase voltage, line current and phase current
如何写Cover Letter?
Introduction of air gap, etc
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
[three tier architecture and JDBC summary]