c语言编程:1.当输入年时,能显示那一年的台历 2.当输入年月时,能输出这个月的台历 3.输入年月日,能输出这天是这年的第几周和第几天,是否是农历中国传统的节日,如果是,是什么节日。
时间: 2023-12-08 10:04:49 浏览: 158
以下是C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 判断是否是闰年
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 1;
}
return 0;
}
// 获取某年某月的天数
int getDaysOfMonth(int year, int month) {
int days;
switch (month) {
case 2:
days = isLeapYear(year) ? 29 : 28;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
days = 31;
break;
}
return days;
}
// 获取某年某月1号是星期几(0表示星期日,1表示星期一,以此类推)
int getWeekday(int year, int month) {
int days = 0;
for (int i = 1900; i < year; i++) {
days += isLeapYear(i) ? 366 : 365;
}
for (int i = 1; i < month; i++) {
days += getDaysOfMonth(year, i);
}
return (days + 1) % 7;
}
// 输出某年的台历
void printCalendar(int year) {
printf("=========================%d年=========================\n", year);
printf(" 一 二 三 四 五 六 日\n");
int weekday = getWeekday(year, 1);
for (int i = 0; i < weekday; i++) {
printf(" ");
}
for (int i = 1; i <= 12; i++) {
int days = getDaysOfMonth(year, i);
for (int j = 1; j <= days; j++) {
printf("%4d ", j);
weekday++;
if (weekday == 7) {
printf("\n");
weekday = 0;
}
}
if (weekday != 0) {
printf("\n");
}
}
}
// 输出某年某月的台历
void printCalendarOfMonth(int year, int month) {
printf("===================%d年%d月===================\n", year, month);
printf(" 一 二 三 四 五 六 日\n");
int weekday = getWeekday(year, month);
for (int i = 0; i < weekday; i++) {
printf(" ");
}
int days = getDaysOfMonth(year, month);
for (int i = 1; i <= days; i++) {
printf("%4d ", i);
weekday++;
if (weekday == 7) {
printf("\n");
weekday = 0;
}
}
if (weekday != 0) {
printf("\n");
}
}
// 判断是否是农历传统节日
int isTraditionFestival(int month, int day) {
char *festival[24] = {"元旦", "春节", "元宵", "清明", "劳动", "端午",
"中元", "中秋", "重阳", "寒衣", "小雪", "大雪",
"腊八", "小年", "除夕", "腊月二十三", "腊月二十四",
"腊月二十五", "腊月二十六", "腊月二十七", "腊月二十八",
"腊月二十九", "腊月三十", "春分", "秋分"};
int festivalMonth[24] = {1, 1, 1, 4, 5, 5, 7, 8, 9, 10, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 3, 9};
int festivalDay[24] = {1, 1, 15, 4, 1, 5, 15, 15, 9, 1, 7, 22, 8, 23, 30, 23, 24, 25, 26, 27, 28, 29, 20, 23};
for (int i = 0; i < 24; i++) {
if (month == festivalMonth[i] && day == festivalDay[i]) {
printf("是%s\n", festival[i]);
return 1;
}
}
return 0;
}
// 输出某年某月某日的信息
void printInfo(int year, int month, int day) {
// 判断这天是第几周
int days = 0;
for (int i = 1; i < month; i++) {
days += getDaysOfMonth(year, i);
}
days += day;
int week = days % 7 == 0 ? days / 7 : days / 7 + 1;
printf("这天是第%d周,", week);
// 判断是否是农历节日
if (!isTraditionFestival(month, day)) {
printf("不是传统节日\n");
}
}
int main() {
int year, month, day;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份(或输入0返回年台历):");
scanf("%d", &month);
if (month == 0) {
printCalendar(year);
} else {
printf("请输入日期(或输入0返回月台历):");
scanf("%d", &day);
if (day == 0) {
printCalendarOfMonth(year, month);
} else {
printInfo(year, month, day);
}
}
system("pause");
return 0;
}
```
运行程序后,根据提示输入年月日信息即可输出对应的结果。
阅读全文