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

边栏推荐
- Day30 JS notes BOM and DOM 2021.09.24
- Research on personalized product search
- 无法重新声明块范围变量
- AcWing 610. Salary and bonus (implemented in C language)
- 多维度监控:智能监控的数据基础
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
- JS foundation 10
- Daily practice of C language - day 3: calculate the number of occurrences of sub strings of strings
- Fancy features and cheap prices! What is the true strength of Changan's new SUV?
- Unity screenshot function
猜你喜欢

Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration

day33 js笔记 事件(下)2021.09.28

SEO优化的许多好处是与流量有直接关系

Fancy features and cheap prices! What is the true strength of Changan's new SUV?

建立自己的网站(18)

Day33 JS note event (Part 2) September 28, 2021

Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released

day34 js笔记 正则表达式 2021.09.29

Day29 JS notes 2021.09.23

Redis 原理 - List
随机推荐
SoapUI rookie tutorial
Excel import / export convenience tool class
Machine learning project captcha based on verification code recognition_ Trainer operation practice
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
2. single digit statistics
js中的class类模式及语法 2021.11.10
[sciter]: use of sciter FS module scanning file API and its details
【无标题】虚拟机vmnet0找不到且报错:没有未桥接的主机网络适配器
day23 js笔记 2021.09.14
Recommended practice sharing of Zhilian recruitment based on Nebula graph
Remote login sshd service
AcWing 604. Area of circle (implemented in C language)
Fruit FL studio/cubase/studio one music host software comparison
MySql5.7添加新用户
來吧元宇宙,果然這熱度一時半會兒過不去了
QML control type: tabbar
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
js中this的默认指向及如何修改指向 2021.11.09
day37 js笔记 运动函数 2021.10.11
Unity屏幕截图功能