用c++语言写一段程序,能够打印某一年某一月的日历
时间: 2024-05-07 21:17:05 浏览: 17
好的,以下是用 C 语言编写的打印某一年某一月日历的程序:
```
#include <stdio.h>
int main()
{
int year, month, days, start, i, j;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
// 计算这个月有多少天
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
days = 31;
else if (month == 4 || month == 6 || month == 9 || month == 11)
days = 30;
else if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
days = 29;
else
days = 28;
// 计算这个月的第一天是星期几(0为星期日,1为星期一,以此类推)
if (month == 1 || month == 2) {
month += 12;
year--;
}
start = (1 + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
// 打印日历表头
printf(" %d年%d月\n", year, month - 12);
printf(" 日 一 二 三 四 五 六\n");
// 打印日期
for (i = 0; i < start; i++)
printf(" ");
for (j = 1; j <= days; j++) {
printf("%2d ", j);
if ((j + start) % 7 == 0)
printf("\n");
}
return 0;
}
```
输入年份和月份后,程序会输出格式化的日历。例如,输入2022和9,则会输出:
```
2022年9月
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
```
相关推荐
![](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)
![](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)