#include <stdio.h>#include <string.h>#define MAX_SIZE 100typedef struct { char name[20]; // 姓名 char company[50]; // 单位 char telephone[12]; // 固定电话 char mobile[12]; // 移动手机 char category[10]; // 分类 char email[50]; // EMAIL char qq[15]; // QQ} AddressBook;AddressBook contacts[MAX_SIZE]; // 存储通讯录信息的数组int count = 0; // 当前通讯录数量void input() { char choice = 'y'; while (choice == 'y' || choice == 'Y') { printf("请输入姓名:"); scanf("%s", contacts[count].name); printf("请输入单位:"); scanf("%s", contacts[count].company); printf("请输入固定电话:"); scanf("%s", contacts[count].telephone); printf("请输入移动手机:"); scanf("%s", contacts[count].mobile); printf("请输入分类:"); scanf("%s", contacts[count].category); printf("请输入EMAIL:"); scanf("%s", contacts[count].email); printf("请输入QQ:"); scanf("%s", contacts[count].qq); count++; // 当前通讯录数量加1 printf("是否继续输入(Y/N)?"); scanf(" %c", &choice); // 注意空格的作用,防止读取上一个字符 } printf("输入完成!\n");}void display() { if (count == 0) { printf("通讯录为空!\n"); return; } for (int i = 0; i < count; i++) { printf("姓名:%s\n", contacts[i].name); printf("单位:%s\n", contacts[i].company); printf("固定电话:%s\n", contacts[i].telephone); printf("移动手机:%s\n", contacts[i].mobile); printf("分类:%s\n", contacts[i].category); printf("EMAIL:%s\n", contacts[i].email); printf("QQ:%s\n", contacts[i].qq); printf("\n"); }}void search() { char keyword[20]; printf("请输入要查找的关键字:"); scanf("%s", keyword); int found = 0; for (int i = 0; i < count; i++) { if (strcmp(contacts[i].name, keyword) == 0) { printf("找到匹配的结果:\n"); printf("姓名:%s\n", contacts[i].name); printf("单位:%s\n", contacts[i].company); printf("固定电话:%s\n", contacts[i].telephone); printf("移动手机:%s\n", contacts[i].mobile); printf("分类:%s\n", contacts[i].category); printf("EMAIL:%s\n", contacts[i].email); printf("QQ:%s\n", contacts[i].qq); printf("\n"); found = 1; } } if (!found) { printf("未找到匹配的结果!\n"); }}void add() { if (count >= MAX_SIZE) { printf("通讯录已满,无法添加!\n"); return; } printf("请输入姓名:"); scanf("%s", contacts[count].name); printf("请输入单位:"); scanf("%s", contacts[count].company); printf("请输入固定电话:"); scanf("%s", contacts[count].telephone); printf("请输入移动手机:"); scanf("%s", contacts[count].mobile); printf("请输入分类:"); scanf("%s", contacts[count].category); printf("请输入EMAIL:"); scanf("%s", contacts[count].email); printf("请输入QQ:"); scanf("%s", contacts[count].qq); count++; // 当前通讯录数量加1 printf("添加成功!\n");}void remove() { char keyword[20]; printf("请输入要删除的关键字:"); scanf("%s", keyword); int found = 0; for (int i = 0; i < count; i++) { if (strcmp(contacts[i].name, keyword) == 0) { found = 1; // 将后面的所有信息往前移动一个位置 for (int j = i; j < count - 1; j++) { strcpy(contacts[j].name, contacts[j+1].name); strcpy(contacts[j].company, contacts[j+1].company); strcpy(contacts[j].telephone, contacts[j+1].telephone); strcpy(contacts[j].mobile, contacts[j+1].mobile); strcpy(contacts[j].category, contacts[j+1].category); strcpy(contacts[j].email, contacts[j+1].email); strcpy(contacts[j].qq, contacts[j+1].qq); } count--; // 当前通讯录数量减1 printf("删除成功!\n"); break; } } if (!found) { printf("未找到匹配的结果!\n"); }}void modify() { char keyword[20]; printf("请输入要修改的关键字:"); scanf("%s", keyword); int found = 0; for (int i = 0; i < count; i++) { if (strcmp(contacts[i].name, keyword) == 0) { found = 1; printf("请输入新的姓名:"); scanf("%s", contacts[i].name); printf("请输入新的单位:"); scanf("%s", contacts[i].company); printf("请输入新的固定电话:"); scanf("%s", contacts[i].telephone); printf("请输入新的移动手机:"); scanf("%s", contacts[i].mobile); printf("请输入新的分类:"); scanf("%s", contacts[i].category); printf("请输入新的EMAIL:"); scanf("%s", contacts[i].email); printf("请输入新的QQ:"); scanf("%s", contacts[i].qq); printf("修改成功!\n"); break; } } if (!found) { printf("未找到匹配的结果!\n"); }}int main() { int choice; while (1) { printf("请选择操作:\n"); printf("1. 输入通讯录信息\n"); printf("2. 显示通讯录信息\n"); printf("3. 查找通讯录信息\n"); printf("4. 添加通讯录信息\n"); printf("5. 删除通讯录信息\n"); printf("6. 修改通讯录信息\n"); printf("0. 退出程序\n"); printf("请输入选择:"); scanf("%d", &choice); switch(choice) { case 1: input(); break; case 2: display(); break; case 3: search(); break; case 4: add(); break; case 5: remove(); break; case 6: modify(); break; case 0: printf("程序已退出。\n"); return 0; default: printf("无效的选择,请重新输入!\n"); break; } printf("\n"); }}
该代码包含了主函数,可以实现通讯录管理系统的各种功能。你可以根据需要进行测试和使用。希望能对你有所帮助!