通讯录管理系统c语言
时间: 2023-07-03 10:11:36 浏览: 58
通讯录管理系统的实现可以使用C语言,下面是一个简单的通讯录管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CONTACTS 1000
#define NAME_LENGTH 20
#define PHONE_LENGTH 20
struct Contact {
char name[NAME_LENGTH];
char phone[PHONE_LENGTH];
};
struct Contact contacts[MAX_CONTACTS];
int num_contacts = 0;
void add_contact() {
if (num_contacts == MAX_CONTACTS) {
printf("Error: contact list is full\n");
return;
}
struct 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;
num_contacts++;
printf("Contact added successfully\n");
}
void delete_contact() {
char name[NAME_LENGTH];
printf("Enter name: ");
scanf("%s", name);
int i;
for (i = 0; i < num_contacts; i++) {
if (strcmp(contacts[i].name, name) == 0) {
int j;
for (j = i; j < num_contacts - 1; j++) {
contacts[j] = contacts[j + 1];
}
num_contacts--;
printf("Contact deleted successfully\n");
return;
}
}
printf("Contact not found\n");
}
void list_contacts() {
int i;
for (i = 0; i < num_contacts; i++) {
printf("%s: %s\n", contacts[i].name, contacts[i].phone);
}
}
int main() {
int choice;
while (1) {
printf("Enter 1 to add contact, 2 to delete contact, 3 to list contacts, or 4 to quit: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_contact();
break;
case 2:
delete_contact();
break;
case 3:
list_contacts();
break;
case 4:
printf("Quitting\n");
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
```
该程序使用结构体实现通讯录条目,并使用一个数组来存储所有条目。程序提供了添加、删除和列出通讯录条目的功能。