C 语言日期计算代码:错误分析、修改与健壮性提升
以下是对这段代码的错误分析与修改: 错误分析 1. `_DateG` 函数声明问题:在 C 语言里,函数若未提前声明就使用,编译器会默认其返回 `int` 类型,这可能引发类型不匹配的错误。所以要提前声明 `_DateG` 函数。 2. `Date2string` 函数返回静态数组问题:`Date2string` 函数返回的是静态数组的指针,这在多线程环境或者多次调用该函数时会有问题,因为静态数组是全局唯一的,后续调用会覆盖之前的结果。可以考虑动态分配内存或者让调用者传入缓冲区。 3. `scanf` 输入问题:使用 `scanf("%s", st)` 读取日期字符串时,若输入包含空格等分隔符,会提前截断输入。可以使用 `fgets` 来读取整行输入。 4. 日期计算精度问题:在计算两个日期之间的年数时,使用 `(Days(d2) - Days(d1)) / 365.0` 简单地除以 365 会有精度损失,因为存在闰年的情况。 修改后的代码 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义日期结构体 struct DateG { int yy, mm, dd; }; // 声明函数 char leapYear(int year); int Days(struct DateG x); struct DateG days2Date(int x); struct DateG _DateG(char *st); struct DateG Add(struct DateG x, int y); char* Date2string(struct DateG x); // 判断是否为闰年 char leapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 每个月的天数 int monthDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 计算自公元1年1月1日到指定的日期共经历了多少天 int Days(struct DateG x) { int days = 0; // 计算之前年份的总天数 for (int year = 1; year < x.yy; year++) { days += 365 + leapYear(year); } // 计算当前年份到当前月份之前的总天数 for (int month = 1; month < x.mm; month++) { days += monthDays[month]; if (month == 2 && leapYear(x.yy)) { days++; } } // 加上当前月份的天数 days += x.dd; return days; } // 由自公元1年1月1日历经指定的天数后的日期 struct DateG days2Date(int x) { struct DateG date = {1, 1, 1}; int year = 1; // 先减去之前年份的天数 while (1) { int daysInYear = 365 + leapYear(year); if (x > daysInYear) { x -= daysInYear; year++; } else { break; } } date.yy = year; // 减去当前年份到当前月份之前的天数 int month = 1; while (1) { int daysInMonth = monthDays[month]; if (month == 2 && leapYear(year)) { daysInMonth++; } if (x > daysInMonth) { x -= daysInMonth; month++; } else { break; } } date.mm = month; date.dd = x; return date; } // 将字符串转换为日期结构体 struct DateG _DateG(char *st) { struct DateG date; sscanf(st, "%d-%d-%d", &date.yy, &date.mm, &date.dd); return date; } // 日期加上指定天数 struct DateG Add(struct DateG x, int y) { int days = Days(x) + y; return days2Date(days); } // 将日期结构体转换为字符串 char* Date2string(struct DateG x) { char *str = (char *)malloc(12 * sizeof(char)); if (str == NULL) { fprintf(stderr, "Memory allocation failed\n"); exit(1); } sprintf(str, "%04d-%02d-%02d", x.yy, x.mm, x.dd); return str; } int main() { char st[12]; // 使用 fgets 读取整行输入 if (fgets(st, sizeof(st), stdin) != NULL) { // 去除换行符 st[strcspn(st, "\n")] = 0; } struct DateG d1 = _DateG(st); struct DateG d2 = Add(d1, 60 * 365); char *date_str = Date2string(d2); printf("[%s]\n", date_str); free(date_str); d2 = Add(d2, -1000); date_str = Date2string(d2); printf("[%s]\n", date_str); free(date_str); d2 = _DateG("2020-1-23"); printf("%.2f\n", (Days(d2) - Days(d1)) / 365.0); return 0; } ``` 修改说明 1. 函数声明:在文件开头提前声明了 `_DateG` 函数,避免编译器默认其返回 `int` 类型。 2. `Date2string` 函数:使用 `malloc` 动态分配内存来存储日期字符串,避免静态数组的问题。在使用完后,要记得使用 `free` 释放内存。 3. 输入读取:使用 `fgets` 读取整行输入,防止输入包含空格时被截断。同时使用 `strcspn` 去除换行符。 通过这些修改,代码的健壮性和正确性得到了提升。 ######[AI问答 | 1504点数解答 | 2025-12-12 20:54:15]
- Photoshop 错误 8800 大揭秘:原因分析与实用解决方案(DeepSeek | 349点数解答 | 2025-02-28 11:19:16)572
- MaxScript 导出模型长度到 CSV 文件:代码错误排查与修正全解析 (字节豆包 | 525点数解答 | 2025-07-09 15:37:45)191
- Java 实现:轻松生成 5 位数字、大小写字母混合验证码( | 266点数解答 | 2024-03-06 17:39:10)330
- 深度剖析:游戏中两个 Buff 效果的触发条件、逻辑及注意要点 (阿里通义 | 566点数解答 | 2024-11-26 14:24:12)259
- 深度剖析 Vue 2 插槽:从基础使用到高级玩法(字节豆包 | 165点数解答 | 2024-11-29 11:25:06)215
- C++实现支持四则运算的简单计算器,处理除数为0和无效操作(字节豆包 | 241点数解答 | 2025-10-29 20:21:29)61
- C语言破解甲乙丙真假话谜题:开发中需留意的三大要点(字节豆包 | 444点数解答 | 2025-11-24 00:10:58)51
- C++ 代码实现自定义运算 x@y = x² + y² - xy 及 x@(x+y) 计算(DeepSeek | 215点数解答 | 2025-12-13 15:08:14)69
- C++ 实现根据输入正整数奇偶性计算偶数和或约数和(字节豆包 | 838点数解答 | 2026-02-28 20:23:59)25
- Excel 日期格式显示####?这些解决办法快收好!( | 327点数解答 | 2023-12-29 09:53:45)280
- Java实现日程管理系统:添加、修改、查询与提醒一应俱全,数据本地txt保存!(GPT | 5818点数解答 | 2024-06-03 20:02:47)344
- 用Python处理商品销售数据:替换类别、合并类目并输出前5行(百度文心 | 486点数解答 | 2024-08-23 09:14:21)311