下面是一个简单的 C 语言日程安排程序的示例。该程序支持添加、删除和修改日程安排,并可以根据日期查看日程。它可以按周或月查看日程安排的概览。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_EVENTS 100 #define MAX_DESC 256 typedef struct { int year; int month; int day; char description[MAX_DESC]; } Event; Event events[MAX_EVENTS]; int eventCount = 0; // 添加事件 void addEvent(int year, int month, int day, const char *description) { if (eventCount < MAX_EVENTS) { events[eventCount].year = year; events[eventCount].month = month; events[eventCount].day = day; strncpy(events[eventCount].description, description, MAX_DESC); eventCount++; printf("事件添加成功!\n"); } else { printf("事件数量已达到上限!\n"); } } // 删除事件 void deleteEvent(int index) { if (index >= 0 && index < eventCount) { for (int i = index; i < eventCount - 1; i++) { events[i] = events[i + 1]; } eventCount--; printf("事件删除成功!\n"); } else { printf("无效的事件索引!\n"); } } // 修改事件 void modifyEvent(int index, int year, int month, int day, const char *description) { if (index >= 0 && index < eventCount) { events[index].year = year; events[index].month = month; events[index].day = day; strncpy(events[index].description, description, MAX_DESC); printf("事件修改成功!\n"); } else { printf("无效的事件索引!\n"); } } // 显示当天的事件 void showEvents(int year, int month, int day) { int found = 0; printf("事件列表(%d-%02d-%02d):\n", year, month, day); for (int i = 0; i < eventCount; i++) { if (events[i].year == year && events[i].month == month && events[i].day == day) { printf(" [%d] %s\n", i, events[i].description); found = 1; } } if (!found) { printf("今天没有事件。\n"); } } // 查看一周事件概览 void showWeekOverview(int year, int month, int day) { printf("一周事件概览(%d-%02d-%02d 到 %d-%02d-%02d):\n", year, month, day, year, month, day + 6); for (int d = 0; d < 7; d++) { showEvents(year, month, day + d); } } // 查看一月事件概览 void showMonthOverview(int year, int month) { printf("本月事件概览(%d-%02d):\n", year, month); for (int d = 1; d <= 31; d++) { showEvents(year, month, d); } } int main() { int choice; while (1) { printf("\n日程安排程序\n"); printf("1. 添加事件\n"); printf("2. 删除事件\n"); printf("3. 修改事件\n"); printf("4. 显示当天事件\n"); printf("5. 查看一周事件概览\n"); printf("6. 查看一月事件概览\n"); printf("7. 退出\n"); printf("请选择操作: "); scanf("%d", &choice); if (choice == 7) { break; } int year, month, day; char description[MAX_DESC]; int index; switch (choice) { case 1: printf("输入事件日期 (年 月 日): "); scanf("%d %d %d", &year, &month, &day); printf("输入事件描述: "); scanf(" %[^\n]", description); addEvent(year, month, day, description); break; case 2: printf("输入要删除的事件索引: "); scanf("%d", &index); deleteEvent(index); break; case 3: printf("输入要修改的事件索引: "); scanf("%d", &index); printf("输入新的事件日期 (年 月 日): "); scanf("%d %d %d", &year, &month, &day); printf("输入新的事件描述: "); scanf(" %[^\n]", description); modifyEvent(index, year, month, day, description); break; case 4: printf("输入日期 (年 月 日): "); scanf("%d %d %d", &year, &month, &day); showEvents(year, month, day); break; case 5: printf("输入周的起始日期 (年 月 日): "); scanf("%d %d %d", &year, &month, &day); showWeekOverview(year, month, day); break; case 6: printf("输入月份 (年 月): "); scanf("%d %d", &year, &month); showMonthOverview(year, month); break; default: printf("无效的选项!\n"); break; } } return 0; } ``` 编译与运行 1. 将上面的代码复制到一个 `.c` 文件中,例如 `scheduler.c`。 2. 使用 GCC 编译器在终端中运行: ```bash gcc scheduler.c -o scheduler ``` 3. 然后运行程序: ```bash ./scheduler ``` 注意 该程序是一个简化版本,适合用作学习和娱乐。实际应用中,可以考虑使用更复杂的数据结构和持久化存储(如文件或数据库)来保存事件,此外没有处理日期的有效性,也未考虑跨月和跨年的日期处理。 [2024-11-09 15:49:56 | AI写代码神器 | 3268点数解答]