通讯录完整





在这里插入图片描述

以下是一个简单的C语言通讯录完整代码,包括添加、删除、查找、修改和显示通讯录中所有联系人的功能: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define NAME_LEN 20 #define PHONE_LEN 11 #define MAX_CONTACTS 100 typedef struct Contact { char name[NAME_LEN]; char phone[PHONE_LEN]; } Contact; Contact contacts[MAX_CONTACTS]; int num_contacts = 0; void add_contact() { if (num_contacts == MAX_CONTACTS) { printf("The contacts list is full.\n"); return; } Contact new_contact; printf("Enter name: "); scanf("%s", new_contact.name); printf("Enter phone number: "); scanf("%s", new_contact.phone); contacts[num_contacts++] = new_contact; printf("Contact added.\n"); } void delete_contact() { char name[NAME_LEN]; printf("Enter name to delete: "); scanf("%s", name); int index = -1; for (int i = 0; i < num_contacts; i++) { if (strcmp(contacts[i].name, name) == 0) { index = i; break; } } if (index == -1) { printf("Contact not found.\n"); return; } for (int i = index; i < num_contacts - 1; i++) { contacts[i] = contacts[i + 1]; } num_contacts--; printf("Contact deleted.\n"); } void find_contact() { char name[NAME_LEN]; printf("Enter name to find: "); scanf("%s", name); int index = -1; for (int i = 0; i < num_contacts; i++) { if (strcmp(contacts[i].name, name) == 0) { index = i; break; } } if (index == -1) { printf("Contact not found.\n"); return; } printf("Name: %s\nPhone: %s\n", contacts[index].name, contacts[index].phone); } void modify_contact() { char name[NAME_LEN]; printf("Enter name to modify: "); scanf("%s", name); int index = -1; for (int i = 0; i < num_contacts; i++) { if (strcmp(contacts[i].name, name) == 0) { index = i; break; } } if (index == -1) { printf("Contact not found.\n"); return; } printf("Enter new phone number: "); scanf("%s", contacts[index].phone); printf("Contact modified.\n"); } void print_contacts() { printf("Contacts:\n"); for (int i = 0; i < num_contacts; i++) { printf("%s: %s\n", contacts[i].name, contacts[i].phone); } } int main() { int choice; while (1) { printf("Choose an option:\n"); printf("1. Add contact\n2. Delete contact\n3. Find contact\n4. Modify contact\n5. Print contacts\n6. Exit\n"); scanf("%d", &choice); switch (choice) { case 1: add_contact(); break; case 2: delete_contact(); break; case 3: find_contact(); break; case 4: modify_contact(); break; case 5: print_contacts(); break; case 6: exit(0); default: printf("Invalid choice.\n"); } } return 0; } ``` 这个通讯录程序使用了结构体来存储联系人的姓名和电话号码。程序中定义了一些常量,如姓名和电话号码的最大长度,以及通讯录最多可以存储的联系人数量。然后定义了一个Contact结构体类型和一个存储Contact类型的数组。 程序中实现了五个功能:添加联系人、删除联系人、查找联系人、修改联系人以及显示所有联系人。在主函数中,使用了一个无限循环来让用户选择要执行的操作。每次循环中,程序会打印出菜单,并根据用户的选择执行相应的操作。执行完操作后,程序会回到循环开始处,等待用户下一次操作。用户可以在任何时候选择退出程序。 注意,这个程序中没有对输入进行任何的错误检查,例如输入的电话号码是否合法等。在实际编写程序时,应该考虑到这些情况并进行相应的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值