当前位置:网站首页>C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
2022-07-26 06:26:00 【liberg】
This is a 《C Language introduction practice 》 Series No 8 piece .
Last one :C Language introduction practice (7):switch-case Calculate the number of days in the year ( Ordinary year / Leap year calculations )
List of articles
subject
For any entered date ( Use variables for month, year and day respectively y、m、d Express ), Find the date of the next day . The result is yyyy/MM/dd Format output .
Tips
- Consider the end of the month 、 End of year and leap year .
- To calculate the number of days in the current month, you can use switch Statements for .
requirement
Example of input / output format 1:
Input :2019 12 31< enter >
Output :2020/01/01
Example of input / output format 2:
Input :2020 02 28< enter >
Output :2020/02/29
Pre knowledge
A hundred years : Can be 400 Divided by leap years ; Not a whole century : Can be 4 Divided by leap year .
Ordinary year 365 God , February is 28 God .
Leap year is 366 God , February is 29 God .
Reference code
#include <stdio.h>
int main() {
int y,m,d;
scanf("%d %d %d", &y, &m, &d);
// Ordinary year 2 month 28 God
int daysInFebruary = 28;
// If it's a leap year , Then February 29 God
if(y%400==0 || (y%100!=0 && y%4==0)) {
daysInFebruary = 29;
}
// Calculate the maximum number of days in the current month
int maxDays;
switch(m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
maxDays = 31;
break;
case 2:
maxDays = daysInFebruary;
break;
default:
maxDays = 30;
break;
}
// Processing days +1, If the number of days exceeds the maximum number of days in the month
// Continue processing month +1, If the month exceeds the maximum month of the year
// Then year +1
d += 1;
if(d > maxDays) {
// Exceed the maximum number of days in the current month 1 God , Count as next month 1 Number
d = 1;
// month +1
m += 1;
// Month exceeds , Then year +1
if(m > 12) {
m = 1;
y += 1;
}
}
printf("%d/%02d/%02d", y, m, d);
return 0;
}
Code duplicate
be familiar with switch-case Usage of , Note that each branch requires break.
The header file stdio.h Declarative scanf() and printf() It is used to process standard input and output in the standard library Library function .
边栏推荐
猜你喜欢

Advanced C language - archived address book (file)

时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)

Implementation of PHP multitask second timer

Leetcode:940. How many subsequences have different literal values

Swift basic FileManager (file management)
![[untitled]](/img/42/5e8b62edc0aa289098425b26df2453.jpg)
[untitled]

What is the concept and purpose of white box testing? And what are the main methods?

Interpretation of TPS motion (cvpr2022) video generation paper
![[day04_0421] C language multiple choice questions](/img/18/42924b5994b385a3cf132742141d88.png)
[day04_0421] C language multiple choice questions

Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
随机推荐
Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
PG vacuum auto vacuum
[day06_0423] C language multiple choice questions
CCTV dialogue ZTE: why must the database be in your own hands?
VRRP protocol and experimental configuration
Leetcode:741. picking cherries
RNN循环神经网络
Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
Interpretation of TPS motion (cvpr2022) video generation paper
BigDecimal becomes negative
[day03_0420] C language multiple choice questions
Embedded sharing collection 15
[day02_0419] C language multiple choice questions
英语句式参考纯享版 - 状语从句
If I want to listen to Jay Chou with you, I want you to listen to my whole youth
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
K. Link with Bracket Sequence I dp
Multi target detection
【Day04_0421】C语言选择题
多目标检测