写个通讯录系统的c语言代码,包括增加、删除、修改、查找、保存的功能
时间: 2023-10-20 16:16:02 浏览: 74
下面是一个简单的通讯录系统的C语言代码,包括增加、删除、修改、查找、保存的功能。该系统使用了结构体和文件读写操作:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PHONE_LEN 11
#define MAX_EMAIL_LEN 30
typedef struct {
char name[MAX_NAME_LEN];
char phone[MAX_PHONE_LEN];
char email[MAX_EMAIL_LEN];
} Contact;
void add_contact(Contact *contacts, int *count);
void delete_contact(Contact *contacts, int *count);
void modify_contact(Contact *contacts, int count);
void search_contact(Contact *contacts, int count);
void save_contacts(Contact *contacts, int count);
int main() {
Contact contacts[100];
int count = 0;
int choice;
while (1) {
printf("1. Add Contact\n");
printf("2. Delete Contact\n");
printf("3. Modify Contact\n");
printf("4. Search Contact\n");
printf("5. Save Contacts\n");
printf("6. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_contact(&contacts[count], &count);
break;
case 2:
delete_contact(contacts, &count);
break;
case 3:
modify_contact(contacts, count);
break;
case 4:
search_contact(contacts, count);
break;
case 5:
save_contacts(contacts, count);
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
void add_contact(Contact *contacts, int *count) {
printf("Enter name: ");
scanf("%s", contacts->name);
printf("Enter phone: ");
scanf("%s", contacts->phone);
printf("Enter email: ");
scanf("%s", contacts->email);
(*count)++;
}
void delete_contact(Contact *contacts, int *count) {
char name[MAX_NAME_LEN];
int i, j, found = 0;
printf("Enter name to delete: ");
scanf("%s", name);
for (i = 0; i < *count; i++) {
if (strcmp(contacts[i].name, name) == 0) {
found = 1;
for (j = i; j < *count - 1; j++) {
contacts[j] = contacts[j + 1];
}
(*count)--;
break;
}
}
if (found) {
printf("Contact deleted successfully!\n");
} else {
printf("Contact not found!\n");
}
}
void modify_contact(Contact *contacts, int count) {
char name[MAX_NAME_LEN];
int i, found = 0;
printf("Enter name to modify: ");
scanf("%s", name);
for (i = 0; i < count; i++) {
if (strcmp(contacts[i].name, name) == 0) {
found = 1;
printf("Enter new phone: ");
scanf("%s", contacts[i].phone);
printf("Enter new email: ");
scanf("%s", contacts[i].email);
break;
}
}
if (found) {
printf("Contact modified successfully!\n");
} else {
printf("Contact not found!\n");
}
}
void search_contact(Contact *contacts, int count) {
char name[MAX_NAME_LEN];
int i, found = 0;
printf("Enter name to search: ");
scanf("%s", name);
for (i = 0; i < count; i++) {
if (strcmp(contacts[i].name, name) == 0) {
found = 1;
printf("Name: %s\nPhone: %s\nEmail: %s\n",
contacts[i].name, contacts[i].phone, contacts[i].email);
break;
}
}
if (!found) {
printf("Contact not found!\n");
}
}
void save_contacts(Contact *contacts, int count) {
FILE *file = fopen("contacts.txt", "w");
int i;
if (file == NULL) {
printf("Failed to open file!\n");
return;
}
for (i = 0; i < count; i++) {
fprintf(file, "%s %s %s\n",
contacts[i].name, contacts[i].phone, contacts[i].email);
}
fclose(file);
printf("Contacts saved successfully!\n");
}
```
在该代码中,`Contact` 结构体表示每个通讯录条目,包括姓名、电话和电子邮件地址。`add_contact` 函数用于添加新条目,`delete_contact` 函数用于删除条目,`modify_contact` 函数用于修改条目,`search_contact` 函数用于搜索条目,`save_contacts` 函数用于将条目保存到文件中。在 `main` 函数中,使用一个无限循环来提供菜单选项。
阅读全文