C++ 链表实现错题本管理系统

本程序使用 C++ 链表结构实现了一个错题本管理系统,帮助用户高效整理错题,提升学习效率。系统支持以下功能:

  1. 添加错题信息: 输入题目、错误信息、答案分析、心得体会、错题来源、错题原因、难易程度、题目类型和知识点等信息,将错题添加到错题本中。
  2. 浏览错题信息: 显示所有已添加的错题信息,方便用户回顾和查阅。
  3. 查询错题信息: 提供按知识点和题目类型两种查询方式,帮助用户快速定位特定类型的错题。
  4. 修改错题信息: 允许用户修改已添加的错题信息,保持错题本的准确性。
  5. 删除错题信息: 提供按知识点和题目类型查找后删除错题的功能,方便用户清理过时的错题。
  6. 文件导入: 从文件中读取若干条错题信息,方便用户导入已有错题数据。
  7. 文件导出: 将错题信息输出到文件中,方便用户备份或分享错题。

程序菜单

错题本管理程序 1 添加错题信息 2 浏览错题信息 3 查询错题信息 4 删除错题信息 5 文件导入 6 文件导出 7 退出 请选择功能:

代码示例

#include <iostream>
#include <fstream>
using namespace std;

struct WrongQuestion {
    string question;
    string mistake;
    string analysis;
    string experience;
    string source;
    string reason;
    string difficulty;
    string type;
    string knowledge;
    WrongQuestion* next;
};

class WrongQuestionManager {
private:
    WrongQuestion* head;

public:
    WrongQuestionManager() {
        head = nullptr;
    }

    void displayMenu() {
        cout << '错题本管理程序' << endl;
        cout << '1 添加错题信息' << endl;
        cout << '2 浏览错题信息' << endl;
        cout << '3 查询错题信息' << endl;
        cout << '4 删除错题信息' << endl;
        cout << '5 文件导入' << endl;
        cout << '6 文件导出' << endl;
        cout << '7 退出' << endl;
        cout << '请选择功能:';
    }

    void addWrongQuestion() {
        WrongQuestion* newQuestion = new WrongQuestion;
        cout << '请输入题目:';
        getline(cin, newQuestion->question);
        cout << '请输入错误信息:';
        getline(cin, newQuestion->mistake);
        cout << '请输入答案分析:';
        getline(cin, newQuestion->analysis);
        cout << '请输入心得体会:';
        getline(cin, newQuestion->experience);
        cout << '请输入错题来源:';
        getline(cin, newQuestion->source);
        cout << '请输入错题原因:';
        getline(cin, newQuestion->reason);
        cout << '请输入难易程度:';
        getline(cin, newQuestion->difficulty);
        cout << '请输入题目类型:';
        getline(cin, newQuestion->type);
        cout << '请输入知识点:';
        getline(cin, newQuestion->knowledge);

        newQuestion->next = head;
        head = newQuestion;

        cout << '错题信息添加成功!' << endl;
    }

    void browseWrongQuestions() {
        WrongQuestion* current = head;

        if (current == nullptr) {
            cout << '暂无错题信息!' << endl;
        } else {
            while (current != nullptr) {
                cout << '题目:' << current->question << endl;
                cout << '错误信息:' << current->mistake << endl;
                cout << '答案分析:' << current->analysis << endl;
                cout << '心得体会:' << current->experience << endl;
                cout << '错题来源:' << current->source << endl;
                cout << '错题原因:' << current->reason << endl;
                cout << '难易程度:' << current->difficulty << endl;
                cout << '题目类型:' << current->type << endl;
                cout << '知识点:' << current->knowledge << endl;
                cout << endl;

                current = current->next;
            }
        }
    }

    void searchWrongQuestionsByKnowledge() {
        string targetKnowledge;
        cout << '请输入要查询的知识点:';
        getline(cin, targetKnowledge);

        WrongQuestion* current = head;
        bool found = false;

        while (current != nullptr) {
            if (current->knowledge == targetKnowledge) {
                cout << '题目:' << current->question << endl;
                cout << '错误信息:' << current->mistake << endl;
                cout << '答案分析:' << current->analysis << endl;
                cout << '心得体会:' << current->experience << endl;
                cout << '错题来源:' << current->source << endl;
                cout << '错题原因:' << current->reason << endl;
                cout << '难易程度:' << current->difficulty << endl;
                cout << '题目类型:' << current->type << endl;
                cout << '知识点:' << current->knowledge << endl;
                cout << endl;

                found = true;
            }

            current = current->next;
        }

        if (!found) {
            cout << '未找到符合条件的错题!' << endl;
        }
    }

