给我一份复杂万年历c语言编程代码
时间: 2023-08-11 07:07:56 浏览: 75
以下是一个较为复杂的万年历的 C 语言编程代码,它支持任意年份的万年历:
```c
#include <stdio.h>
int is_leap_year(int year);
int get_day_of_week(int year, int month, int day);
int get_days_in_month(int year, int month);
void print_month(int year, int month);
int main()
{
int year, month;
printf("请输入年份: ");
scanf("%d", &year);
printf("请输入月份: ");
scanf("%d", &month);
printf("\n");
// 输出当年的日历
for (int i = 1; i <= 12; i++)
{
print_month(year, i);
}
printf("\n");
// 输出当月的日历
print_month(year, month);
return 0;
}
// 判断是否是闰年
int is_leap_year(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
// 计算某一天是星期几
int get_day_of_week(int year, int month, int day)
{
int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
if (month < 3)
{
year--;
}
return (year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7;
}
// 获取某个月份的天数
int get_days_in_month(int year, int month)
{
int days_in_month[] = {31, 28 + is_leap_year(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return days_in_month[month - 1];
}
// 输出某个月份的日历
void print_month(int year, int month)
{
int days_in_month = get_days_in_month(year, month);
int first_day_of_month = get_day_of_week(year, month, 1);
printf("%d 年 %d 月\n", year, month);
printf("日\t一\t二\t三\t四\t五\t六\n");
for (int i = 0; i < first_day_of_month; i++)
{
printf("\t");
}
for (int i = 1; i <= days_in_month; i++)
{
printf("%d\t", i);
if ((first_day_of_month + i) % 7 == 0)
{
printf("\n");
}
}
if ((first_day_of_month + days_in_month) % 7 != 0)
{
printf("\n");
}
printf("\n");
}
```
程序中使用了一些数学公式和算法来计算某一天是星期几,某个月份的天数等信息。运行程序后,用户可以输入年份和月份,然后程序会输出当年和当月的日历。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)