当前位置:网站首页>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 !
边栏推荐
- Five design details of linear regulator
- Go dependency injection -- Google open source library wire
- Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
- 【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
- Slist of linked list
- 第十八章 使用工作队列管理器(一)
- Example 010: time to show
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
- Weidongshan Internet of things learning lesson 1
- QEMU STM32 vscode debugging environment configuration
猜你喜欢
Circleq of linked list
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
QEMU STM32 vscode debugging environment configuration
leetcode - 445. Add two numbers II
H264 (I) i/p/b frame gop/idr/ and other parameters
Example 008: 99 multiplication table
STM32 single chip microcomputer -- volatile keyword
Step motor generates S-curve upper computer
Sword finger offer 09 Implementing queues with two stacks
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
随机推荐
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
How to copy formatted notepad++ text?
[three tier architecture]
leetcode - 445. Add two numbers II
Talk about the circuit use of TVs tube
Stm32--- systick timer
STM32 --- NVIC interrupt
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Take you to understand the working principle of lithium battery protection board
实例006:斐波那契数列
Sql Server的存儲過程詳解
Shell script
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
Why is 1900 not a leap year
Verilog -- state machine coding method
MATLAB小技巧(28)模糊综合评价
DokuWiki deployment notes
Introduction of air gap, etc
MySQL MHA high availability cluster
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.