C语言动态与静态分别实现通讯录的方法

发布时间:2022-02-28 09:27:58 作者:iii
来源:亿速云 阅读:157

C语言动态与静态分别实现通讯录的方法

目录

  1. 引言
  2. 通讯录的基本功能
  3. 静态实现通讯录
  4. 动态实现通讯录
  5. 静态与动态实现的对比
  6. 总结

引言

通讯录是日常生活中常用的工具,用于存储和管理联系人信息。在C语言中,我们可以通过静态和动态两种方式来实现通讯录。静态实现通常使用数组来存储联系人信息,而动态实现则使用链表或动态数组等数据结构。本文将详细介绍这两种实现方法,并通过代码示例展示其具体实现过程。

通讯录的基本功能

一个基本的通讯录通常包含以下功能: 1. 添加联系人:向通讯录中添加新的联系人信息。 2. 删除联系人:从通讯录中删除指定的联系人。 3. 查找联系人:根据姓名或其他信息查找联系人。 4. 修改联系人:修改通讯录中已有联系人的信息。 5. 显示所有联系人:显示通讯录中所有联系人的信息。 6. 保存通讯录:将通讯录保存到文件中。 7. 加载通讯录:从文件中加载通讯录。

静态实现通讯录

数据结构设计

在静态实现中,我们通常使用数组来存储联系人信息。每个联系人可以定义为一个结构体,包含姓名、电话号码、电子邮件等信息。

#define MAX_CONTACTS 100

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

Contact contacts[MAX_CONTACTS];
int contact_count = 0;

功能实现

  1. 添加联系人
    • 检查通讯录是否已满。
    • 将新联系人信息添加到数组中。
void add_contact() {
    if (contact_count >= MAX_CONTACTS) {
        printf("通讯录已满,无法添加新联系人。\n");
        return;
    }

    Contact new_contact;
    printf("请输入姓名: ");
    scanf("%s", new_contact.name);
    printf("请输入电话号码: ");
    scanf("%s", new_contact.phone);
    printf("请输入电子邮件: ");
    scanf("%s", new_contact.email);

    contacts[contact_count++] = new_contact;
    printf("联系人添加成功。\n");
}
  1. 删除联系人
    • 根据姓名查找联系人。
    • 将数组中的后续元素向前移动,覆盖要删除的联系人。
void delete_contact() {
    char name[50];
    printf("请输入要删除的联系人姓名: ");
    scanf("%s", name);

    int index = -1;
    for (int i = 0; i < contact_count; i++) {
        if (strcmp(contacts[i].name, name) == 0) {
            index = i;
            break;
        }
    }

    if (index == -1) {
        printf("未找到该联系人。\n");
        return;
    }

    for (int i = index; i < contact_count - 1; i++) {
        contacts[i] = contacts[i + 1];
    }

    contact_count--;
    printf("联系人删除成功。\n");
}
  1. 查找联系人
    • 根据姓名查找联系人并显示其信息。
void find_contact() {
    char name[50];
    printf("请输入要查找的联系人姓名: ");
    scanf("%s", name);

    for (int i = 0; i < contact_count; i++) {
        if (strcmp(contacts[i].name, name) == 0) {
            printf("姓名: %s\n", contacts[i].name);
            printf("电话号码: %s\n", contacts[i].phone);
            printf("电子邮件: %s\n", contacts[i].email);
            return;
        }
    }

    printf("未找到该联系人。\n");
}
  1. 修改联系人
    • 根据姓名查找联系人并修改其信息。
void modify_contact() {
    char name[50];
    printf("请输入要修改的联系人姓名: ");
    scanf("%s", name);

    for (int i = 0; i < contact_count; i++) {
        if (strcmp(contacts[i].name, name) == 0) {
            printf("请输入新的电话号码: ");
            scanf("%s", contacts[i].phone);
            printf("请输入新的电子邮件: ");
            scanf("%s", contacts[i].email);
            printf("联系人信息修改成功。\n");
            return;
        }
    }

    printf("未找到该联系人。\n");
}
  1. 显示所有联系人
    • 遍历数组并显示所有联系人的信息。
void display_contacts() {
    if (contact_count == 0) {
        printf("通讯录为空。\n");
        return;
    }

    for (int i = 0; i < contact_count; i++) {
        printf("联系人 %d:\n", i + 1);
        printf("姓名: %s\n", contacts[i].name);
        printf("电话号码: %s\n", contacts[i].phone);
        printf("电子邮件: %s\n", contacts[i].email);
        printf("\n");
    }
}
  1. 保存通讯录
    • 将通讯录保存到文件中。
void save_contacts() {
    FILE *file = fopen("contacts.dat", "wb");
    if (file == NULL) {
        printf("无法打开文件。\n");
        return;
    }

    fwrite(&contact_count, sizeof(int), 1, file);
    fwrite(contacts, sizeof(Contact), contact_count, file);

    fclose(file);
    printf("通讯录保存成功。\n");
}
  1. 加载通讯录
    • 从文件中加载通讯录。
