基于c语言的台历,输入年月日,输出这一天是这一年的第几周的第几天,是否为传统节日,如果是,是什么节日,不要参考思路,要代码
时间: 2024-05-14 22:15:21 浏览: 155
好的,以下是基于 C 语言的台历程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool is_leap_year(int year) {
// 判断是否为闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}
int get_days_of_month(int year, int month) {
// 获取某个月的天数
int days;
switch (month) {
case 2:
if (is_leap_year(year)) {
days = 29;
} else {
days = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
days = 31;
break;
}
return days;
}
int get_weekday(int year, int month, int day) {
// 获取某一天是星期几
if (month == 1 || month == 2) {
month += 12;
year--;
}
int c = year / 100;
year %= 100;
int week = year + year / 4 + c / 4 - 2 * c + 26 * (month + 1) / 10 + day - 1;
week = (week % 7 + 7) % 7;
return week;
}
bool is_traditional_festival(int month, int day) {
// 判断是否为传统节日
if (month == 1 && day == 1) {
printf("元旦节\n");
return true;
} else if (month == 2 && day == 14) {
printf("情人节\n");
return true;
} else if (month == 3 && day == 8) {
printf("妇女节\n");
return true;
} else if (month == 5 && day == 1) {
printf("劳动节\n");
return true;
} else if (month == 6 && day == 1) {
printf("儿童节\n");
return true;
} else if (month == 8 && day == 1) {
printf("建军节\n");
return true;
} else if (month == 9 && day == 10) {
printf("教师节\n");
return true;
} else if (month == 10 && day == 1) {
printf("国庆节\n");
return true;
} else if (month == 12 && day == 25) {
printf("圣诞节\n");
return true;
} else {
return false;
}
}
void print_calendar(int year, int month, int day) {
// 输出结果
int days_of_month = get_days_of_month(year, month);
int weekday = get_weekday(year, month, 1);
int week_of_year = (weekday + day - 1) / 7 + 1;
printf("%d年%d月%d日是%d年的第%d周的第%d天,", year, month, day, year, week_of_year, weekday + 1);
if (is_traditional_festival(month, day)) {
// 如果是传统节日,输出节日名称
} else {
printf("不是传统节日");
}
}
int main() {
int year, month, day;
printf("请输入年月日(格式:yyyy-mm-dd):");
scanf("%d-%d-%d", &year, &month, &day);
print_calendar(year, month, day);
return 0;
}
```
这个程序会先让用户输入年月日,然后计算出这一天是这一年的第几周的第几天,并判断是否为传统节日。
阅读全文