当前位置:网站首页>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 .
边栏推荐
- 【Day03_0420】C语言选择题
- Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
- Why use the static keyword when defining methods
- Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
- How can machinery manufacturing enterprises do well in production management with the help of ERP system?
- [day_010418] delete public string
- [day04_0421] C language multiple choice questions
- YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
- [day_040421] binary conversion
猜你喜欢

【pytorch】图片增广

Upgrade appium automation framework to the latest 2.0

BPG notes (IV)

RNN recurrent neural network

白盒测试的概念、目的是什么?及主要方法有哪些?

【Day_06 0423】不要二

性能测试包括哪些方面?分类及测试方法有哪些?

Intelligent fire protection application based on fire GIS system

Easycvr video square channel display and video access full screen display style problem repair

多目标检测
随机推荐
[pytorch] picture enlargement
YOLOv6:又快又准的目标检测框架开源啦
JVM class loading and GC garbage collection mechanism
【C语言】文件操作
Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
Matlab vector and matrix
CCTV dialogue ZTE: why must the database be in your own hands?
English sentence pattern reference exclusive Edition - attributive clause
Yolov6: the fast and accurate target detection framework is open source
【C语言】通讯录动态版和文件版
L. Link with Level Editor I dp
C language explanation series - comprehensive exercises, guessing numbers games
【BM2 链表内指定区间反转】
09 eth smart contract
PHP 多任务秒级定时器的实现方法
[day04_0421] C language multiple choice questions
If introduced according to the open source framework
【pytorch】微调技术
[day_020419] inverted string
Latex同时合并表格的多行多列