C语言实现日期与天数相互转换:从公元1年1月1日起算
```c #include <stdio.h> struct DateG { int yy, mm, dd; }; // 判断是否为闰年 char leapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 每月的天数 int daysInMonth(int year, int month) { int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && leapYear(year)) { return 29; } return days[month - 1]; } // 计算自公元1年1月1日到指定的日期共经历了多少天 int Days(struct DateG x) { int totalDays = 0; for (int year = 1; year < x.yy; year++) { totalDays += leapYear(year) ? 366 : 365; } for (int month = 1; month < x.mm; month++) { totalDays += daysInMonth(x.yy, month); } totalDays += x.dd; return totalDays; } // 由自公元1年1月1日历经指定的天数后的日期 struct DateG days2Date(int x) { struct DateG date = {1, 1, 1}; while (x > 0) { int daysInThisYear = leapYear(date.yy) ? 366 : 365; if (x >= daysInThisYear) { x -= daysInThisYear; date.yy++; } else { for (int month = 1; month <= 12; month++) { int daysInThisMonth = daysInMonth(date.yy, month); if (x >= daysInThisMonth) { x -= daysInThisMonth; date.mm++; } else { date.dd = x; x = 0; break; } } } } 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) { static char str[12]; sprintf(str, "%04d-%02d-%02d", x.yy, x.mm, x.dd); return str; } int main() { char st[12]; scanf("%s", st); struct DateG d1 = _DateG(st), d2 = Add(d1, 60 * 365); printf("[%s]\n", Date2string(d2)); d2 = Add(d2, -1000); printf("[%s]\n", Date2string(d2)); d2 = _DateG("2020-1-23"); printf("%.2f\n", (Days(d2) - Days(d1)) / 365.0); return 0; } ``` 代码说明: 1. `leapYear`函数:用于判断某一年是否为闰年。如果是闰年返回`1`,否则返回`0`。 2. `daysInMonth`函数:根据年份和月份返回该月的天数。如果是闰年的2月,返回29天,否则返回该月正常的天数。 3. `Days`函数:计算从公元1年1月1日到指定日期的总天数。先计算完整年份的天数,再计算当前年份中到指定月份之前的天数,最后加上指定日期的天数。 4. `days2Date`函数:根据从公元1年1月1日开始历经的天数计算对应的日期。先逐年减去完整年份的天数,再逐月减去该月的天数,直到剩余天数小于该月的天数,此时剩余天数即为该月的日期。 5. `_DateG`函数:将输入的字符串(格式为`YYYY-MM-DD`)转换为`DateG`结构体。 6. `Add`函数:将指定日期加上指定的天数,调用`Days`函数计算总天数,再调用`days2Date`函数计算新的日期。 7. `Date2string`函数:将`DateG`结构体转换为字符串(格式为`YYYY-MM-DD`)。 8. `main`函数:读取输入的日期,计算60年后的日期、退休前倒计时1000天的日期以及到2020年1月23日的年龄,并输出结果。 ######[AI问答 | 1203点数解答 | 2025-12-12 13:24:03]
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)345
- C++ 代码:从儒略日精准计算公历日期及复杂度分析(字节豆包 | 1156点数解答 | 2026-02-08 19:36:11)33
- 2024 STEMA T6:解密T1188超难密码锁,Python区间DP解法揭秘!(字节豆包 | 718点数解答 | 2025-11-08 22:09:01)93
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)313
- 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
- 双11前小明巧用数据:计算机械键盘与游戏鼠标最近购买均价!(字节豆包 | 32点数解答 | 2024-10-17 20:43:03)211
- 云南 8 日摄影行程表 HTML 代码优化与逻辑注意点揭秘(字节豆包 | 217点数解答 | 2025-03-09 13:19:03)264
- 新员工7月业绩冲刺计划书:15天填补28000元缺口,附Word转换指南!(DeepSeek | 233点数解答 | 2025-07-16 18:48:05)110
- 新员工业绩达标计划书:完善方案待店长审批,多维度优化冲刺7月目标!(字节豆包 | 500点数解答 | 2025-07-16 18:50:16)98
- 2025版PHP项目资料管理系统需求文档:全方位打造高效协作与安全存储平台 (字节豆包 | 1332点数解答 | 2025-07-18 10:36:24)154