当前位置:网站首页>【C语言】NextDay问题
【C语言】NextDay问题
2022-06-28 11:36:00 【贾璞】
输入一个时间,某年某月某日,输出下一天
主要就是边界控制,不使用愚蠢写法,无技术含量,仅仅逻辑性考验
注释完善,自行理解
执行环境:Ubuntu18.04 CLion/GCC
WIndows环境也无需修改
Code:
/* Next Day * 注意:紧扣题意,输入格式为:xx年xx月xx日,正好利用这锻炼格式化字符串sscanf()函数 * 至于例如2019 02 25这种格式,如此一来,写的更没意思了 * 包括多此一举在堆区开辟空间,同时也避免了用户故意无限输入导致栈区溢出问题 * 学习软件测试,就要考虑到万无一失 * 考研的同学可以多了解这些函数 * * */
#include <stdio.h>
#include <stdlib.h>
int main(void) {
//定义13个长度,0号索引不使用,避免后期+1操作
int monArray[13] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("输入年月日[Format:xx年xx月xx日]:\n");
//堆区开辟空间,栈区空间Windows默认只有1M,Linux一般默认8M,可以扩大,在此不做赘述
char *time = malloc(64);
scanf("%s", time);
int year, mon, day;
//格式化字符串置于对应变量中
sscanf(time, "%d年%d月%d日", &year, &mon, &day);
//判断闰年后2月份数组天数加一
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
monArray[2]++;
//月,日未超范围
if ((mon <= 12 && mon >= 1) && (day <= monArray[mon])) {
//月尾判断
if (day == monArray[mon]) {
//到达年尾
if (mon == 12) {
day = 1;
mon = 1;
year++;
}
//只达到月尾
else {
day = 1;
mon++;
}
}
//月日均为超范围
else {
day++;
}
}
//月日超范围,直接return
else {
printf("Data Error\n");
return EXIT_FAILURE;
}
//计算成功
printf("Next Day is:%d年%d月%d日\n", year, mon, day);
return EXIT_SUCCESS;
}

边栏推荐
- day29 js笔记 2021.09.23
- day25 js中的预解析、递归函数、事件 2021.09.16
- Pre parsing, recursive functions and events in day25 JS 2021.09.16
- Why do many people want to change careers as programmers, while some programmers want to change careers as others?
- Prefix and (2D)
- Day39 prototype chain and page Fireworks Effect 2021.10.13
- [sciter]: use of sciter FS module scanning file API and its details
- If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience
- AcWing 609. Salary (implemented in C language)
- [Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
猜你喜欢

ArrayList源码解析

.NET混合开发解决方案24 WebView2对比CefSharp的超强优势

Class pattern and syntax in JS 2021.11.10

day30 js笔记 BOM和DOM 2021.09.24

赛尔号抽奖模拟求期望

无法重新声明块范围变量

Day37 JS note motion function 2021.10.11

Oracle date format exception: invalid number

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022
随机推荐
Allez, Meta - Cosme, comme prévu, cette chaleur ne durera pas longtemps.
AcWing 605. Simple product (implemented in C language)
.NET混合开发解决方案24 WebView2对比CefSharp的超强优势
[sciter]: how sciter uses i18 to realize multi language switching of desktop applications and its advantages and disadvantages
5. Sum of N numbers
day39 原型链及页面烟花效果 2021.10.13
day31 js笔记 DOM下 2021.09.26
Self use demo of basic component integration of fluent
来吧元宇宙,果然这热度一时半会儿过不去了
面试步骤的面试技巧
New listing of operation light 3.0 - a sincere work of self subversion across the times!
Apache2 configuration denies access to the directory, but can access the settings of the files inside
Contract quantitative trading system development | contract quantitative app development (ready-made cases)
Cohere, a large model company, completed the round B financing of US $125million
Day29 JS notes 2021.09.23
分析list中有无重复数据且重复了几次
AcWing 608. Poor (implemented in C language)
day25 js中的预解析、递归函数、事件 2021.09.16
2022中国信通院首届业务与应用安全发展论坛成功召开!
Recommended practice sharing of Zhilian recruitment based on Nebula graph