通讯录管理系统c语言
时间: 2023-07-03 19:17:02 浏览: 65
好的,您想了解通讯录管理系统的C语言实现。通讯录管理系统可以用来存储联系人的姓名、电话号码、地址等信息,并且可以进行添加、删除、修改、查询等操作。以下是一个简单的通讯录管理系统的C语言实现。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CONTACTS 100
struct contact {
char name[50];
char phone[20];
char address[100];
};
struct contact contacts[MAX_CONTACTS];
int num_contacts = 0;
void add_contact() {
if (num_contacts == MAX_CONTACTS) {
printf("The contact list is full.\n");
return;
}
struct contact new_contact;
printf("Enter the name: ");
scanf("%s", new_contact.name);
printf("Enter the phone number: ");
scanf("%s", new_contact.phone);
printf("Enter the address: ");
scanf("%s", new_contact.address);
contacts[num_contacts++] = new_contact;
printf("Contact added.\n");
}
void delete_contact() {
if (num_contacts == 0) {
printf("The contact list is empty.\n");
return;
}
char name[50];
printf("Enter the name of the contact to delete: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_contacts; i++) {
if (strcmp(name, contacts[i].name) == 0) {
found = 1;
for (int j = i; j < num_contacts - 1; j++) {
contacts[j] = contacts[j + 1];
}
num_contacts--;
printf("Contact deleted.\n");
break;
}
}
if (!found) {
printf("Contact not found.\n");
}
}
void modify_contact() {
if (num_contacts == 0) {
printf("The contact list is empty.\n");
return;
}
char name[50];
printf("Enter the name of the contact to modify: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_contacts; i++) {
if (strcmp(name, contacts[i].name) == 0) {
found = 1;
printf("Enter the new phone number: ");
scanf("%s", contacts[i].phone);
printf("Enter the new address: ");
scanf("%s", contacts[i].address);
printf("Contact modified.\n");
break;
}
}
if (!found) {
printf("Contact not found.\n");
}
}
void display_contact() {
if (num_contacts == 0) {
printf("The contact list is empty.\n");
return;
}
for (int i = 0; i < num_contacts; i++) {
printf("Name: %s\n", contacts[i].name);
printf("Phone: %s\n", contacts[i].phone);
printf("Address: %s\n", contacts[i].address);
printf("\n");
}
}
int main() {
int choice;
while (1) {
printf("1. Add a contact\n");
printf("2. Delete a contact\n");
printf("3. Modify a contact\n");
printf("4. Display all contacts\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_contact();
break;
case 2:
delete_contact();
break;
case 3:
modify_contact();
break;
case 4:
display_contact();
break;
case 5:
exit(0);
default:
printf("Invalid choice.\n");
}
printf("\n");
}
return 0;
}
```
这个通讯录管理系统使用了一个结构体 `contact` 来存储每个联系人的信息,包括姓名、电话号码和地址。在主函数中,使用一个循环来显示菜单,让用户可以选择进行添加、删除、修改、查询等操作。每个操作都对应一个函数,实现相应的功能。需要注意的是,这个通讯录管理系统最多只能存储100个联系人的信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)