C++ 错题本管理系统:高效整理错题,提升学习效率
#include
struct Mistake// 定义错题的结构体 { string question; // 题目 string fault; // 错误信息 string analysis; // 答案分析 string experience; // 心得体会 string source; // 错题来源 string reason; // 错题原因 string difficulty; // 难易程度 string type; // 题目类型 string knowledgePoint; // 知识点 };
struct Node// 定义链表节点 { Mistake data; Node* next; };
void addMistake(Node*& head)// 添加错题信息 { Mistake mistake; cout << "请输入题目:"; getline(cin, mistake.question); cout << "请输入错误信息:"; getline(cin, mistake.fault); cout << "请输入答案分析:"; getline(cin, mistake.analysis); cout << "请输入心得体会:"; getline(cin, mistake.experience); cout << "请输入错题来源:"; getline(cin, mistake.source); cout << "请输入错题原因:"; getline(cin, mistake.reason); cout << "请输入难易程度:"; getline(cin, mistake.difficulty); cout << "请输入题目类型:"; getline(cin, mistake.type); cout << "请输入知识点:"; getline(cin, mistake.knowledgePoint); Node* newNode = new Node; newNode->data = mistake; newNode->next = nullptr; if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } cout << "错题信息添加成功!" << endl; }
void displayMistakes(Node* head)// 显示所有错题信息 { if (head == nullptr) { cout << "没有任何错题信息!" << endl; return; } Node* current = head; while (current != nullptr) { Mistake mistake = current->data; cout << "题目:" << mistake.question << endl; cout << "错误信息:" << mistake.fault << endl; cout << "答案分析:" << mistake.analysis << endl; cout << "心得体会:" << mistake.experience << endl; cout << "错题来源:" << mistake.source << endl; cout << "错题原因:" << mistake.reason << endl; cout << "难易程度:" << mistake.difficulty << endl; cout << "题目类型:" << mistake.type << endl; cout << "知识点:" << mistake.knowledgePoint << endl; cout << "-------------------------" << endl; current = current->next; } }
void searchMistakes(Node* head)// 查询错题信息 { if (head == nullptr) { cout << "没有任何错题信息!" << endl; return; } string keyword; cout << "请输入要查询的关键字:"; getline(cin, keyword); Node* current = head; int count = 0; while (current != nullptr) { Mistake mistake = current->data; if (mistake.knowledgePoint == keyword || mistake.type == keyword) { cout << "题目:" << mistake.question << endl; cout << "错误信息:" << mistake.fault << endl; cout << "答案分析:" << mistake.analysis << endl; cout << "心得体会:" << mistake.experience << endl; cout << "错题来源:" << mistake.source << endl; cout << "错题原因:" << mistake.reason << endl; cout << "难易程度:" << mistake.difficulty << endl; cout << "题目类型:" << mistake.type << endl; cout << "知识点:" << mistake.knowledgePoint << endl; cout << "-------------------------" << endl; count++; } current = current->next; } if (count == 0) { cout << "没有找到相关错题信息!" << endl; } }
void modifyMistake(Node* head)// 修改错题信息 { if (head == nullptr) { cout << "没有任何错题信息!" << endl; return; } string keyword; cout << "请输入要修改的关键字:"; getline(cin, keyword); Node* current = head; vector<Node*> matches; while (current != nullptr) { Mistake mistake = current->data; if (mistake.knowledgePoint == keyword || mistake.type == keyword) { matches.push_back(current); } current = current->next; } if (matches.empty()) { cout << "没有找到相关错题信息!" << endl; return; } cout << "找到以下匹配的错题信息:" << endl; for (int i = 0; i < matches.size(); i++) { Mistake mistake = matches[i]->data; cout << i + 1 << ". " << "题目:" << mistake.question << endl; cout << " 错误信息:" << mistake.fault << endl; cout << " 答案分析:" << mistake.analysis << endl; cout << " 心得体会:" << mistake.experience << endl; cout << " 错题来源:" << mistake.source << endl; cout << " 错题原因:" << mistake.reason << endl; cout << " 难易程度:" << mistake.difficulty << endl; cout << " 题目类型:" << mistake.type << endl; cout << " 知识点:" << mistake.knowledgePoint << endl; cout << "-------------------------" << endl; } int choice; cout << "请选择要修改的错题序号:"; cin >> choice; cin.ignore(); // 忽略之前的换行符 if (choice < 1 || choice > matches.size()) { cout << "输入的序号无效!" << endl; return; } Node* selectedNode = matches[choice - 1]; Mistake& selectedMistake = selectedNode->data; cout << "请输入新的题目(原题目:" << selectedMistake.question << "):"; getline(cin, selectedMistake.question); cout << "请输入新的错误信息(原错误信息:" << selectedMistake.fault << "):"; getline(cin, selectedMistake.fault); cout << "请输入新的答案分析(原答案分析:" << selectedMistake.analysis << "):"; getline(cin, selectedMistake.analysis); cout << "请输入新的心得体会(原心得体会:" << selectedMistake.experience << "):"; getline(cin, selectedMistake.experience); cout << "请输入新的错题来源(原错题来源:" << selectedMistake.source << "):"; getline(cin, selectedMistake.source); cout << "请输入新的错题原因(原错题原因:" << selectedMistake.reason << "):"; getline(cin, selectedMistake.reason); cout << "请输入新的难易程度(原难易程度:" << selectedMistake.difficulty << "):"; getline(cin, selectedMistake.difficulty); cout << "请输入新的题目类型(原题目类型:" << selectedMistake.type << "):"; getline(cin, selectedMistake.type); cout << "请输入新的知识点(原知识点:" << selectedMistake.knowledgePoint << "):"; getline(cin, selectedMistake.knowledgePoint); cout << "错题信息修改成功!" << endl; }
void deleteMistakes(Node*& head)// 删除错题信息 { if (head == nullptr) { cout << "没有任何错题信息!" << endl; return; } string keyword; cout << "请输入要删除的关键字:"; getline(cin, keyword); Node* current = head; Node* prev = nullptr; int count = 0; while (current != nullptr) { Mistake mistake = current->data; if (mistake.knowledgePoint == keyword || mistake.type == keyword) { if (prev == nullptr) { head = current->next; } else { prev->next = current->next; } delete current; count++; } else { prev = current; } current = current->next; } if (count == 0) { cout << "没有找到相关错题信息!" << endl; } else { cout << "成功删除 " << count << " 条错题信息!" << endl; } }
void importMistakes(Node*& head)// 文件导入 { string filename; cout << "请输入要导入的文件名:"; cin >> filename; cin.ignore(); // 忽略之前的换行符
ifstream file(filename);
if (!file.is_open())
{
cout << "无法打开文件!" << endl;
return;
}
int count = 0;// 文件导入
while (!file.eof())
{
Mistake mistake;
getline(file, mistake.question);
getline(file, mistake.fault);
getline(file, mistake.analysis);
getline(file, mistake.experience);
getline(file, mistake.source);
getline(file, mistake.reason);
getline(file, mistake.difficulty);
getline(file, mistake.type);
getline(file, mistake.knowledgePoint);
Node* newNode = new Node;
newNode->data = mistake;
newNode->next = nullptr;
if (head == nullptr)
{
head = newNode;
}
else
{
Node* current = head;
while (current->next != nullptr)
{
current = current->next;
}
current->next = newNode;
}
count++;
}
file.close();
cout << "成功导入 " << count << " 条错题信息!" << endl;
}
void exportMistakes(Node* head)// 文件导出 { if (head == nullptr) { cout << "没有任何错题信息!" << endl; return; } string filename; cout << "请输入要导出的文件名:"; cin >> filename; cin.ignore(); // 忽略之前的换行符 ofstream file(filename); if (!file.is_open()) { cout << "无法打开文件!" << endl; return; } Node* current = head; while (current != nullptr) { Mistake mistake = current->data; file << mistake.question << endl; file << mistake.fault << endl; file << mistake.analysis << endl; file << mistake.experience << endl; file << mistake.source << endl; file << mistake.reason << endl; file << mistake.difficulty << endl; file << mistake.type << endl; file << mistake.knowledgePoint << endl; current = current->next; } file.close(); cout << "成功导出错题信息到文件!" << endl; }
void releaseMemory(Node*& head)// 释放链表内存 { Node* current = head; while (current != nullptr) { Node* temp = current; current = current->next; delete temp; } head = nullptr; }
int main() { Node* head = nullptr; int choice; while (true) { cout << "错题本管理程序" << endl; cout << "1 添加错题信息" << endl; cout << "2 浏览错题信息" << endl; cout << "3 查询错题信息" << endl; cout << "4 删除错题信息" << endl; cout << "5 文件导入" << endl; cout << "6 文件导出" << endl; cout << "7 退出" << endl; cout << "请选择功能:"; cin >> choice; cin.ignore(); // 忽略之前的换行符 switch (choice) { case 1: addMistake(head); break; case 2: displayMistakes(head); break; case 3: searchMistakes(head); break; case 4: deleteMistakes(head); break; case 5: importMistakes(head); break; case 6: exportMistakes(head); break; case 7: releaseMemory(head); return 0; default: cout << "输入的选项无效!" << endl; break; } cout << endl; } }
原文地址: https://www.cveoy.top/t/topic/qzC7 著作权归作者所有。请勿转载和采集!