void load_contacts() {
    FILE *file = fopen("contacts.dat", "rb");
    if (file == NULL) {
        printf("无法打开文件。\n");
        return;
    }

    fread(&contact_count, sizeof(int), 1, file);
    fread(contacts, sizeof(Contact), contact_count, file);

    fclose(file);
    printf("通讯录加载成功。\n");
}

代码示例

#include <stdio.h>
#include <string.h>

#define MAX_CONTACTS 100

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

Contact contacts[MAX_CONTACTS];
int contact_count = 0;

void add_contact() {
    if (contact_count >= MAX_CONTACTS) {
        printf("通讯录已满,无法添加新联系人。\n");
        return;
    }

    Contact new_contact;
    printf("请输入姓名: ");
    scanf("%s", new_contact.name);
    printf("请输入电话号码: ");
    scanf("%s", new_contact.phone);
    printf("请输入电子邮件: ");
    scanf("%s", new_contact.email);

    contacts[contact_count++] = new_contact;
    printf("联系人添加成功。\n");
}

void delete_contact() {
    char name[50];
    printf("请输入要删除的联系人姓名: ");
    scanf("%s", name);

    int index = -1;
    for (int i = 0; i < contact_count; i++) {
        if (strcmp(contacts[i].name, name) == 0) {
            index = i;
            break;
        }
    }

    if (index == -1) {
        printf("未找到该联系人。\n");
        return;
    }

    for (int i = index; i < contact_count - 1; i++) {
        contacts[i] = contacts[i + 1];
    }

    contact_count--;
    printf("联系人删除成功。\n");
}

void find_contact() {
    char name[50];
    printf("请输入要查找的联系人姓名: ");
    scanf("%s", name);

    for (int i = 0; i < contact_count; i++) {
        if (strcmp(contacts[i].name, name) == 0) {
            printf("姓名: %s\n", contacts[i].name);
            printf("电话号码: %s\n", contacts[i].phone);
            printf("电子邮件: %s\n", contacts[i].email);
            return;
        }
    }

    printf("未找到该联系人。\n");
}

void modify_contact() {
    char name[50];
    printf("请输入要修改的联系人姓名: ");
    scanf("%s", name);

    for (int i = 0; i < contact_count; i++) {
        if (strcmp(contacts[i].name, name) == 0) {
            printf("请输入新的电话号码: ");
            scanf("%s", contacts[i].phone);
            printf("请输入新的电子邮件: ");
            scanf("%s", contacts[i].email);
            printf("联系人信息修改成功。\n");
            return;
        }
    }

    printf("未找到该联系人。\n");
}

void display_contacts() {
    if (contact_count == 0) {
        printf("通讯录为空。\n");
        return;
    }

    for (int i = 0; i < contact_count; i++) {
        printf("联系人 %d:\n", i + 1);
        printf("姓名: %s\n", contacts[i].name);
        printf("电话号码: %s\n", contacts[i].phone);
        printf("电子邮件: %s\n", contacts[i].email);
        printf("\n");
    }
}

void save_contacts() {
    FILE *file = fopen("contacts.dat", "wb");
    if (file == NULL) {
        printf("无法打开文件。\n");
        return;
    }

    fwrite(&contact_count, sizeof(int), 1, file);
    fwrite(contacts, sizeof(Contact), contact_count, file);

    fclose(file);
    printf("通讯录保存成功。\n");
}

void load_contacts() {
    FILE *file = fopen("contacts.dat", "rb");
    if (file == NULL) {
        printf("无法打开文件。\n");
        return;
    }

    fread(&contact_count, sizeof(int), 1, file);
    fread(contacts, sizeof(Contact), contact_count, file);

    fclose(file);
    printf("通讯录加载成功。\n");
}

int main() {
    int choice;
    while (1) {
        printf("\n通讯录管理系统\n");
        printf("1. 添加联系人\n");
        printf("2. 删除联系人\n");
        printf("3. 查找联系人\n");
        printf("4. 修改联系人\n");
        printf("5. 显示所有联系人\n");
        printf("6. 保存通讯录\n");
        printf("7. 加载通讯录\n");
        printf("8. 退出\n");
        printf("请选择操作: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                add_contact();
                break;
            case 2:
                delete_contact();
                break;
            case 3:
                find_contact();
                break;
            case 4:
                modify_contact();
                break;
            case 5:
                display_contacts();
                break;
            case 6:
                save_contacts();
                break;
            case 7:
                load_contacts();
                break;
            case 8:
                return 0;
            default:
                printf("无效的选择,请重新输入。\n");
        }
    }

    return 0;
}

动态实现通讯录

数据结构设计

在动态实现中,我们通常使用链表来存储联系人信息。每个联系人可以定义为一个结构体,包含姓名、电话号码、电子邮件等信息,以及指向下一个联系人的指针。

typedef struct ContactNode {
    char name[50];
    char phone[20];
    char email[50];
    struct ContactNode *next;
} ContactNode;

ContactNode *head = NULL;

功能实现

  1. 添加联系人
    • 创建新节点并插入到链表中。
