当前位置:网站首页>【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;
}

边栏推荐
- Packaging and publishing application of jetpack compose desktop version
- Batch will png . bmp . JPEG format pictures are converted to Jpg format picture
- 6.A-B
- Solutions to connection failures and errors when accessing mysql8 using the SSM project
- Open3d manual clipping point cloud
- day32 js笔记 事件(上)2021.09.27
- What method is required for word, PDF and txt files to realize full-text content retrieval?
- 3. seat number
- Pre parsing, recursive functions and events in day25 JS 2021.09.16
- Graphics view framework for QT learning (to realize startup animation)
猜你喜欢

Fruit FL studio/cubase/studio one music host software comparison

Packaging and publishing application of jetpack compose desktop version

《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作!

Simulation of the Saier lottery to seek expectation

Still using simpledateformat for time formatting? Be careful that the project collapses!

js中的class类模式及语法 2021.11.10

day39 原型链及页面烟花效果 2021.10.13

day32 js笔记 事件(上)2021.09.27

Data analysis learning notes

What method is required for word, PDF and txt files to realize full-text content retrieval?
随机推荐
js中的class类模式及语法 2021.11.10
Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
Day23 JS notes 2021.09.14
Adding a new user in MySQL 5.7
js中this的默认指向及如何修改指向 2021.11.09
Database Series: is there any way to seamlessly upgrade the business tables of the database
MapReduce项目案例3——温度统计
5. Sum of N numbers
Research on personalized product search
Redis hash hash type string (5)
day28 严格模式、字符串 js 2021.09.22
Software test interview classic + 1000 high-frequency real questions, and the hit rate of big companies is 80%
Ali three sides: what is the difference between using on or where in the left join associated table and the condition
水果FL Studio/Cubase/Studio one音乐宿主软件对比
Redis principle - List
SoapUI rookie tutorial
Prefix and (one dimension)
Interview skills for interview steps
Data analysis learning notes
Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community