用c语言编写万年历代码,要求上下有边框,1月2月在一行
时间: 2024-03-16 18:43:42 浏览: 58
好的,下面是修改后的代码,可以实现输入年月日,然后输出该月的日历,并且上下有边框,1月和2月在同一行:
```c
#include <stdio.h>
int main()
{
int year, month, day, days, i, leap;
int month_day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char week[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// 输入年月日
printf("Please input the date (year/month/day): ");
scanf("%d/%d/%d", &year, &month, &day);
// 计算天数
days = 0;
for (i = 1; i < month; i++)
{
days += month_day[i];
}
days += day;
// 判断是否为闰年
leap = 0;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
leap = 1;
month_day[2] = 29;
}
// 计算该年的1月1日是星期几
int y = year - 1;
int w = y + y / 4 - y / 100 + y / 400 + 1;
int week_day = w % 7;
// 输出结果
printf("\n");
printf("+-----------------------------+\n");
printf("| %d-%d |\n", year, month);
printf("+-----------------------------+\n");
printf("| Sun Mon Tue Wed Thu Fri Sat |\n");
printf("+-----------------------------+\n");
// 输出日历
int k = 0;
for (i = 0; i < week_day; i++)
{
printf("| ");
k++;
}
for (i = 1; i <= month_day[month]; i++)
{
printf("|%3d ", i);
week_day++;
k++;
if (week_day % 7 == 0)
{
printf("|\n");
printf("+-----------------------------+\n");
if (i != month_day[month])
{
if (month == 1)
{
printf("| %d-%d |\n", year, 2);
}
else
{
printf("| %d-%d |\n", year, month + 1);
}
printf("+-----------------------------+\n");
printf("| Sun Mon Tue Wed Thu Fri Sat |\n");
printf("+-----------------------------+\n");
k = 0;
}
}
}
// 输出该日是星期几
printf("\n%s\n", week[days % 7]);
printf("+-----------------------------+\n");
return 0;
}
```
这个代码可以实现输入年月日,然后输出该月的日历,并且上下有边框,1月和2月在同一行。
阅读全文