编写一个完整的日历表需要处理许多细节,包括公历和农历之间的转换、节气、闰年等。下面提供一个简单的C语言程序,用于生成指定年份的日历表:
#include <stdio.h>
#define YEAR 2023
int is_leap_year(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
int get_days_in_month(int year, int month) {
static int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && is_leap_year(year)) {
return 29;
}
return days_in_month[month - 1];
}
void print_calendar(int year) {
printf(" January February March April May June July August September October November December\n");
for (int i = 1900; i < year; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
printf("+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+\n");
} else {
printf("| | | | | | | | | | |\n");
}
}
for (int i = 1; i <= 12; i++) {
int days = get_days_in_month(year, i);
printf("| %*d |", days, days);
if ((i + YEAR / 4 - YEAR / 100 + YEAR / 400) % 7 == 6 || (i + YEAR / 4 - YEAR / 100 + YEAR / 400) % 7 == 5) {
printf("\n");
} else {
printf("| | | | | | | | | | |\n");
}
}
printf("+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+\n");
}
int main() {
print_calendar(YEAR);
return 0;
}
运行程序后,会输出指定年份的日历表。注意,这个程序只是一个简单的示例,还有很多可以改进和扩展的地方,例如添加节气、节日等。