C 语言实现:带边框的精美日历打印代码揭秘
以下是一个使用 C 语言打印带有边框的日历的示例代码: ```c #include <stdio.h> #include <time.h> // 打印日历边框 void printBorder() { printf("+---------------------------+\n"); } // 打印日历头部 void printHeader(int month, int year) { char *months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; printf("| %s %d |\n", months[month - 1], year); printBorder(); printf("| Sun Mon Tue Wed Thu Fri Sat |\n"); printBorder(); } // 计算指定年月的第一天是星期几 int getFirstDay(int month, int year) { struct tm t; t.tm_year = year - 1900; t.tm_mon = month - 1; t.tm_mday = 1; mktime(&t); return t.tm_wday; } // 打印日历主体 void printCalendar(int month, int year) { int firstDay = getFirstDay(month, year); int daysInMonth; switch (month) { case 2: if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) { daysInMonth = 29; } else { daysInMonth = 28; } break; case 4: case 6: case 9: case 11: daysInMonth = 30; break; default: daysInMonth = 31; } int day = 1; int position = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (i == 0 && j < firstDay) { printf(" "); } else if (day <= daysInMonth) { printf("%4d", day); day++; } position++; if (position % 7 == 0) { printf(" |\n"); } } } printBorder(); } int main() { time_t t = time(NULL); struct tm *now = localtime(&t); int month = now->tm_mon + 1; int year = now->tm_year + 1900; printHeader(month, year); printCalendar(month, year); return 0; } ``` 这个程序会打印出当前月份的日历,并带有边框。你可以根据需要进行修改和扩展。 ######[AI写代码神器 | 669点数解答 | 2024-11-02 20:25:02]
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)380
- Python 实现:打印 -n 到 n 范围内所有整数的代码及详解(字节豆包 | 232点数解答 | 2025-12-07 12:30:06)92
- Python代码:轻松打印 0 到输入整数之间的所有整数(字节豆包 | 126点数解答 | 2025-12-12 18:21:54)63
- C++实现:输入整数打印 0 到该整数间所有整数及代码详解(字节豆包 | 228点数解答 | 2025-12-12 18:22:24)74
- Python实现:打印0到输入整数间所有整数的代码解析(字节豆包 | 264点数解答 | 2025-12-12 19:37:01)63
- Python 实现:打印 -n 到 n 范围内所有整数的代码及详解(字节豆包 | 214点数解答 | 2025-12-12 19:40:30)65
- Python实现:打印 0 到 n 之间所有整数的高效方法(讯飞星火 | 569点数解答 | 2025-12-17 12:23:47)78
- Python实现:打印从 -n 到 n 范围内所有整数的代码详解(讯飞星火 | 660点数解答 | 2025-12-17 17:30:53)48
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)350
- 用 JS 中 for 循环实现 1 到 100 相加并输出结果到页面的完整代码 ( | 240点数解答 | 2024-05-20 22:11:29)497
- 用 JS 的 while 循环实现 1 到 100 相加并输出到页面的代码揭秘( | 47点数解答 | 2024-05-20 22:16:48)386
- C++实现读取数字并输出最小值的代码及解释(字节豆包 | 113点数解答 | 2026-03-20 19:32:56)28