📅  最后修改于: 2023-12-03 15:30:15.710000             🧑  作者: Mango
本程序用于显示给定年份的逐月日历,用户可以在程序中输入年份,然后程序将输出该年份每个月的日历。程序采用了C语言编写,运行简单快捷。
#include <stdio.h>
#include <stdlib.h>
int days_in_month(int month, int year) {
int days = 31;
if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else if (month == 2) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
days = 29;
} else {
days = 28;
}
}
return days;
}
int day_of_week(int day, int month, int year) {
int adjustment, mm, yy;
adjustment = (14 - month) / 12;
mm = month + 12 * adjustment - 2;
yy = year - adjustment;
return (day + (13 * mm - 1) / 5 + yy + yy / 4 - yy / 100 + yy / 400) % 7;
}
void print_month(int month, int year) {
int days = days_in_month(month, year);
int start_day = day_of_week(1, month, year);
printf("\nSun Mon Tue Wed Thu Fri Sat\n");
for (int i = 0; i < start_day; i++) {
printf(" ");
}
for (int i = 1; i <= days; i++) {
printf("%3d ", i);
if ((i + start_day) % 7 == 0 || i == days) {
printf("\n");
}
}
}
void print_calendar(int year) {
char *month_name[] = {
"", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
for (int month = 1; month <= 12; month++) {
printf("\n %s %d\n", month_name[month], year);
print_month(month, year);
}
printf("\n");
}
int main() {
int year;
printf("Enter the year: ");
scanf("%d", &year);
print_calendar(year);
return 0;
}
本程序是一个简单的C语言应用程序,可供初学者练手。程序实现过程涉及到获取月份天数、根据日期计算星期几等基础概念,适合让学生先掌握这些基本概念,进而更好的了解C语言编程。