使用c语言设计一个通讯录程序,实现通讯录信息的录入,添加删除显示的功能,使用结构体存储通讯录的记录信息,使用链表实现通讯录信息的增删及查询显示,使用文件储存通讯录
时间: 2024-03-18 16:42:32 浏览: 63
基于C++语言和单链表数据结构实现的通讯录管理系统,包含文件读取、文件保存、增删改查、排序、清空等功能.zip
5星 · 资源好评率100%
好的,我可以帮你解答这个问题。下面是示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PHONE_LEN 15
// 定义通讯录结构体
typedef struct _Contact {
char name[MAX_NAME_LEN];
char phone[MAX_PHONE_LEN];
struct _Contact* next;
} Contact;
// 定义通讯录链表头
Contact* head = NULL;
// 函数声明
void add_contact();
void delete_contact();
void display_contact();
void search_contact();
void save_contact();
void load_contact();
int main() {
int choice;
do {
printf("\n");
printf("1. Add contact\n");
printf("2. Delete contact\n");
printf("3. Display contacts\n");
printf("4. Search contact\n");
printf("5. Save contacts to file\n");
printf("6. Load contacts from file\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_contact();
break;
case 2:
delete_contact();
break;
case 3:
display_contact();
break;
case 4:
search_contact();
break;
case 5:
save_contact();
break;
case 6:
load_contact();
break;
case 0:
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 0);
return 0;
}
// 添加通讯录记录
void add_contact() {
Contact* new_contact = (Contact*) malloc(sizeof(Contact));
printf("Enter name: ");
scanf("%s", new_contact->name);
printf("Enter phone number: ");
scanf("%s", new_contact->phone);
new_contact->next = head;
head = new_contact;
printf("Contact added successfully!\n");
}
// 删除通讯录记录
void delete_contact() {
char name[MAX_NAME_LEN];
printf("Enter name to delete: ");
scanf("%s", name);
Contact* curr_contact = head;
Contact* prev_contact = NULL;
while (curr_contact != NULL && strcmp(curr_contact->name, name) != 0) {
prev_contact = curr_contact;
curr_contact = curr_contact->next;
}
if (curr_contact == NULL) {
printf("Contact not found!\n");
} else {
if (prev_contact == NULL) {
head = curr_contact->next;
} else {
prev_contact->next = curr_contact->next;
}
free(curr_contact);
printf("Contact deleted successfully!\n");
}
}
// 显示所有通讯录记录
void display_contact() {
Contact* curr_contact = head;
if (curr_contact == NULL) {
printf("No contacts found!\n");
} else {
while (curr_contact != NULL) {
printf("%s\t%s\n", curr_contact->name, curr_contact->phone);
curr_contact = curr_contact->next;
}
}
}
// 搜索通讯录记录
void search_contact() {
char name[MAX_NAME_LEN];
printf("Enter name to search: ");
scanf("%s", name);
Contact* curr_contact = head;
while (curr_contact != NULL && strcmp(curr_contact->name, name) != 0) {
curr_contact = curr_contact->next;
}
if (curr_contact == NULL) {
printf("Contact not found!\n");
} else {
printf("%s\t%s\n", curr_contact->name, curr_contact->phone);
}
}
// 将通讯录记录保存到文件中
void save_contact() {
char filename[MAX_NAME_LEN];
printf("Enter filename to save: ");
scanf("%s", filename);
FILE* fp = fopen(filename, "w");
if (fp == NULL) {
printf("File open failed!\n");
return;
}
Contact* curr_contact = head;
while (curr_contact != NULL) {
fprintf(fp, "%s\t%s\n", curr_contact->name, curr_contact->phone);
curr_contact = curr_contact->next;
}
fclose(fp);
printf("Contacts saved successfully!\n");
}
// 从文件中加载通讯录记录
void load_contact() {
char filename[MAX_NAME_LEN];
printf("Enter filename to load: ");
scanf("%s", filename);
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("File open failed!\n");
return;
}
char name[MAX_NAME_LEN];
char phone[MAX_PHONE_LEN];
while (fscanf(fp, "%s\t%s\n", name, phone) != EOF) {
Contact* new_contact = (Contact*) malloc(sizeof(Contact));
strcpy(new_contact->name, name);
strcpy(new_contact->phone, phone);
new_contact->next = head;
head = new_contact;
}
fclose(fp);
printf("Contacts loaded successfully!\n");
}
```
这是一个基本的通讯录程序,使用了结构体存储通讯录记录信息,使用链表实现通讯录信息的增删及查询显示,使用文件储存通讯录。你可以根据需要进行修改和完善。
阅读全文