6-14 万年历显示函数 分数 15 作者 吕淑琴 单位 武汉理工大学 设计一个万年历,当用户输入年份和月份时,显示这个月的日历表。程序重点是这个月的第一天是星期几和这个月有几天,有了这两个值,只需通过排列,就可以显示这个日历。程序要求用户输入的年份是从1900年开始,已知1900年1月1日是星期一。 日历中每个具体的日期占5个字符宽度,右对齐,上下的分隔线分别是由35个’*’ 连字符构成,表示星期的字符是三/四个字符,加两/一个空格分隔。 (Design a perpetual calendar that displays the calendar for a specific month when the user enters the year and month. The key points of the program are which day of the week the first day of the month is and how many days there are in this month. With these two values, the calendar can be displayed simply by arranging the dates. The program requires that the year entered by the user starts from 1900. It is known that January 1st, 1900, is Monday. Each specific date in the calendar occupies a width of 5 characters and is right-aligned. The upper and lower dividing lines are each composed of 35 consecutive '*' characters. The characters representing the days of the week are three or four characters long, separated by two or one space.) 函数接口定义: void ShowDate(int y, int m); 其中,参数y是给定年,参数m是给定月,函数功能是根据给定的年月,显示该月日历。 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> int IsLeapYear(int y); /*判断是否闰年,细节不表*/ int GetDaysofMonth(int y, int m); /*确定某个月的天数,细节不表*/ int GetTotalDays(int y, int m); /*计算从1900年1月开始到给定年月间的天数,不包含给定月的天数,细节不表*/ int GetFirstDayInTable(int y, int m); /*计算给定年月的第一天在日历表中的位置,返回值为0对应Sun位置,返回值为6对应Sta位置,细节不表*/ void ShowDate(int y, int m); /*显示日历*/ int main() { int y,m; scanf("%d%d",&y,&m); ShowDate(y,m); return 0; } /* 请在这里填写答案 */ 输入样例1: 1949 10 输出样例1: *********************************** Sun Mon Tue Wen Thur Fri Sta 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 *********************************** 输入样例2: 2023 9 输出样例2: *********************************** Sun Mon Tue Wen Thur Fri Sta 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 *********************************** 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB C (gcc) Selection deleted 1 C语音
时间: 2025-07-01 16:57:14 浏览: 17
### 万年历显示函数的实现
为了实现一个能够格式化输出指定年月日历的 C 函数,需要关注以下几个关键点:
#### 判断闰年
判断某一年是否为闰年的逻辑是:能被 4 整除但不能被 100 整除,或者能被 400 整除的年份为闰年。
```c
int IsLeapYear(int y) {
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
```
#### 获取某月的天数
根据月份确定该月的总天数。如果是 2 月,则根据是否为闰年返回 28 或 29 天;对于 4、6、9、11 月则返回 30 天;其余月份返回 31 天。
```c
int GetDaysofMonth(int y, int m) {
if (m == 2)
return IsLeapYear(y) ? 29 : 28;
else if (m == 4 || m == 6 || m == 9 || m == 11)
return 30;
else
return 31;
}
```
#### 计算从 1900 年 1 月到目标年月的总天数
通过逐年累加每年的天数和逐月累加每月的天数,计算从 1900 年 1 月到目标日期的总天数。
```c
int GetTotalDays(int y, int m) {
int total = 0;
for (int year = 1900; year < y; year++)
total += IsLeapYear(year) ? 366 : 365;
for (int month = 1; month < m; month++)
total += GetDaysofMonth(y, month);
return total;
}
```
#### 确定该月第一天在日历表中的位置
以 1900 年 1 月 1 日为星期一为基础,通过总天数对 7 取余,得到该月第一天对应的星期几(0 表示星期日)。
```c
int GetFirstDayInTable(int y, int m) {
int total_days = GetTotalDays(y, m);
return (total_days + 1) % 7;
}
```
#### 格式化输出日历函数
按照要求,每个日期占 5 个字符宽度,右对齐;星期标题使用三/四个字符表示;顶部与底部使用 35 个星号作为分隔线。
```c
void ShowDate(int y, int m) {
printf("***********************************\n");
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
int first_day = GetFirstDayInTable(y, m);
int days = GetDaysofMonth(y, m);
for (int i = 0; i < first_day; i++)
printf(" ");
for (int day = 1; day <= days; day++) {
printf("%5d", day);
if ((first_day + day) % 7 == 0)
printf("\n");
}
printf("\n***********************************\n");
}
```
---
###
阅读全文
相关推荐














