c语言实现万年历系统
时间: 2024-06-14 19:05:04 浏览: 63
以下是C语言实现万年历系统的代码示例:
```c
#include <stdio.h>
// 判断是否为闰年
int isLeapYear(int year {
if ((year % 4 ==0 && year % 100 != 0) year % 400 == 0) {
1 } else {
return 0;
}
// 获取某某的天数
int getMonthDays(int year, int month) {
int days;
if (month == 2) {
if (isLeapYear(year)) {
days = 29;
} else {
days = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
return days;
}
// 获取某年某月的第一天是星期几
int getFirstDayOfWeek(int year, int month) {
int dayOfWeek;
int totalDays = 0;
for (int i = 1900; i < year; i++) {
totalDays += isLeapYear(i) ? 366 : 365;
}
for (int i = 1; i < month; i++) {
totalDays += getMonthDays(year, i);
}
dayOfWeek = (totalDays + 1) % 7;
return dayOfWeek;
}
// 打印日历
void printCalendar(int year, int month) {
int firstDayOfWeek = getFirstDayOfWeek(year, month);
int monthDays = getMonthDays(year, month);
printf(" %d年%d月\n", year, month);
printf("日 一 二 三 四 五 六\n");
for (int i = 0; i < firstDayOfWeek; i++) {
printf(" ");
}
for (int i = 1; i <= monthDays; i++) {
printf("%2d ", i);
if ((firstDayOfWeek + i) % 7 == 0) {
printf("\n");
}
}
printf("\n");
}
int main() {
int year, month;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printCalendar(year, month);
return 0;
}
```
阅读全文