    void searchWrongQuestionsByType() {
        string targetType;
        cout << '请输入要查询的题目类型:';
        getline(cin, targetType);

        WrongQuestion* current = head;
        bool found = false;

        while (current != nullptr) {
            if (current->type == targetType) {
                cout << '题目:' << current->question << endl;
                cout << '错误信息:' << current->mistake << endl;
                cout << '答案分析:' << current->analysis << endl;
                cout << '心得体会:' << current->experience << endl;
                cout << '错题来源:' << current->source << endl;
                cout << '错题原因:' << current->reason << endl;
                cout << '难易程度:' << current->difficulty << endl;
                cout << '题目类型:' << current->type << endl;
                cout << '知识点:' << current->knowledge << endl;
                cout << endl;

                found = true;
            }

            current = current->next;
        }

        if (!found) {
            cout << '未找到符合条件的错题!' << endl;
        }
    }

    void deleteWrongQuestions() {
        string targetKnowledge;
        string targetType;
        cout << '请输入要删除的知识点:';
        getline(cin, targetKnowledge);
        cout << '请输入要删除的题目类型:';
        getline(cin, targetType);

        WrongQuestion* current = head;
        WrongQuestion* previous = nullptr;
        bool found = false;

        while (current != nullptr) {
            if (current->knowledge == targetKnowledge && current->type == targetType) {
                if (previous == nullptr) {
                    head = current->next;
                } else {
                    previous->next = current->next;
                }

                delete current;
                found = true;
            } else {
                previous = current;
            }

            current = current->next;
        }

        if (found) {
            cout << '错题信息删除成功!' << endl;
        } else {
            cout << '未找到符合条件的错题!' << endl;
        }
    }

    void importFromFile() {
        string filename;
        cout << '请输入文件名:';
        getline(cin, filename);

        ifstream file(filename);
        if (!file) {
            cout << '文件打开失败!' << endl;
            return;
        }

        while (!file.eof()) {
            WrongQuestion* newQuestion = new WrongQuestion;

            getline(file, newQuestion->question);
            getline(file, newQuestion->mistake);
            getline(file, newQuestion->analysis);
            getline(file, newQuestion->experience);
            getline(file, newQuestion->source);
            getline(file, newQuestion->reason);
            getline(file, newQuestion->difficulty);
            getline(file, newQuestion->type);
            getline(file, newQuestion->knowledge);

            newQuestion->next = head;
            head = newQuestion;
        }

        cout << '错题信息导入成功!' << endl;
    }

    void exportToFile() {
        string filename;
        cout << '请输入文件名:';
        getline(cin, filename);

        ofstream file(filename);
        if (!file) {
            cout << '文件打开失败!' << endl;
            return;
        }

        WrongQuestion* current = head;

        while (current != nullptr) {
            file << current->question << endl;
            file << current->mistake << endl;
            file << current->analysis << endl;
            file << current->experience << endl;
            file << current->source << endl;
            file << current->reason << endl;
            file << current->difficulty << endl;
            file << current->type << endl;
            file << current->knowledge << endl;

            current = current->next;
        }

        cout << '错题信息导出成功!' << endl;
    }

    void run() {
        int option;

        do {
            displayMenu();
            cin >> option;
            cin.ignore();

            switch (option) {
                case 1:
                    addWrongQuestion();
                    break;
                case 2:
                    browseWrongQuestions();
                    break;
                case 3:
                    searchMenu();
                    break;
                case 4:
                    deleteWrongQuestions();
                    break;
                case 5:
                    importFromFile();
                    break;
                case 6:
                    exportToFile();
                    break;
                case 7:
                    cout << '程序已退出!' << endl;
                    break;
                default:
                    cout << '无效选项,请重新选择!' << endl;
                    break;
            }

            cout << endl;
        } while (option != 7);
    }

private:
    void searchMenu() {
        int option;
        cout << '查询方式:' << endl;
        cout << '1 按知识点查询' << endl;
        cout << '2 按题目类型查询' << endl;
        cout << '请选择查询方式:';
        cin >> option;
        cin.ignore();

        switch (option) {
            case 1:
                searchWrongQuestionsByKnowledge();
                break;
            case 2:
                searchWrongQuestionsByType();
                break;
            default:
                cout << '无效选项,请重新选择!' << endl;
                break;
        }
    }
};

int main() {
    WrongQuestionManager manager;
    manager.run();

    return 0;
}

程序说明

  • 该程序使用链表来存储错题信息,每个WrongQuestion结构体代表一条错题,通过next指针连接形成链表。
  • 程序提供了菜单选项供用户选择不同功能,包括添加、浏览、查询、修改、删除错题信息以及文件导入导出等。
  • 输入输出使用getline(cin, variable)避免输入输出冲突。
  • 文件导入导出使用fstream库实现文件读取写入操作。

总结

本程序提供了一个使用 C++ 链表结构实现的错题本管理系统,方便用户高效整理错题,提升学习效率。您可以根据自己的需求修改和完善代码,打造个性化的错题管理工具。


原文地址: https://www.cveoy.top/t/topic/qzAW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录