void add_contact() {
    ContactNode *new_contact = (ContactNode *)malloc(sizeof(ContactNode));
    if (new_contact == NULL) {
        printf("内存分配失败。\n");
        return;
    }

    printf("请输入姓名: ");
    scanf("%s", new_contact->name);
    printf("请输入电话号码: ");
    scanf("%s", new_contact->phone);
    printf("请输入电子邮件: ");
    scanf("%s", new_contact->email);

    new_contact->next = head;
    head = new_contact;
    printf("联系人添加成功。\n");
}
  1. 删除联系人
    • 根据姓名查找联系人并删除节点。
void delete_contact() {
    char name[50];
    printf("请输入要删除的联系人姓名: ");
    scanf("%s", name);

    ContactNode *current = head;
    ContactNode *previous = NULL;

    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            if (previous == NULL) {
                head = current->next;
            } else {
                previous->next = current->next;
            }
            free(current);
            printf("联系人删除成功。\n");
            return;
        }
        previous = current;
        current = current->next;
    }

    printf("未找到该联系人。\n");
}
  1. 查找联系人
    • 根据姓名查找联系人并显示其信息。
void find_contact() {
    char name[50];
    printf("请输入要查找的联系人姓名: ");
    scanf("%s", name);

    ContactNode *current = head;
    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            printf("姓名: %s\n", current->name);
            printf("电话号码: %s\n", current->phone);
            printf("电子邮件: %s\n", current->email);
            return;
        }
        current = current->next;
    }

    printf("未找到该联系人。\n");
}
  1. 修改联系人
    • 根据姓名查找联系人并修改其信息。
void modify_contact() {
    char name[50];
    printf("请输入要修改的联系人姓名: ");
    scanf("%s", name);

    ContactNode *current = head;
    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            printf("请输入新的电话号码: ");
            scanf("%s", current->phone);
            printf("请输入新的电子邮件: ");
            scanf("%s", current->email);
            printf("联系人信息修改成功。\n");
            return;
        }
        current = current->next;
    }

    printf("未找到该联系人。\n");
}
  1. 显示所有联系人
    • 遍历链表并显示所有联系人的信息。
void display_contacts() {
    if (head == NULL) {
        printf("通讯录为空。\n");
        return;
    }

    ContactNode *current = head;
    int count = 1;
    while (current != NULL) {
        printf("联系人 %d:\n", count++);
        printf("姓名: %s\n", current->name);
        printf("电话号码: %s\n", current->phone);
        printf("电子邮件: %s\n", current->email);
        printf("\n");
        current = current->next;
    }
}
  1. 保存通讯录
    • 将通讯录保存到文件中。
void save_contacts() {
    FILE *file = fopen("contacts.dat", "wb");
    if (file == NULL) {
        printf("无法打开文件。\n");
        return;
    }

    ContactNode *current = head;
    while (current != NULL) {
        fwrite(current, sizeof(ContactNode), 1, file);
        current = current->next;
    }

    fclose(file);
    printf("通讯录保存成功。\n");
}
  1. 加载通讯录
    • 从文件中加载通讯录。
void load_contacts() {
    FILE *file = fopen("contacts.dat", "rb");
    if (file == NULL) {
        printf("无法打开文件。\n");
        return;
    }

    ContactNode *current = NULL;
    ContactNode *previous = NULL;
    while (1) {
        ContactNode *new_contact = (ContactNode *)malloc(sizeof(ContactNode));
        if (fread(new_contact, sizeof(ContactNode), 1, file) != 1) {
            free(new_contact);
            break;
        }
        new_contact->next = NULL;

        if (head == NULL) {
            head = new_contact;
        } else {
            previous->next = new_contact;
        }
        previous = new_contact;
    }

    fclose(file);
    printf("通讯录加载成功。\n");
}

代码示例

”`c #include #include #include

typedef struct ContactNode { char name[50]; char phone[20]; char email[50]; struct ContactNode *next; } ContactNode;

ContactNode *head = NULL;

void add_contact() { ContactNode *new_contact = (ContactNode *)malloc(sizeof(ContactNode)); if (new_contact == NULL) { printf(“内存分配失败。\n”); return; }

printf("请输入姓名: ");
scanf("%s", new_contact->name);
printf("请输入电话号码: ");
scanf("%s", new_contact->phone);
printf("请输入电子邮件: ");
scanf("%s", new_contact->email);

new_contact->next = head;
head = new_contact;
printf("联系人添加成功。\n");

}

void delete_contact() { char name[50]; printf(“请输入要删除的联系人姓名: “); scanf(”%s”, name);

ContactNode *current = head;
ContactNode *previous = NULL;

while (current != NULL) {
    if (strcmp(current->name, name) == 0) {
        if (previous == NULL) {
            head = current->next;
        } else {
            previous->next = current->next;
        }
        free(current);
        printf("联系人删除成功。\n");
        return;
    }
    previous = current;
    current = current->next;
}

printf
推荐阅读:
  1. 菜鸟如何快速理解实现通讯录——静态方法
  2. 通讯录的实现(一)静态实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c语言

上一篇:Go语言单元测试的示例分析

下一篇:MySQL索引创建原则的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》