c语言万年历源代码csdn,用C语言编写万年历,可以阳历和阴历转换?谢了
时间: 2024-03-23 18:43:02 浏览: 267
以下是一个简单的 C 语言万年历源代码:
```C
#include <stdio.h>
// 判断闰年
int isLeapYear(int year) {
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
// 计算某年某月的天数
int getDaysOfMonth(int year, int month) {
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year) && month == 2) {
return 29;
} else {
return days[month];
}
}
// 计算某年某月某日是星期几
int getWeekday(int year, int month, int day) {
if (month == 1 || month == 2) {
month += 12;
year--;
}
int century = year / 100;
year %= 100;
int week = year + year / 4 + century / 4 - 2 * century + 26 * (month + 1) / 10 + day - 1;
week = (week % 7 + 7) % 7;
return week;
}
// 打印某年某月的日历
void printCalendar(int year, int month) {
printf(" %d年%d月\n", year, month);
printf("日 一 二 三 四 五 六\n");
int weekday = getWeekday(year, month, 1);
int days = getDaysOfMonth(year, month);
int i, j;
for (i = 0; i < weekday; i++) {
printf(" ");
}
for (j = 1; j <= days; j++) {
printf("%2d ", j);
if ((j + weekday) % 7 == 0) {
printf("\n");
}
}
printf("\n");
}
int main() {
int year, month;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printCalendar(year, month);
return 0;
}
```
这个万年历可以输入年份和月份,然后输出该月的日历。你可以根据需要进行扩展,实现阳历和阴历的转换。
阅读全文