好的,下面是一个志愿者服务活动信息管理系统的C语言实现。这个系统包括菜单驱动的用户界面,可以录入、浏览、查询和修改志愿者服务活动信息。 代码实现 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_LEN 50 #define MAX_PHONE_LEN 15 #define MAX_ACTIVITY_NAME_LEN 50 #define MAX_LOCATION_LEN 100 #define MAX_CONTENT_LEN 200 #define FILE_NAME "volunteer_info.dat" typedef struct { char name[MAX_NAME_LEN]; char phone[MAX_PHONE_LEN]; char activityName[MAX_ACTIVITY_NAME_LEN]; char location[MAX_LOCATION_LEN]; char content[MAX_CONTENT_LEN]; } VolunteerInfo; void displayMenu(); void addVolunteerInfo(); void viewVolunteerInfo(); void searchVolunteerInfoByName(); void searchVolunteerInfoByPhone(); void deleteVolunteerInfo(); void modifyVolunteerInfo(); void loadData(VolunteerInfo *info, int *count); void saveData(VolunteerInfo *info, int count); int main() { int choice; do { displayMenu(); printf("请输入您的选择: "); scanf("%d", &choice); getchar(); // 清除缓冲区中的换行符 switch (choice) { case 1: addVolunteerInfo(); break; case 2: viewVolunteerInfo(); break; case 3: searchVolunteerInfoByName(); break; case 4: searchVolunteerInfoByPhone(); break; case 5: deleteVolunteerInfo(); break; case 6: modifyVolunteerInfo(); break; case 7: printf("退出系统\n"); break; default: printf("无效的选择,请重新输入\n"); } } while (choice != 7); return 0; } void displayMenu() { printf("\n=== 志愿者服务活动信息管理系统 ===\n"); printf("1. 录入志愿者服务活动信息\n"); printf("2. 浏览志愿者服务活动信息\n"); printf("3. 按志愿者姓名查询\n"); printf("4. 按手机号查询\n"); printf("5. 删除志愿者服务活动信息\n"); printf("6. 修改志愿者服务活动信息\n"); printf("7. 退出系统\n"); } void addVolunteerInfo() { VolunteerInfo info; printf("请输入志愿者姓名: "); fgets(info.name, MAX_NAME_LEN, stdin); info.name[strcspn(info.name, "\n")] = '\0'; // 去除换行符 printf("请输入志愿者电话: "); fgets(info.phone, MAX_PHONE_LEN, stdin); info.phone[strcspn(info.phone, "\n")] = '\0'; // 去除换行符 printf("请输入服务活动名称: "); fgets(info.activityName, MAX_ACTIVITY_NAME_LEN, stdin); info.activityName[strcspn(info.activityName, "\n")] = '\0'; // 去除换行符 printf("请输入地点: "); fgets(info.location, MAX_LOCATION_LEN, stdin); info.location[strcspn(info.location, "\n")] = '\0'; // 去除换行符 printf("请输入内容: "); fgets(info.content, MAX_CONTENT_LEN, stdin); info.content[strcspn(info.content, "\n")] = '\0'; // 去除换行符 FILE *file = fopen(FILE_NAME, "ab"); if (file == NULL) { perror("无法打开文件"); return; } fwrite(&info, sizeof(VolunteerInfo), 1, file); fclose(file); printf("信息录入成功\n"); } void viewVolunteerInfo() { FILE *file = fopen(FILE_NAME, "rb"); if (file == NULL) { perror("无法打开文件"); return; } VolunteerInfo info; while (fread(&info, sizeof(VolunteerInfo), 1, file)) { printf("\n志愿者姓名: %s\n", info.name); printf("志愿者电话: %s\n", info.phone); printf("服务活动名称: %s\n", info.activityName); printf("地点: %s\n", info.location); printf("内容: %s\n", info.content); } fclose(file); } void searchVolunteerInfoByName() { char name[MAX_NAME_LEN]; printf("请输入要查询的志愿者姓名: "); fgets(name, MAX_NAME_LEN, stdin); name[strcspn(name, "\n")] = '\0'; // 去除换行符 FILE *file = fopen(FILE_NAME, "rb"); if (file == NULL) { perror("无法打开文件"); return; } VolunteerInfo info; int found = 0; while (fread(&info, sizeof(VolunteerInfo), 1, file)) { if (strcmp(info.name, name) == 0) { printf("\n志愿者姓名: %s\n", info.name); printf("志愿者电话: %s\n", info.phone); printf("服务活动名称: %s\n", info.activityName); printf("地点: %s\n", info.location); printf("内容: %s\n", info.content); found = 1; } } if (!found) { printf("未找到匹配的志愿者信息\n"); } fclose(file); } void searchVolunteerInfoByPhone() { char phone[MAX_PHONE_LEN]; printf("请输入要查询的志愿者电话: "); fgets(phone, MAX_PHONE_LEN, stdin); phone[strcspn(phone, "\n")] = '\0'; // 去除换行符 FILE *file = fopen(FILE_NAME, "rb"); if (file == NULL) { perror("无法打开文件"); return; } VolunteerInfo info; int found = 0; while (fread(&info, sizeof(VolunteerInfo), 1, file)) { if (strcmp(info.phone, phone) == 0) { printf("\n志愿者姓名: %s\n", info.name); printf("志愿者电话: %s\n", info.phone); printf("服务活动名称: %s\n", info.activityName); printf("地点: %s\n", info.location); printf("内容: %s\n", info.content); found = 1; } } if (!found) { printf("未找到匹配的志愿者信息\n"); } fclose(file); } void deleteVolunteerInfo() { char name[MAX_NAME_LEN]; printf("请输入要删除的志愿者姓名: "); fgets(name, MAX_NAME_LEN, stdin); name[strcspn(name, "\n")] = '\0'; // 去除换行符 FILE *file = fopen(FILE_NAME, "rb"); if (file == NULL) { perror("无法打开文件"); return; } VolunteerInfo info; VolunteerInfo temp[100]; // 假设最多有100条记录,可以根据需要调整大小或动态分配内存 int count = 0; while (fread(&info, sizeof(VolunteerInfo), 1, file)) { if (strcmp(info.name, name) != 0) { temp [2025-03-03 21:04:32 | AI写代码神器 | 2048点数解答]