weixin_54262993 2023-06-13 17:05 采纳率: 0%
浏览 6

c语言通讯录管理系统如何实现菜单交互

即可以根据用户输入的命令,选择不同的操作 。
然后能够保存每次更新的数据。
用线性表实现。

  • 写回答

2条回答 默认 最新

  • 憧憬blog 2023-06-13 17:39
    关注

    实现菜单交互可以使用循环和条件语句,根据用户输入的命令选择不同的操作。具体步骤如下:

    1. 定义一个循环,让程序可以一直运行,直到用户选择退出。
    2. 在循环中,输出菜单选项,让用户选择需要执行的操作,可以使用 switch 语句实现不同命令的分支处理。
    3. 根据用户选择的命令执行相应的操作,例如添加联系人、删除联系人、修改联系人信息、查找联系人、显示联系人列表等。
    4. 对每次更新的数据进行保存,可以将联系人信息保存在文件中,每次程序启动时从文件中加载数据。也可以将联系人信息保存在链表中,每次更新后将链表保存到文件中,程序启动时从文件中加载链表。

    以下是一个使用链表实现通讯录管理系统的示例代码,其中包含菜单交互和数据保存功能:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_NAME_LEN 20
    #define MAX_PHONE_LEN 11
    
    // 联系人结构体
    typedef struct Contact {
        char name[MAX_NAME_LEN + 1]; // 联系人姓名
        char phone[MAX_PHONE_LEN + 1]; // 联系人电话
        struct Contact *next; // 指向下一个联系人的指针
    } Contact;
    
    // 显示菜单
    void displayMenu() {
        printf("Welcome to the contact management system.\n");
        printf("1. Add a new contact\n");
        printf("2. Delete a contact\n");
        printf("3. Modify a contact\n");
        printf("4. Search for a contact\n");
        printf("5. Display all contacts\n");
        printf("6. Quit\n");
        printf("Please enter the corresponding number to select an operation: ");
    }
    
    // 添加一个联系人
    void addContact(Contact **head) {
        Contact *newContact = (Contact*)malloc(sizeof(Contact));
        printf("Please enter the name of the new contact: ");
        scanf("%s", newContact->name);
        printf("Please enter the phone number of the new contact: ");
        scanf("%s", newContact->phone);
        newContact->next = *head;
        *head = newContact;
        printf("The new contact has been added successfully.\n");
    }
    
    // 删除一个联系人
    void deleteContact(Contact **head) {
        char name[MAX_NAME_LEN + 1];
        printf("Please enter the name of the contact to delete: ");
        scanf("%s", name);
        Contact *prev = NULL;
        Contact *current = *head;
        while (current != NULL) {
            if (strcmp(current->name, name) == 0) {
                if (prev == NULL) {
                    *head = current->next;
                } else {
                    prev->next = current->next;
                }
                free(current);
                printf("The contact has been deleted successfully.\n");
                return;
            }
            prev = current;
            current = current->next;
        }
        printf("The contact with the name %s does not exist.\n", name);
    }
    
    // 修改一个联系人的信息
    void modifyContact(Contact *head) {
        char name[MAX_NAME_LEN + 1];
        printf("Please enter the name of the contact to modify: ");
        scanf("%s", name);
        Contact *current = head;
        while (current != NULL) {
            if (strcmp(current->name, name) == 0) {
                printf("Please enter the new phone number of the contact: ");
                scanf("%s", current->phone);
                printf("The contact has been modified successfully.\n");
                return;
            }
            current = current->next;
        }
        printf("The contact with the name %s does not exist.\n", name);
    }
    
    // 查找一个联系人
    void searchContact(Contact *head) {
        char name[MAX_NAME_LEN + 1];
        printf("Please enter the name of the contact to search: ");
        scanf("%s", name);
        Contact *current = head;
        while (current != NULL) {
            if (strcmp(current->name, name) == 0) {
                printf("Name: %s\n", current->name);
                printf("Phone number: %s\n", current->phone);
                return;
            }
            current = current->next;
        }
        printf("The contact with the name %s does not exist.\n", name);
    }
    
    // 显示所有联系人
    void displayContacts(Contact *head) {
        if (head == NULL) {
            printf("There are no contacts.\n");
            return;
        }
        printf("The contact list is as follows:\n");
        Contact *current = head;
        while (current != NULL) {
            printf("Name: %s\n", current->name);
            printf("Phone number: %s\n", current->phone);
            current = current->next;
        }
    }
    
    // 保存联系人信息到文件
    void saveContactsToFile(Contact *head, const char *filename) {
        FILE *file = fopen(filename, "w");
        if (file == NULL) {
            printf("Failed to open file %s.\n", filename);
            return;
        }
        Contact *current = head;
        while (current != NULL) {
            fprintf(file, "%s,%s\n", current->name, current->phone);
            current = current->next;
        }
        fclose(file);
    }
    
    // 从文件中加载联系人信息
    void loadContactsFromFile(Contact **head, const char *filename) {
        FILE *file = fopen(filename, "r");
        if (file == NULL) {
            printf("Failed to open file %s.\n", filename);
            return;
        }
        char line[MAX_NAME_LEN + MAX_PHONE_LEN + 2];
        while (fgets(line, sizeof(line), file) != NULL) {
            char *name = strtok(line, ",");
            char *phone = strtok(NULL, ",");
            Contact *newContact = (Contact*)malloc(sizeof(Contact));
            strncpy(newContact->name, name, MAX_NAME_LEN);
            strncpy(newContact->phone, phone, MAX_PHONE_LEN);
            newContact->next = *head;
            *head = newContact;
        }
        fclose(file);
    }
    
    int main() {
        Contact *head = NULL;
        loadContactsFromFile(&head, "contacts.txt"); // 从文件中加载联系人信息
        int choice;
        do {
            displayMenu(); // 显示菜单
            scanf("%d", &choice);
            switch (choice) {
                case 1:
                    addContact(&head); // 添加联系人
                    break;
                case 2:
                    deleteContact(&head); // 删除联系人
                    break;
                case 3:
                    modifyContact(head); // 修改联系人信息
                    break;
                case 4:
                    searchContact(head); // 查找联系人
                    break;
                case 5:
                    displayContacts(head); // 显示联系人列表
                    break;
                case 6:
                    saveContactsToFile(head, "contacts.txt"); // 保存联系人信息到文件
                    printf("Goodbye!\n");
                    break;
                default:
                    printf("Invalid choice.\n");
                    break;
            }
        } while (choice != 6);
        return 0;
    }
    

    在上面的示例代码中,我们使用了链表作为数据结构来存储联系人信息,每次更新联系人信息后将链表保存到文件中,程序启动时从文件中加载链表。同时,我们也实现了菜单交互功能,根据用户输入的命令执行不同的操作。

    评论

报告相同问题?

问题事件

  • 创建了问题 6月13日

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败