C语言编写程序显示单月的日历。用户指定这个月的天数和该月起始日是星期几: 31 3 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 31 输入 1、输入这个月的天数。 2、输入该月起始日是星期几(1=Sun,7=Stat)。 输出 输出单月的日历,日与日之间间隔一个空格,上下行日期右对齐。
时间: 2023-12-29 07:05:54 浏览: 163
好的,这是一个简单的单月日历程序,使用C语言实现,您可以在命令行中输入月份的天数和起始日来运行它:
```c
#include <stdio.h>
int main() {
int days_in_month, start_day;
printf("请输入这个月的天数:");
scanf("%d", &days_in_month);
printf("请输入该月起始日是星期几(1=Sun,7=Sat):");
scanf("%d", &start_day);
printf(" Mo Tu We Th Fr Sa Su\n");
// 计算第一行前面需要有多少空格
for (int i = 1; i < start_day; i++) {
printf(" ");
}
// 打印日历
int day = 1;
while (day <= days_in_month) {
// 每行打印七天
for (int i = start_day; i <= 7; i++) {
if (day <= days_in_month) {
printf("%4d", day);
day++;
} else {
break;
}
}
printf("\n");
start_day = 1;
}
return 0;
}
```
这个程序会输出一个类似于这样的日历:
```
Mo Tu We Th Fr Sa Su
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 31
```
请注意,程序中使用了 `%4d` 这个格式化字符串来保证输出的数字都是四个字符宽度,这样才能保证上下行日期右对齐。
阅读全文
相关推荐















