c语言实现万年历包含农历完整代码
时间: 2023-07-27 10:05:46 浏览: 142
以下是用C语言实现万年历并包含农历的完整代码:
```
#include <stdio.h>
#include <stdlib.h>
// 返回某年某月的天数
int get_month_days(int year, int month) {
int month_days[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
return 29;
}
return month_days[month - 1];
}
// 返回某年的总天数
int get_year_days(int year) {
int year_days = 0;
for (int i = 1; i <= 12; i++) {
year_days += get_month_days(year, i);
}
return year_days;
}
// 返回某年某月1号是星期几
int get_weekday(int year, int month) {
int days = 0;
for (int i = 1; i < year; i++) {
days += get_year_days(i);
}
for (int i = 1; i < month; i++) {
days += get_month_days(year, i);
}
days++;
return days % 7;
}
// 判断某年是否是闰年
int is_leap_year(int year) {
return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}
// 返回某年某月的农历月份
char* get_lunar_month(int year, int month) {
char* lunar_month[] = {
"正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"
};
int leap_month = get_leap_month(year);
if (leap_month > 0 && month > leap_month) {
month--;
}
return lunar_month[month - 1];
}
// 返回某年某月的农历日期
char* get_lunar_day(int day) {
char* lunar_day[] = {
"初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
"十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
"廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"
};
return lunar_day[day - 1];
}
// 返回某年的闰月月份,如果没有返回0
int get_leap_month(int year) {
int leap_month[] = {
0, 0x4bd8, 0x4ae0, 0xa570, 0x54d5, 0xd260, 0xd950, 0x6556, 0x56a0, 0x9ad0,
0x55d2, 0x4ae0, 0xa5b6, 0xa4d0, 0xd250, 0xd558, 0xb540, 0xb6a0, 0x95b5, 0x95b0,
0x49b0, 0x4978, 0xa4b0, 0xb4b8, 0x6a50, 0x6d40, 0xab60, 0x9575, 0x4af0, 0x4970
};
int leap = leap_month[year - 1900] & 0xf;
leap = (leap_month[year - 1900] >> 4) == leap ? 0 : leap;
return leap;
}
// 返回某年某月的农历天数
int get_lunar_days(int year, int month, int day) {
int lunar_days[] = {
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x06556, 0x056a0, 0x09ad0,
0x055d2, 0x04ae0, 0x0a4d0, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x095b5, 0x095b0,
0x049b0, 0x04978, 0x0a4b0, 0x0b4b8, 0x06a50, 0x06d40, 0x0ab60, 0x09575, 0x04af0,
0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab00, 0x0a8e0, 0x0ae50,
0x05acf, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a5, 0x095d0,
0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af40, 0x0ab60, 0x09570,
0x04af0, 0x0a4d0, 0x0d4d0, 0x0d250, 0x0d53c, 0x0b540, 0x0b6a0, 0x195a6, 0x095b0,
0x049b0, 0x0a4b0, 0x0b258, 0x06a20, 0x06d10, 0x0aea0, 0x0a6d0, 0x0a5d0, 0x052b0,
0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x055d9, 0x055b0, 0x049b0, 0x0a577, 0x0a4b0,
0x0aa50, 0x1b255, 0x06d20, 0x0ada0, 0x14b63, 0x09370, 0x04970, 0x064b0, 0x0d534,
0x0e9d5, 0x0d550, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x0d0b6,
0x0d25f, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a4b0, 0x0b4b5,
0x06a90, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x0d4a9,
0x0ea50, 0x06b58, 0x055c0, 0x0ab20, 0x0a930, 0x0d5a5, 0x0d4d0, 0x0d558, 0x0b540,
0x0b6a0, 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40,
0x0af40, 0x0ab60, 0x09570, 0x04af0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540,
0x0b5a0, 0x195a5, 0x095d0, 0x049b0, 0x0a7b0, 0x0a4b0, 0x0b4b7, 0x06a50, 0x06d40,
0x0ea50, 0x0da50, 0x05aa0, 0x076c6, 0x0a6d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6,
0x0d25f, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0,
0x0aa50, 0x1b255, 0x06d20, 0x0ada0, 0x14b63
};
int lunar_day = 0;
for (int i = 1; i < month; i++) {
lunar_day += get_month_days(year, i);
}
lunar_day += day - 1;
if (month > 2 && is_leap_year(year)) {
lunar_day++;
}
if (lunar_day >= lunar_days[year - 1900]) {
lunar_day = lunar_day - lunar_days[year - 1900] + 1;
if ((lunar_days[year - 1900 + 1] & 0xf) == 0) {
lunar_day--;
}
}
return lunar_day;
}
// 返回某年某月某日的农历日期
void get_lunar_date(int year, int month, int day, char* lunar_date) {
int lunar_year = 1900;
int lunar_month = 1;
int lunar_day = 1;
int leap_month = get_leap_month(year);
int days = 0;
while (1) {
int year_days = get_year_days(lunar_year);
if (days + year_days > day) {
break;
}
days += year_days;
lunar_year++;
}
int months = 0;
for (int i = 1; i <= 12; i++) {
int month_days = get_month_days(lunar_year, i);
if (i == leap_month + 1) {
month_days += get_month_days(lunar_year, i - 1);
}
if (days + month_days > day) {
lunar_month = i;
lunar_day = day - days + 1;
break;
}
days += month_days;
months++;
}
sprintf(lunar_date, "%d年%s%s", lunar_year, get_lunar_month(lunar_year, lunar_month), get_lunar_day(lunar_day));
if (leap_month > 0 && lunar_month > leap_month) {
sprintf(lunar_date, "%s(闰)", lunar_date);
}
}
// 打印某年某月的日历
void print_calendar(int year, int month) {
printf(" %d年%d月\n", year, month);
printf("日 一 二 三 四 五 六\n");
int weekday = get_weekday(year, month);
int month_days = get_month_days(year, month);
for (int i = 0; i < weekday; i++) {
printf(" ");
}
for (int i = 1; i <= month_days; i++) {
if (i == 1 || weekday == 0) {
printf("\n");
}
printf("%2d ", i);
weekday++;
if (weekday == 7) {
weekday = 0;
}
}
printf("\n");
}
int main() {
int year, month, day;
printf("请输入日期(yyyy-mm-dd): ");
scanf("%d-%d-%d", &year, &month, &day);
char lunar_date[20];
get_lunar_date(year, month, day, lunar_date);
printf("农历日期: %s\n", lunar_date);
print_calendar(year, month);
return 0;
}
```
注意:此代码仅为参考,可能存在不足之处,如有需要请自行进行修改和优化。
阅读全文