掌握电话簿C语言编程:从零开始构建实用通讯录系统

引言
电话簿是一个基础且实用的信息管理工具,通过C语言编程实现一个电话簿系统可以帮助我们更好地管理和查找联系人信息。本文将引导您从零开始,逐步构建一个实用的电话簿通讯录系统。
一、系统需求分析
在开始编程之前,我们需要明确系统的基本需求:
添加联系人
:用户可以输入联系人的信息,如姓名、电话号码、电子邮件等。
查找联系人
:用户可以通过姓名、电话号码或其他标识符查找联系人。
删除联系人
:用户可以删除指定的联系人信息。
修改联系人
:用户可以更新联系人的信息。
显示所有联系人
:用户可以查看所有存储的联系人信息。
数据持久化
:将联系人信息保存到文件中,以便在程序关闭后重新加载。
二、环境准备
在开始编程之前,确保您已安装以下环境:
C语言编译器(如GCC)
文本编辑器(如VS Code、Sublime Text等)
三、数据结构设计
为了存储联系人信息,我们使用结构体(struct)来定义联系人:
#include
#include
#include

#define NAME_LEN 50
#define PHONE_LEN 20
#define EMAIL_LEN 50

typedef struct {
char name[NAME_LEN];
char phone[PHONE_LEN];
char email[EMAIL_LEN];
} Contact;
四、功能模块实现
1. 添加联系人
void addContact(Contact **contacts, int *size) {
Contact *newContact = (Contact *)malloc(sizeof(Contact));
if (newContact == NULL) {
printf(“Memory allocation failed.\n”);
return;
}

printf(“Enter name: “);
scanf(“%49s”, newContact->name);

printf(“Enter phone: “);
scanf(“%19s”, newContact->phone);

printf(“Enter email: “);
scanf(“%49s”, newContact->email);

*contacts = (Contact *)realloc(*contacts, (*size + 1) * sizeof(Contact));
if (*contacts == NULL) {
printf(“Memory reallocation failed.\n”);
free(newContact);
return;
}

(*contacts)[*size] = *newContact;
(*size)++;
}
2. 查找联系人
void findContact(Contact *contacts, int size) {
char name[NAME_LEN];
printf(“Enter name to find: “);
scanf(“%49s”, name);

for (int i = 0; i < size; i++) { if (strcmp(contacts[i].name, name) == 0) { printf("Contact found: %s, %s, %s\n", contacts[i].name, contacts[i].phone, contacts[i].email); return; } } printf("Contact not found.\n"); } 3. 删除联系人 void deleteContact(Contact **contacts, int *size) { char name[NAME_LEN]; printf("Enter name to delete: "); scanf("%49s", name); int found = 0; for (int i = 0; i < *size; i++) { if (strcmp((*contacts)[i].name, name) == 0) { found = 1; for (int j = i; j < *size - 1; j++) { (*contacts)[j] = (*contacts)[j + 1]; } *size--; break; } } if (found) { printf("Contact deleted.\n"); } else { printf("Contact not found.\n"); } } 4. 修改联系人 void modifyContact(Contact *contacts, int size) { char name[NAME_LEN]; printf("Enter name to modify: "); scanf("%49s", name); for (int i = 0; i < size; i++) { if (strcmp(contacts[i].name, name) == 0) { printf("Enter new phone: "); scanf("%19s", contacts[i].phone); printf("Enter new email: "); scanf("%49s", contacts[i].email); printf("Contact updated.\n"); return; } } printf("Contact not found.\n"); } 5. 显示所有联系人 void displayContacts(Contact *contacts, int size) { for (int i = 0; i < size; i++) { printf("%d. %s, %s, %s\n", i + 1, contacts[i].name, contacts[i].phone, contacts[i].email); } } 6. 数据持久化 void saveContacts(Contact *contacts, int size) { FILE *file = fopen("contacts.txt", "w"); if (file == NULL) { printf("File cannot be opened.\n"); return; } for (int i = 0; i < size; i++) { fprintf(file, "%s,%s,%s\n", contacts[i].name, contacts[i].phone, contacts[i].email); } fclose(file); } void loadContacts(Contact **contacts, int *size) { FILE *file = fopen("contacts.txt", "r"); if (file == NULL) { printf("File cannot be opened.\n"); return; } char line[200]; while (fgets(line, sizeof(line), file)) { Contact *newContact = (Contact *)malloc(sizeof(Contact)); if (newContact == NULL) { printf("Memory allocation failed.\n"); continue; } sscanf(line, "%49[^,],%19[^,],%49[^\n]", newContact->name, newContact->phone, newContact->email);

*contacts = (Contact *)realloc(*contacts, (*size + 1) * sizeof(Contact));
if (*contacts == NULL) {
printf(“Memory reallocation failed.\n”);
free(newContact);
continue;
}

(*contacts)[*size] = *newContact;
(*size)++;
}

fclose(file);
}
五、主函数
int main() {
Contact *contacts = NULL;
int size = 0;

loadContacts(&contacts, &size);

int choice;
do {
printf(“\n1. Add contact\n”);
printf(“2. Find contact\n”);
printf(“3. Delete contact\n”);
printf(“4. Modify contact\n”);
printf(“5. Display contacts\n”);
printf(“6. Save contacts\n”);
printf(“0. Exit\n”);
printf(“Enter your choice: “);
scanf(“%d”, &choice);

switch (choice) {
case 1:
addContact(&contacts, &size);
break;
case 2:
findContact(contacts, size);
break;
case 3:
deleteContact(&contacts, &size);
break;
case 4:
modifyContact(contacts, size);
break;
case 5:
displayContacts(contacts, size);
break;
case 6:
saveContacts(contacts, size);
break;
case 0:
printf(“Exiting…\n”);
break;
default:
printf(“Invalid choice. Please try again.\n”);
}
} while (choice != 0);

// Free allocated memory
for (int i = 0; i < size; i++) { free(contacts[i]); } free(contacts); return 0; } 六、总结 通过本文的引导,您已经学会了如何使用C语言构建一个实用的电话簿通讯录系统。这个系统具备添加、查找、删除、修改和显示联系人的功能,并且可以将联系人信息保存到文件中。希望这个项目能够帮助您巩固C语言编程技能,并在实际应用中发挥其作用。