公历转农历 1901-2100

 

好的,以下是C语言实现农历算法的示例代码: ```c #include <stdio.h> const char* gan[] = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}; const char* zhi[] = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}; const char* animal[] = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"}; int lunar_year(int year); int lunar_month(int year, int month, int is_leap); int lunar_day(int year, int month, int day); int main() { int year, month, day; printf("请输入公历日期(格式: 月 日):\n"); scanf("%d %d %d", &year, &month, &day); int lunarYear, lunarMonth, lunarDay; lunarYear = lunar_year(year); lunarMonth = lunar_month(year, month, 0); lunarDay = lunar_day(year, month, day); printf("农历: %s%s %s%s月 %s%s日", gan[(lunarYear - 4) % 10], zhi[(lunarYear - 4) % 12], lunarMonth < 1 ? "闰" : "", lunarMonth < 1 ? zhi[-lunarMonth - 1] : zhi[lunarMonth - 1], lunarDay < 11 ? "初" : (lunarDay < 20 ? "十" : (lunarDay == 20 ? "二十" : (lunarDay < 30 ? "廿" : "三十"))), lunarDay % 10 == 0 ? "" : (lunarDay < 20 ? zhi[lunarDay % 10 - 1] : zhi[lunarDay % 10 - 1])); return 0; } int lunar_year(int year) { int i, leap = 0; for (i = 1901; i <= 2100; i++) { if (year == i) break; } int k = (i - 1901 + 12) % 60; if (k < 0) k += 60; if (k == 0 || k == 25 || k == 50) leap = 1; return k < 0 ? 0 : ((k < 11 ? 1 : 0) * (k - 1) % 10 + (k >= 11 ? 1 : 0) * k % 10 + 1 + leap); } int lunar_month(int year, int month, int is_leap) { int i, leap = 0; for (i = 1901; i <= year; i++) { if (lunar_year(i) == 0) leap = 1; } int k = (((year - 1901) * 12 + month + 12) % 60 + 60) % 60; if (is_leap && k == lunar_year(year) * 12 + month + 12) is_leap = 0; if (is_leap && k >= lunar_year(year) * 12) k--; int realMonth; if (k < lunar_year(year) * 12) realMonth = k % 12 + 1; else realMonth = (k + 1) % 12 + 1; return is_leap ? -realMonth : realMonth; } int lunar_day(int year, int month, int day) { int i, leap = 0; for (i = 1901; i <= year; i++) { if (lunar_year(i) == 0) leap = 1; } int k = (((year - 1901) * 12 + month + 12) % 60 + 60) % 60; int days = (lunar_year(year) - 1) * 29 + k * 29 / 12; days += day - 1; days += 5 + leap; for (i = 1; i <= 13; i++) { int lMonth = lunar_month(year, i, 0); if (days < lMonth) break; days -= lMonth; } return days + 1; } ``` 该代码实现了从公历日期到农历日期的换。其中gan,zhi和animal数组分别存储了干支和生肖的名称,lunar_year函数计算某农历份,lunar_month函数计算某某月的农历月份,lunar_day函数计算某某月某日的农历日份。在main函数中,程序首先读入公历日期,然后调用上述三个函数来求出对应的农历日期,最后按指定格式输出结果。 请注意,由于历法计算复杂,该算法可能不完全准确。同时,该算法只适用19012100之间的时间范围。如果需要计算其他时间段的农历日期,可以参考相关文献中的其他算法来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值