显示某一年的全年日历(每天要有对应的星期)。用c++写一下这个程序
时间: 2023-05-19 07:01:19 浏览: 90
C++编写的显示全年的日历。
非常感谢您的提问!以下是CSDN开发的C知道为您提供的代码:
```
#include <stdio.h>
int main()
{
int year, month, day, week, days_in_month;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
// 计算该月有多少天
if (month == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
days_in_month = 29;
}
else
{
days_in_month = 28;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
days_in_month = 30;
}
else
{
days_in_month = 31;
}
// 计算该月第一天是星期几
day = 1;
week = 0;
while (week < 7)
{
if (day > days_in_month)
{
break;
}
if (week == 0)
{
printf("日\t");
}
else if (week == 1)
{
printf("一\t");
}
else if (week == 2)
{
printf("二\t");
}
else if (week == 3)
{
printf("三\t");
}
else if (week == 4)
{
printf("四\t");
}
else if (week == 5)
{
printf("五\t");
}
else if (week == 6)
{
printf("六\t");
}
week++;
}
printf("\n");
// 输出日历
day = 1;
week = 0;
while (day <= days_in_month)
{
if (week == 0)
{
printf("%d\t", day);
}
else if (week == 6)
{
printf("%d\n", day);
}
else
{
printf("%d\t", day);
}
day++;
week++;
if (week == 7)
{
week = 0;
}
}
return 0;
}
```
希望这个程序能够满足您的需求!如果您还有其它问题,请随时提出。
阅读全文