下面我将为您详细讲解“C语言实现通讯录的详细代码”的完整攻略:
一、需求分析
1. 该通讯录需要实现的基本功能有添加联系人、删除联系人、查找联系人、修改联系人信息以及显示通讯录中所有联系人信息。
2. 联系人信息需要包含姓名、电话号码、电子邮箱等信息。
3. 联系人信息需要存储在文件中,以便程序重启后能够读取之前保存的联系人信息。
二、程序设计
1. 定义联系人结构体(包含姓名、电话号码、电子邮箱)。
2. 定义通讯录结构体(包含联系人信息、当前联系人数量、最大联系人数量)。
3. 实现添加联系人功能:从键盘读取联系人信息并添加到通讯录中。
4. 实现删除联系人功能:从键盘读取要删除联系人的姓名,遍历通讯录找到对应联系人并删除。
5. 实现查找联系人功能:从键盘读取要查找联系人的姓名,遍历通讯录找到对应联系人并打印联系人的信息。
6. 实现修改联系人信息功能:从键盘读取要修改的联系人姓名,遍历通讯录找到对应联系人并修改联系人信息。
7. 实现显示通讯录中所有联系人信息功能:遍历通讯录并打印所有联系人信息。
8. 实现读取联系人信息功能:从文件中读取联系人信息并存储到通讯录中。
9. 实现保存联系人信息功能:将通讯录中所有联系人信息保存到文件中。
三、完整代码(包含示例说明)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CONTACTS 100
typedef struct {
char name[20];
char phone[20];
char email[30];
} Contact;
typedef struct {
Contact contacts[MAX_CONTACTS];
int num_contacts;
int max_contacts;
} AddressBook;
void add_contact(AddressBook* book) {
if (book->num_contacts >= book->max_contacts) {
printf("The address book is full.\n");
return;
}
Contact new_contact;
printf("Please enter the contact's name: ");
scanf("%s", new_contact.name);
printf("Please enter the contact's phone number: ");
scanf("%s", new_contact.phone);
printf("Please enter the contact's email: ");
scanf("%s", new_contact.email);
book->contacts[book->num_contacts] = new_contact;
book->num_contacts++;
}
int find_contact(AddressBook* book, char* name) {
for (int i = 0; i < book->num_contacts; i++) {
if (strcmp(book->contacts[i].name, name) == 0) {
return i;
}
}
return -1;
}
void remove_contact(AddressBook* book) {
char name[20];
printf("Please enter the contact's name: ");
scanf("%s", name);
int index = find_contact(book, name);
if (index == -1) {
printf("Contact not found.\n");
return;
}
for (int i = index + 1; i < book->num_contacts; i++) {
book->contacts[i - 1] = book->contacts[i];
}
book->num_contacts--;
printf("Contact removed.\n");
}
void find_contact_info(AddressBook* book) {
char name[20];
printf("Please enter the contact's name: ");
scanf("%s", name);
int index = find_contact(book, name);
if (index == -1) {
printf("Contact not found.\n");
return;
}
Contact contact = book->contacts[index];
printf("Name: %s\n", contact.name);
printf("Phone: %s\n", contact.phone);
printf("Email: %s\n", contact.email);
}
void update_contact(AddressBook* book) {
char name[20];
printf("Please enter the contact's name: ");
scanf("%s", name);
int index = find_contact(book, name);
if (index == -1) {
printf("Contact not found.\n");
return;
}
Contact new_contact;
printf("Please enter the new phone number for %s: ", name);
scanf("%s", new_contact.phone);
printf("Please enter the new email for %s: ", name);
scanf("%s", new_contact.email);
book->contacts[index] = new_contact;
printf("Contact information updated.\n");
}
void print_contacts(AddressBook* book) {
printf("Contacts:\n");
for (int i = 0; i < book->num_contacts; i++) {
Contact contact = book->contacts[i];
printf("Name: %s\n", contact.name);
printf("Phone: %s\n", contact.phone);
printf("Email: %s\n", contact.email);
printf("\n");
}
}
void load_contacts(AddressBook* book) {
FILE* file = fopen("contacts.txt", "r");
if (file == NULL) {
printf("Error loading contacts.\n");
return;
}
int num_contacts = 0;
Contact contact;
while (fscanf(file, "%s %s %s", contact.name, contact.phone, contact.email) == 3) {
book->contacts[num_contacts] = contact;
num_contacts++;
}
book->num_contacts = num_contacts;
fclose(file);
printf("Contacts loaded successfully.\n");
}
void save_contacts(AddressBook* book) {
FILE* file = fopen("contacts.txt", "w");
if (file == NULL) {
printf("Error saving contacts.\n");
return;
}
for (int i = 0; i < book->num_contacts; i++) {
Contact contact = book->contacts[i];
fprintf(file, "%s %s %s\n", contact.name, contact.phone, contact.email);
}
fclose(file);
printf("Contacts saved successfully.\n");
}
int main() {
AddressBook book;
book.num_contacts = 0;
book.max_contacts = MAX_CONTACTS;
int choice;
do {
printf("Please choose an option:\n");
printf("1. Add contact\n");
printf("2. Remove contact\n");
printf("3. Find contact information\n");
printf("4. Update contact information\n");
printf("5. Print all contacts\n");
printf("6. Load contacts\n");
printf("7. Save contacts\n");
printf("8. Quit\n");
printf("> ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_contact(&book);
break;
case 2:
remove_contact(&book);
break;
case 3:
find_contact_info(&book);
break;
case 4:
update_contact(&book);
break;
case 5:
print_contacts(&book);
break;
case 6:
load_contacts(&book);
break;
case 7:
save_contacts(&book);
break;
case 8:
printf("Goodbye.\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 8);
return 0;
}
示例说明:
假设我们想要添加联系人“Tom”的信息,则在程序界面选择选项1,输入联系人的姓名、电话号码、电子邮箱,按照提示添加即可。如果添加过程中通讯录已满,则会提示“The address book is full.”。
假设我们想要查找联系人“Tom”的信息,则在程序界面选择选项3,输入联系人的姓名,然后程序会遍历通讯录查找该联系人,如果找到则会将该联系人的信息打印出来;如果没有找到,则会提示“Contact not found.”。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言实现通讯录的详细代码 - Python技术站