以下是使用C语言实现农历万年历的完整代码,包括公历转农历、查询指定年月的节气、查询指定日期的节日等功能。
```c
#include <stdio.h>
// 农历数据(1901年-2100年)
int lunarInfo[] = {
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, // 1901-1910
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, // 1911-1920
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, // 1921-1930
0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, // 1931-1940
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, // 1941-1950
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, // 1951-1960
0x0ae50, 0x0ab54, 0x04b60, 0x0aae0, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, // 1961-1970
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, // 1971-1980
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, // 1981-1990
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, // 1991-2000
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, // 2001-2010
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, // 2011-2020
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, // 2021-2030
0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, // 2031-2040
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, // 2041-2050
0x14b63, 0x09370, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0, 0x092e0, // 2051-2060
0x0d2e3, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4, 0x052d0, // 2061-2070
0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba0, 0x04b60, 0x0aae4, 0x0a570, // 2071-2080
0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x1d0b4, // 2081-2090
0x0d258, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, // 2091-2100
};
// 公历转农历
void solarToLunar(int year, int month, int day, int *lunarYear, int *lunarMonth, int *lunarDay) {
int leap = 0, temp = 0, offset = 0;
int i;
for (i = 1901; i <= year; i++) {
temp = lunarInfo[i - 1901];
leap = temp & 0xf;
temp >>= 4;
if (year == i) {
break;
}
}
offset = (year - 1901) * 365 + (year - 1901) / 4 + day - 1 + temp;
if (month <= 2) {
offset--;
}
while (offset >= lunarInfo[year - 1901] & 0xfff) {
offset -= lunarInfo[year - 1901] & 0xfff;
year++;
temp = lunarInfo[year - 1901];
leap = temp & 0xf;
temp >>= 4;
}
*lunarYear = year;
*lunarMonth = 1;
while (offset >= 29 + (temp & 0x1) && *lunarMonth <= 12) {
offset -= 29 + (temp & 0x1);
(*lunarMonth)++;
temp >>= 1;
}
*lunarDay = offset + 1;
if (leap == *lunarMonth) {
*lunarMonth += 100;
}
}
// 查找指定年月的节气
char *findSolarTerm(int year, int month) {
static char *solarTerm[] = {
"小寒", "大寒", "立春", "雨水", "惊蛰", "春分",
"清明", "谷雨", "立夏", "小满", "芒种", "夏至",
"小暑", "大暑", "立秋", "处暑", "白露", "秋分",
"寒露", "霜降", "立冬", "小雪", "大雪", "冬至"
};
int solarTerms[] = {3, 18, 4, 19, 5, 20, 6, 21, 7, 22, 8, 23, 9, 24, 8, 23, 8, 23, 7, 22, 7, 22, 6, 21, 6, 21};
int index = (month - 1) * 2 + (year % 4 == 0 && month > 2 ? 1 : 0);
return solarTerm[solarTerms[index] - 1];
}
// 查找指定日期的节日
char *findFestival(int year, int month, int day) {
static struct {
int month;
int day;
char *name;
} festivals[] = {
{1, 1, "元旦"},
{2, 14, "情人节"},
{3, 8, "妇女节"},
{3, 12, "植树节"},
{4, 1, "愚人节"},
{5, 1, "劳动节"},
{5, 4, "青年节"},
{6, 1, "儿童节"},
{7, 1, "建党节"},
{8, 1, "建军节"},
{9, 10, "教师节"},
{10, 1, "国庆节"},
{12, 24, "平安夜"},
{12, 25, "圣诞节"}
};
int i;
for (i = 0; i < sizeof(festivals) / sizeof(festivals[0]); i++) {
if (festivals[i].month == month && festivals[i].day == day) {
return festivals[i].name;
}
}
return NULL;
}
int main() {
int year, month, day;
int lunarYear, lunarMonth, lunarDay;
char *solarTerm, *festival;
printf("请输入公历年月日(格式:yyyy-mm-dd):");
scanf("%d-%d-%d", &year, &month, &day);
solarToLunar(year, month, day, &lunarYear, &lunarMonth, &lunarDay);
printf("农历:%d年%d月%d日\n", lunarYear, lunarMonth, lunarDay);
solarTerm = findSolarTerm(year, month);
if (solarTerm != NULL) {
printf("节气:%s\n", solarTerm);
}
festival = findFestival(year, month, day);
if (festival != NULL) {
printf("节日:%s\n", festival);
}
return 0;
}
```
使用方法:
1. 编译运行上述代码;
2. 输入公历年月日,例如:2022-01-01;
3. 程序将输出对应的农历日期、节气和节日。