酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

用 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; } ``` 这个程序会打印出当前月份的日历,并带有边框。你可以根据需要进行修改和扩展。 [2024-11-02 20:25:02 | AI写代码神器 | 669点数解答]

相关提问