c语言打印月历
时间: 2023-07-02 16:09:41 浏览: 164
以下是一个简单的 C 语言程序,可以打印出指定年份和月份的月历:
```
#include <stdio.h>
int main()
{
int year, month, days, start;
printf("请输入年份和月份(格式:yyyy mm):\n");
scanf("%d %d", &year, &month);
// 计算该月有多少天和第一天是星期几
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
days = 29;
else
days = 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
start = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1;
for (int i = 1; i < month; i++) {
if (i == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
start += 29;
else
start += 28;
} else if (i == 4 || i == 6 || i == 9 || i == 11) {
start += 30;
} else {
start += 31;
}
}
start = start % 7;
// 打印月历
printf(" %d年%d月\n", year, month);
printf(" Su Mo Tu We Th Fr Sa\n");
for (int i = 0; i < start; i++) {
printf(" ");
}
for (int i = 1; i <= days; i++) {
printf("%3d", i);
if ((start + i) % 7 == 0) {
printf("\n");
}
}
if ((start + days) % 7 != 0) {
printf("\n");
}
return 0;
}
```
注意事项:
1. 该程序只能处理公历,且在 1582 年 10 月 4 日之后的日期才能正确计算。
2. 该程序中年份输入时请使用四位数,如 2021。
3. 该程序中月份输入时请使用 1 至 12 的数字,如 3 表示三月。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.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://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)