下面是一个简单的C语言实现的志愿者服务活动管理系统的设计。这个系统包括菜单显示、信息录入、浏览、查询、删除和修改功能。代码中包含了详细的注释来解释每一步的操作。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define FILENAME "volunteer_data.txt" typedef struct { char name[50]; char phone[15]; char activity[100]; char location[100]; char content[200]; } Volunteer; void displayMenu() { printf("\n志愿者服务活动管理系统\n"); printf("1. 录入志愿者服务活动信息\n"); printf("2. 浏览志愿者服务活动信息\n"); printf("3. 查询志愿者服务活动信息\n"); printf("4. 删除志愿者服务活动信息\n"); printf("5. 修改志愿者服务活动信息\n"); printf("6. 退出系统\n"); printf("请选择操作: "); } void addVolunteer() { Volunteer vol; FILE *fp = fopen(FILENAME, "a"); if (fp == NULL) { printf("文件打开失败\n"); return; } printf("请输入志愿者姓名: "); scanf("%s", vol.name); printf("请输入志愿者电话: "); scanf("%s", vol.phone); printf("请输入服务活动名称: "); scanf("%s", vol.activity); printf("请输入服务活动地点: "); scanf("%s", vol.location); printf("请输入服务活动内容: "); scanf("%s", vol.content); fprintf(fp, "%s %s %s %s %s\n", vol.name, vol.phone, vol.activity, vol.location, vol.content); fclose(fp); printf("信息已保存\n"); } void viewVolunteers() { Volunteer vol; FILE *fp = fopen(FILENAME, "r"); if (fp == NULL) { printf("文件打开失败\n"); return; } while (fscanf(fp, "%s %s %s %s %s", vol.name, vol.phone, vol.activity, vol.location, vol.content) != EOF) { printf("姓名: %s, 电话: %s, 活动: %s, 地点: %s, 内容: %s\n", vol.name, vol.phone, vol.activity, vol.location, vol.content); } fclose(fp); } void searchVolunteer() { char searchTerm[50]; int choice; Volunteer vol; FILE *fp = fopen(FILENAME, "r"); if (fp == NULL) { printf("文件打开失败\n"); return; } printf("按姓名查询请输入1,按电话查询请输入2: "); scanf("%d", &choice); printf("请输入查询内容: "); scanf("%s", searchTerm); while (fscanf(fp, "%s %s %s %s %s", vol.name, vol.phone, vol.activity, vol.location, vol.content) != EOF) { if ((choice == 1 && strcmp(vol.name, searchTerm) == 0) || (choice == 2 && strcmp(vol.phone, searchTerm) == 0)) { printf("找到匹配信息: 姓名: %s, 电话: %s, 活动: %s, 地点: %s, 内容: %s\n", vol.name, vol.phone, vol.activity, vol.location, vol.content); fclose(fp); return; } } printf("未找到匹配信息\n"); fclose(fp); } void deleteVolunteer() { char searchTerm[50]; int choice; Volunteer vol; FILE *fp = fopen(FILENAME, "r"); FILE *temp = fopen("temp.txt", "w"); if (fp == NULL || temp == NULL) { printf("文件打开失败\n"); return; } printf("按姓名删除请输入1,按电话删除请输入2: "); scanf("%d", &choice); printf("请输入删除内容: "); scanf("%s", searchTerm); while (fscanf(fp, "%s %s %s %s %s", vol.name, vol.phone, vol.activity, vol.location, vol.content) != EOF) { if (!((choice == 1 && strcmp(vol.name, searchTerm) == 0) || (choice == 2 && strcmp(vol.phone, searchTerm) == 0))) { fprintf(temp, "%s %s %s %s %s\n", vol.name, vol.phone, vol.activity, vol.location, vol.content); } } fclose(fp); fclose(temp); remove(FILENAME); rename("temp.txt", FILENAME); printf("信息已删除\n"); } void modifyVolunteer() { char searchTerm[50]; int choice; Volunteer vol; FILE *fp = fopen(FILENAME, "r"); FILE *temp = fopen("temp.txt", "w"); if (fp == NULL || temp == NULL) { printf("文件打开失败\n"); return; } printf("按姓名修改请输入1,按电话修改请输入2: "); scanf("%d", &choice); printf("请输入修改内容: "); scanf("%s", searchTerm); while (fscanf(fp, "%s %s %s %s %s", vol.name, vol.phone, vol.activity, vol.location, vol.content) != EOF) { if ((choice == 1 && strcmp(vol.name, searchTerm) == 0) || (choice == 2 && strcmp(vol.phone, searchTerm) == 0)) { printf("请输入新的志愿者姓名: "); scanf("%s", vol.name); printf("请输入新的志愿者电话: "); scanf("%s", vol.phone); printf("请输入新的服务活动名称: "); scanf("%s", vol.activity); printf("请输入新的服务活动地点: "); scanf("%s", vol.location); printf("请输入新的服务活动内容: "); scanf("%s", vol.content); } fprintf(temp, "%s %s %s %s %s\n", vol.name, vol.phone, vol.activity, vol.location, vol.content); } fclose(fp); fclose(temp); remove(FILENAME); rename("temp.txt", FILENAME); printf("信息已修改\n"); } int main() { int choice; while (1) { displayMenu(); scanf("%d", &choice); switch (choice) { case 1: addVolunteer(); break; case 2: viewVolunteers(); break; case 3: searchVolunteer(); break; case 4: deleteVolunteer(); break; case 5: modifyVolunteer(); break; case 6: exit(0); default: printf("无效选择,请重新选择\n"); } } return 0; } ``` 代码解释: 1. 结构体定义:`Volunteer`结构体用于存储志愿者的信息。 2. 文件操作:使用文件`volunteer_data.txt`来存储志愿者信息。 3. 菜单显示:`displayMenu`函数显示系统菜单。 4. 信息录入:`addVolunteer`函数允许用户输入志愿者信息并保存到文件中。 5. 信息浏览:`viewVolunteers`函数读取并显示所有志愿者信息。 6. 信息查询:`searchVolunteer`函数允许用户按姓名或电话查询志愿者信息。 7. 信息删除:`deleteVolunteer`函数允许用户按姓名或电话删除志愿者信息。 8. 信息修改:`modifyVolunteer`函数允许用户按姓名或电话修改志愿者信息。 额外需要注意的逻辑点: 1. 文件操作时,确保文件正确打开和关闭,避免资源泄露。 2. 在删除和修改操作中,使用临时文件来更新数据,确保数据一致性。 3. 用户输入处理时,考虑输入错误的情况,增加输入验证以提高程序的健壮性。 [2025-03-03 20:55:46 | AI写代码神器 | 1768点数解答]