c语言实现添加、打印、拨号、删除和编辑联系人功能完整代码

这个程序可以实现一个简单的通讯录功能,可以添加、打印、拨号、删除和编辑联系人。同时,程序还可以将联系人信息保存到文件中,下次运行时可以从文件中读取联系人信息。
完整代码如下:
#include <stdio.h>
#include <string.h>

#define MAX_CONTACTS 100

typedef struct {
    char name[50];
    char phone[20];
} Contact;

Contact contacts[MAX_CONTACTS];
int num_contacts = 0;

void add_contact() {
    if (num_contacts >= MAX_CONTACTS) {
        printf("Max contacts exceeded.\n");
        return;
    }
    printf("Enter name: ");
    scanf("%s", contacts[num_contacts].name);
    printf("Enter phone number: ");
    scanf("%s", contacts[num_contacts].phone);
    num_contacts++;
    printf("Contact added.\n");
}

void print_contacts() {
    if (num_contacts == 0) {
        printf("No contacts.\n");
        return;
    }
    for (int i = 0; i < num_contacts; i++) {
        printf("%s: %s\n", contacts[i].name, contacts[i].phone);
    }
}

void dial_contact() {
    char name[50];
    printf("Enter name of contact to dial: ");
    scanf("%s", name);
    for (int i = 0; i < num_contacts; i++) {
        if (strcmp(contacts[i].name, name) == 0) {
            printf("Dialing %s...\n", contacts[i].phone);
            return;
        }
    }
    printf("Contact not found.\n");
}

void delete_contact() {
    char name[50];
    printf("Enter name of contact to delete: ");
    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++) {
                contacts[j] = contacts[j+1];
            }
            num_contacts--;
            printf("Contact deleted.\n");
            return;
        }
    }
    printf("Contact not found.\n");
}

void edit_contact() {
    char name[50];
    printf("Enter name of contact to edit: ");
    scanf("%s", name);
    for (int i = 0; i < num_contacts; i++) {
        if (strcmp(contacts[i].name, name) == 0) {
            printf("Editing contact: %s\n", contacts[i].name);
            printf("Enter new name (press enter to keep current value): ");
            char new_name[50];
            scanf("%s", new_name);
            if (strlen(new_name) > 0) {
                strcpy(contacts[i].name, new_name);
            }
            printf("Enter new phone (press enter to keep current value): ");
            char new_phone[20];
            scanf("%s", new_phone);
            if (strlen(new_phone) > 0) {
                strcpy(contacts[i].phone, new_phone);
            }
            printf("Contact updated.\n");
            return;
        }
    }
    printf("Contact not found.\n");
}

int read_contacts() {
    FILE *f = fopen("contacts.dat", "rb");
    if (f == NULL) {
        return 0;
    }
    fread(&num_contacts, sizeof(num_contacts), 1, f);
    fread(contacts, sizeof(Contact), num_contacts, f);
    fclose(f);
    return 1;
}

void save_contacts() {
    FILE *f = fopen("contacts.dat", "wb");
    if (f == NULL) {
        printf("Error saving contacts.\n");
        return;
    }
    fwrite(&num_contacts, sizeof(num_contacts), 1, f);
    fwrite(contacts, sizeof(Contact), num_contacts,fclose(f);
printf("Contacts saved.\n");
}

int main() {
if (!read_contacts()) {
printf("No contacts found.\n");
}
while (1) {
printf("Menu:\n");
printf("1. Add contact\n");
printf("2. Print contacts\n");
printf("3. Dial contact\n");
printf("4. Delete contact\n");
printf("5. Edit contact\n");
printf("6. Exit\n");
printf("Enter selection: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_contact();
break;
case 2:
print_contacts();
break;
case 3:
dial_contact();
break;
case 4:
delete_contact();
break;
case 5:
edit_contact();
break;
case 6:
save_contacts();
printf("Goodbye!\n");
return 0;
default:
printf("Invalid selection.\n");
break;
}
}
return 0;
}