写一个通讯录管理系统c语言,能实现数据的修改、插入、删除、保存、排序等。
时间: 2023-07-10 12:17:10 浏览: 101
c 语言的通讯录管理系统
好的,下面是一个简单的通讯录管理系统的C语言实现,可以实现数据的修改、插入、删除、保存、排序等功能。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
#define MAX_PHONE_LENGTH 11
#define MAX_CONTACTS 100
struct contact {
char name[MAX_NAME_LENGTH];
char phone[MAX_PHONE_LENGTH];
};
int num_contacts = 0;
struct contact contacts[MAX_CONTACTS];
void print_menu() {
printf("\n\n");
printf("1. Print contacts\n");
printf("2. Add contact\n");
printf("3. Update contact\n");
printf("4. Delete contact\n");
printf("5. Save contacts\n");
printf("6. Load contacts\n");
printf("7. Sort contacts\n");
printf("8. Quit\n");
}
void print_contacts() {
printf("\n\n");
printf("Contacts:\n");
for (int i = 0; i < num_contacts; i++) {
printf("%s, %s\n", contacts[i].name, contacts[i].phone);
}
}
void add_contact() {
if (num_contacts == MAX_CONTACTS) {
printf("\n\n");
printf("Error: Max contacts reached\n");
return;
}
printf("\n\n");
printf("Enter name: ");
scanf("%s", contacts[num_contacts].name);
printf("Enter phone: ");
scanf("%s", contacts[num_contacts].phone);
num_contacts++;
printf("\n\n");
printf("Contact added\n");
}
void update_contact() {
printf("\n\n");
printf("Enter name to update: ");
char name[MAX_NAME_LENGTH];
scanf("%s", name);
for (int i = 0; i < num_contacts; i++) {
if (strcmp(contacts[i].name, name) == 0) {
printf("Enter new phone: ");
scanf("%s", contacts[i].phone);
printf("\n\n");
printf("Contact updated\n");
return;
}
}
printf("\n\n");
printf("Error: Contact not found\n");
}
void delete_contact() {
printf("\n\n");
printf("Enter name to delete: ");
char name[MAX_NAME_LENGTH];
scanf("%s", name);
for (int i = 0; i < num_contacts; i++) {
if (strcmp(contacts[i].name, name) == 0) {
for (int j = i; j < num_contacts - 1; j++) {
strcpy(contacts[j].name, contacts[j + 1].name);
strcpy(contacts[j].phone, contacts[j + 1].phone);
}
num_contacts--;
printf("\n\n");
printf("Contact deleted\n");
return;
}
}
printf("\n\n");
printf("Error: Contact not found\n");
}
void save_contacts() {
FILE* fp;
fp = fopen("contacts.txt", "w");
for (int i = 0; i < num_contacts; i++) {
fprintf(fp, "%s, %s\n", contacts[i].name, contacts[i].phone);
}
fclose(fp);
printf("\n\n");
printf("Contacts saved\n");
}
void load_contacts() {
FILE* fp;
fp = fopen("contacts.txt", "r");
if (fp == NULL) {
printf("\n\n");
printf("Error: Failed to open contacts file\n");
return;
}
char name[MAX_NAME_LENGTH];
char phone[MAX_PHONE_LENGTH];
num_contacts = 0;
while (fscanf(fp, "%[^,], %[^\n]\n", name, phone) != EOF) {
strcpy(contacts[num_contacts].name, name);
strcpy(contacts[num_contacts].phone, phone);
num_contacts++;
}
fclose(fp);
printf("\n\n");
printf("Contacts loaded\n");
}
void sort_contacts() {
for (int i = 0; i < num_contacts - 1; i++) {
for (int j = i + 1; j < num_contacts; j++) {
if (strcmp(contacts[i].name, contacts[j].name) > 0) {
struct contact temp = contacts[i];
contacts[i] = contacts[j];
contacts[j] = temp;
}
}
}
printf("\n\n");
printf("Contacts sorted\n");
}
int main() {
int choice = 0;
do {
print_menu();
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
print_contacts();
break;
case 2:
add_contact();
break;
case 3:
update_contact();
break;
case 4:
delete_contact();
break;
case 5:
save_contacts();
break;
case 6:
load_contacts();
break;
case 7:
sort_contacts();
break;
case 8:
printf("\n\n");
printf("Goodbye!\n");
break;
default:
printf("\n\n");
printf("Error: Invalid choice\n");
break;
}
} while (choice != 8);
return 0;
}
```
这个程序使用一个结构体数组来存储通讯录中的联系人,每个联系人包含一个名字和一个电话号码。程序提供了一个菜单,用户可以选择执行各种操作,例如打印联系人列表,添加联系人,更新联系人信息,删除联系人,保存联系人到文件,从文件中加载联系人,对联系人按照名字排序等。
阅读全文