使用c语言数据结构写一个通讯录系统,功能包括新建联系人,模糊查找联系人,删除联系人,修改联系人信息,个性化排序,通讯录分组等功能
时间: 2023-09-17 21:09:56 浏览: 51
好的,您需要使用哪种数据结构来实现这个通讯录系统呢?可以使用链表、数组或哈希表等数据结构。以下是一个使用结构体和链表实现通讯录系统的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LENGTH 20
#define PHONE_LENGTH 12
#define EMAIL_LENGTH 50
typedef struct Contact {
char name[NAME_LENGTH];
char phone[PHONE_LENGTH];
char email[EMAIL_LENGTH];
struct Contact *next;
} Contact;
Contact *head = NULL;
void add_contact() {
Contact *new_contact = (Contact *)malloc(sizeof(Contact));
printf("Enter name: ");
scanf("%s", new_contact->name);
printf("Enter phone: ");
scanf("%s", new_contact->phone);
printf("Enter email: ");
scanf("%s", new_contact->email);
new_contact->next = head;
head = new_contact;
printf("Contact added successfully!\n");
}
void search_contact() {
char query[NAME_LENGTH];
printf("Enter name or part of name to search: ");
scanf("%s", query);
Contact *current = head;
int found = 0;
while (current != NULL) {
if (strstr(current->name, query) != NULL) {
printf("%s\t%s\t%s\n", current->name, current->phone, current->email);
found = 1;
}
current = current->next;
}
if (!found) {
printf("No contacts found!\n");
}
}
void delete_contact() {
char name[NAME_LENGTH];
printf("Enter name to delete: ");
scanf("%s", name);
if (head == NULL) {
printf("No contacts to delete!\n");
return;
}
Contact *current = head;
if (strcmp(current->name, name) == 0) {
head = current->next;
free(current);
printf("Contact deleted successfully!\n");
return;
}
while (current->next != NULL) {
if (strcmp(current->next->name, name) == 0) {
Contact *temp = current->next;
current->next = temp->next;
free(temp);
printf("Contact deleted successfully!\n");
return;
}
current = current->next;
}
printf("Contact not found!\n");
}
void modify_contact() {
char name[NAME_LENGTH];
printf("Enter name to modify: ");
scanf("%s", name);
if (head == NULL) {
printf("No contacts to modify!\n");
return;
}
Contact *current = head;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
printf("Enter new phone: ");
scanf("%s", current->phone);
printf("Enter new email: ");
scanf("%s", current->email);
printf("Contact modified successfully!\n");
return;
}
current = current->next;
}
printf("Contact not found!\n");
}
void print_contacts(Contact *contacts) {
if (contacts == NULL) {
printf("No contacts to display!\n");
return;
}
while (contacts != NULL) {
printf("%s\t%s\t%s\n", contacts->name, contacts->phone, contacts->email);
contacts = contacts->next;
}
}
Contact *sort_contacts(Contact *contacts) {
if (contacts == NULL || contacts->next == NULL) {
return contacts;
}
Contact *middle = contacts;
Contact *current = contacts->next;
while (current != NULL) {
current = current->next;
if (current != NULL) {
middle = middle->next;
current = current->next;
}
}
Contact *right = middle->next;
middle->next = NULL;
Contact *sorted_left = sort_contacts(contacts);
Contact *sorted_right = sort_contacts(right);
Contact *merged = NULL;
if (strcmp(sorted_left->name, sorted_right->name) <= 0) {
merged = sorted_left;
sorted_left = sorted_left->next;
} else {
merged = sorted_right;
sorted_right = sorted_right->next;
}
Contact *current_merged = merged;
while (sorted_left != NULL && sorted_right != NULL) {
if (strcmp(sorted_left->name, sorted_right->name) <= 0) {
current_merged->next = sorted_left;
sorted_left = sorted_left->next;
} else {
current_merged->next = sorted_right;
sorted_right = sorted_right->next;
}
current_merged = current_merged->next;
}
if (sorted_left != NULL) {
current_merged->next = sorted_left;
} else {
current_merged->next = sorted_right;
}
return merged;
}
void group_contacts() {
Contact *sorted_contacts = sort_contacts(head);
Contact *a_to_m = NULL;
Contact *n_to_z = NULL;
while (sorted_contacts != NULL) {
Contact *temp = sorted_contacts;
sorted_contacts = sorted_contacts->next;
if (temp->name[0] >= 'A' && temp->name[0] <= 'M') {
temp->next = a_to_m;
a_to_m = temp;
} else {
temp->next = n_to_z;
n_to_z = temp;
}
}
printf("Contacts from A to M:\n");
print_contacts(a_to_m);
printf("Contacts from N to Z:\n");
print_contacts(n_to_z);
}
int main() {
int choice;
while (1) {
printf("1. Add contact\n");
printf("2. Search contact\n");
printf("3. Delete contact\n");
printf("4. Modify contact\n");
printf("5. Group contacts\n");
printf("6. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_contact();
break;
case 2:
search_contact();
break;
case 3:
delete_contact();
break;
case 4:
modify_contact();
break;
case 5:
group_contacts();
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
}
}
}
```
请注意,此示例代码未对输入进行完整性检查。您可能需要添加更多的输入检查和错误处理来确保程序的稳定性和安全性。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)