酷代码 AI
菜单

#include <stdio.h> struct DateG { int yy, mm, dd; }; // 判断是否为闰年 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++

以下是对这段代码的错误分析与修改: 错误分析 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` 去除换行符。 通过这些修改,代码的健壮性和正确性得到了提升。 [2025-12-12 20:54:15 | AI问答 | 1504点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多