以下是基于给定需求的C语言志愿者服务活动管理系统代码,包括了详细的 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 100 // 定义最大记录数 // 定义结构体用于存储志愿服务信息 typedef struct { char name[50]; // 志愿者姓名 char phone[20]; // 电话号码 char activityName[100]; // 活动名称 char location[100]; // 地点 char content[256]; // 内容 } VolunteerInfo; // 函数声明 void menu(); void addVolunteer(VolunteerInfo volunteers[], int *count); void displayAll(VolunteerInfo volunteers[], int count); void searchByName(VolunteerInfo volunteers[], int count); void searchByPhone(VolunteerInfo volunteers[], int count); void deleteVolunteer(VolunteerInfo volunteers[], int *count, int index); void modifyVolunteer(VolunteerInfo volunteers[], int index); void saveToFile(VolunteerInfo volunteers[], int count); void loadFromFile(VolunteerInfo volunteers[], int *count); int main() { VolunteerInfo volunteers[MAX]; int count = 0; int choice; int exitFlag = 0; loadFromFile(volunteers, &count); // 加载已有数据 while (!exitFlag) { menu(); printf("请选择操作:"); scanf("%d", &choice); switch (choice) { case 1: addVolunteer(volunteers, &count); break; case 2: displayAll(volunteers, count); break; case 3: searchByName(volunteers, count); break; case 4: searchByPhone(volunteers, count); break; case 5: // 删除功能 break; case 6: // 修改功能 break; case 7: exitFlag = 1; saveToFile(volunteers, count); break; default: printf("无效选择,请重新输入。\n"); } } return 0; } // 显示菜单选项 void menu() { printf("\n志愿者服务活动管理系统\n"); printf("-----------------------------\n"); printf("1. 录入志愿服务信息\n"); printf("2. 浏览所有志愿服务信息\n"); printf("3. 按姓名查询志愿服务信息\n"); printf("4. 按手机号查询志愿服务信息\n"); printf("5. 删除志愿服务信息\n"); printf("6. 修改志愿服务信息\n"); printf("7. 退出系统\n"); printf("-----------------------------\n"); } // 添加新的志愿服务信息 void addVolunteer(VolunteerInfo volunteers[], int *count) { if (*count >= MAX) { printf("已达到最大记录数!\n"); return; } printf("请输入志愿者姓名:"); scanf("%s", volunteers[*count].name); printf("请输入电话:"); scanf("%s", volunteers[*count].phone); printf("请输入服务活动名称:"); scanf("%s", volunteers[*count].activityName); printf("请输入地点:"); scanf("%s", volunteers[*count].location); printf("请输入内容:"); scanf(" %[^\n]", volunteers[*count].content); (*count)++; printf("录入成功!\n"); } // 显示所有志愿服务信息 void displayAll(VolunteerInfo volunteers[], int count) { for (int i = 0; i < count; i++) { printf("序号:%d\n", i + 1); printf("姓名:%s\n", volunteers[i].name); printf("电话:%s\n", volunteers[i].phone); printf("活动名称:%s\n", volunteers[i].activityName); printf("地点:%s\n", volunteers[i].location); printf("内容:%s\n\n", volunteers[i].content); } } // 按姓名查询志愿服务信息 void searchByName(VolunteerInfo volunteers[], int count) { char name[50]; printf("请输入要查询的志愿者姓名:"); scanf("%s", name); for (int i = 0; i < count; i++) { if (strcmp(volunteers[i].name, name) == 0) { printf("找到匹配结果:\n"); printf("姓名:%s\n", volunteers[i].name); printf("电话:%s\n", volunteers[i].phone); printf("活动名称:%s\n", volunteers[i].activityName); printf("地点:%s\n", volunteers[i].location); printf("内容:%s\n\n", volunteers[i].content); return; } } printf("未找到匹配结果。\n"); } // 按手机号查询志愿服务信息 void searchByPhone(VolunteerInfo volunteers[], int count) { char phone[20]; printf("请输入要查询的手机号:"); scanf("%s", phone); for (int i = 0; i < count; i++) { if (strcmp(volunteers[i].phone, phone) == 0) { printf("找到匹配结果:\n"); printf("姓名:%s\n", volunteers[i].name); printf("电话:%s\n", volunteers[i].phone); printf("活动名称:%s\n", volunteers[i].activityName); printf("地点:%s\n", volunteers[i].location); printf("内容:%s\n\n", volunteers[i].content); return; } } printf("未找到匹配结果。\n"); } // 删除志愿服务信息 void deleteVolunteer(VolunteerInfo volunteers[], int *count, int index) { for (int i = index; i < *count - 1; i++) { volunteers[i] = volunteers[i + 1]; } (*count)--; } // 修改志愿服务信息 void modifyVolunteer(VolunteerInfo volunteers[], int index) { printf("修改前的信息:\n"); printf("姓名:%s\n", volunteers[index].name); printf("电话:%s\n", volunteers[index].phone); printf("活动名称:%s\n", volunteers[index].activityName); printf("地点:%s\n", volunteers[index].location); printf("内容:%s\n\n", volunteers[index].content); printf("请依次输入需要修改后的信息:\n"); printf("姓名:"); scanf("%s", volunteers[index].name); printf("电话:"); scanf("%s", volunteers[index].phone); printf("活动名称:"); scanf("%s", volunteers[index].activityName); printf("地点:"); scanf("%s", volunteers[index].location); printf("内容:"); scanf(" %[^\n]", volunteers[index].content); printf("修改成功!\n"); } // 将数据保存到文件 void saveToFile(VolunteerInfo volunteers[], int count) { FILE *file = fopen("volunteers.txt", "w"); if (file == NULL) { printf("文件打开失败!\n"); return; } for (int i = 0; i < count; i++) { fprintf(file, "%s %s %s %s %s\n", volunteers[i].name, volunteers[i].phone, volunteers[i].activityName, volunteers[i].location, volunteers[i].content); } fclose(file); } // 从文件加载数据 void loadFromFile(VolunteerInfo volunteers[], int *count) { FILE *file = fopen("volunteers.txt", "r"); if (file == NULL) { return; } *count = 0; while (fscanf(file, "%s %s %s %s %[^\n]", volunteers[*count].name, volunteers[*count].phone, volunteers[*count].activityName, volunteers[*count].location, volunteers[*count].content) != EOF) { (*count)++; } fclose(file); } ``` 三条额外需要注意的逻辑点: - 在添加新记录时检查是否超过数组容量。 - 文件读写操作需处理可能发生的错误情况。 - 查询功能区分大小写敏感性。 [2025-03-03 21:05:31 | AI写代码神器 | 1790点数解答]