C++ 错题本管理系统:使用链表实现,附详细注释
#include <iostream>
#include <fstream>
using namespace std;
struct Node {
string question;
string errorInfo;
string answerAnalysis;
string experience;
string source;
string cause;
string difficulty;
string type;
string knowledgePoint;
Node* next;
};
class ErrorBook {
private:
Node* head;
public:
ErrorBook() {
head = nullptr;
}
// 添加错题信息
void addError() {
Node* newNode = new Node;
cout << '请输入题目:' << endl;
cin.ignore();
getline(cin, newNode->question);
cout << '请输入错误信息:' << endl;
getline(cin, newNode->errorInfo);
cout << '请输入答案分析:' << endl;
getline(cin, newNode->answerAnalysis);
cout << '请输入心得体会:' << endl;
getline(cin, newNode->experience);
cout << '请输入错题来源:' << endl;
getline(cin, newNode->source);
cout << '请输入错题原因:' << endl;
getline(cin, newNode->cause);
cout << '请输入难易程度:' << endl;
getline(cin, newNode->difficulty);
cout << '请输入题目类型:' << endl;
getline(cin, newNode->type);
cout << '请输入知识点:' << endl;
getline(cin, newNode->knowledgePoint);
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
cout << '成功添加错题信息!' << endl;
}
// 浏览错题信息
void displayErrors() {
if (head == nullptr) {
cout << '暂无错题信息!' << endl;
} else {
Node* temp = head;
while (temp != nullptr) {
cout << '题目:' << temp->question << endl;
cout << '错误信息:' << temp->errorInfo << endl;
cout << '答案分析:' << temp->answerAnalysis << endl;
cout << '心得体会:' << temp->experience << endl;
cout << '错题来源:' << temp->source << endl;
cout << '错题原因:' << temp->cause << endl;
cout << '难易程度:' << temp->difficulty << endl;
cout << '题目类型:' << temp->type << endl;
cout << '知识点:' << temp->knowledgePoint << endl;
cout << endl;
temp = temp->next;
}
}
}
// 查询错题信息
void searchErrors() {
int choice;
cout << '请选择查询方式:' << endl;
cout << '1. 按知识点查询' << endl;
cout << '2. 按题目类型查询' << endl;
cin >> choice;
string keyword;
cout << '请输入查询关键字:' << endl;
cin.ignore();
getline(cin, keyword);
Node* temp = head;
bool found = false;
while (temp != nullptr) {
if (choice == 1 && temp->knowledgePoint == keyword) {
found = true;
cout << '题目:' << temp->question << endl;
cout << '错误信息:' << temp->errorInfo << endl;
cout << '答案分析:' << temp->answerAnalysis << endl;
cout << '心得体会:' << temp->experience << endl;
cout << '错题来源:' << temp->source << endl;
cout << '错题原因:' << temp->cause << endl;
cout << '难易程度:' << temp->difficulty << endl;
cout << '题目类型:' << temp->type << endl;
cout << '知识点:' << temp->knowledgePoint << endl;
cout << endl;
} else if (choice == 2 && temp->type == keyword) {
found = true;
cout << '题目:' << temp->question << endl;
cout << '错误信息:' << temp->errorInfo << endl;
cout << '答案分析:' << temp->answerAnalysis << endl;
cout << '心得体会:' << temp->experience << endl;
cout << '错题来源:' << temp->source << endl;
cout << '错题原因:' << temp->cause << endl;
cout << '难易程度:' << temp->difficulty << endl;
cout << '题目类型:' << temp->type << endl;
cout << '知识点:' << temp->knowledgePoint << endl;
cout << endl;
}
temp = temp->next;
}
if (!found) {
cout << '未找到相关错题信息!' << endl;
}
}
// 修改错题信息
void modifyError() {
string keyword;
cout << '请输入要修改的题目:' << endl;
cin.ignore();
getline(cin, keyword);
Node* temp = head;
bool found = false;
while (temp != nullptr) {
if (temp->question == keyword) {
found = true;
cout << '请输入新的错误信息:' << endl;
getline(cin, temp->errorInfo);
cout << '请输入新的答案分析:' << endl;
getline(cin, temp->answerAnalysis);
cout << '请输入新的心得体会:' << endl;
getline(cin, temp->experience);
cout << '请输入新的错题来源:' << endl;
getline(cin, temp->source);
cout << '请输入新的错题原因:' << endl;
getline(cin, temp->cause);
cout << '请输入新的难易程度:' << endl;
getline(cin, temp->difficulty);
cout << '请输入新的题目类型:' << endl;
getline(cin, temp->type);
cout << '请输入新的知识点:' << endl;
getline(cin, temp->knowledgePoint);
cout << '成功修改错题信息!' << endl;
}
temp = temp->next;
}
if (!found) {
cout << '未找到该题目的错题信息!' << endl;
}
}
// 删除错题信息
void deleteError() {
string keyword;
cout << '请输入要删除的知识点:' << endl;
cin.ignore();
getline(cin, keyword);
Node* prev = nullptr;
Node* curr = head;
bool found = false;
while (curr != nullptr) {
if (curr->knowledgePoint == keyword) {
found = true;
if (prev == nullptr) {
head = curr->next;
delete curr;
curr = head;
} else {
prev->next = curr->next;
delete curr;
curr = prev->next;
}
cout << '成功删除错题信息!' << endl;
} else {
prev = curr;
curr = curr->next;
}
}
if (!found) {
cout << '未找到该知识点的错题信息!' << endl;
}
}
// 文件导入
void importFromFile() {
string filename;
cout << '请输入文件名:' << endl;
cin >> filename;
ifstream inputFile;
inputFile.open(filename);
if (!inputFile) {
cout << '文件打开失败!' << endl;
return;
}
while (!inputFile.eof()) {
Node* newNode = new Node;
getline(inputFile, newNode->question);
getline(inputFile, newNode->errorInfo);
getline(inputFile, newNode->answerAnalysis);
getline(inputFile, newNode->experience);
getline(inputFile, newNode->source);
getline(inputFile, newNode->cause);
getline(inputFile, newNode->difficulty);
getline(inputFile, newNode->type);
getline(inputFile, newNode->knowledgePoint);
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}
inputFile.close();
cout << '成功导入错题信息!' << endl;
}
// 文件导出
void exportToFile() {
string filename;
cout << '请输入文件名:' << endl;
cin >> filename;
ofstream outputFile;
outputFile.open(filename);
if (!outputFile) {
cout << '文件打开失败!' << endl;
return;
}
Node* temp = head;
while (temp != nullptr) {
outputFile << temp->question << endl;
outputFile << temp->errorInfo << endl;
outputFile << temp->answerAnalysis << endl;
outputFile << temp->experience << endl;
outputFile << temp->source << endl;
outputFile << temp->cause << endl;
outputFile << temp->difficulty << endl;
outputFile << temp->type << endl;
outputFile << temp->knowledgePoint << endl;
temp = temp->next;
}
outputFile.close();
cout << '成功导出错题信息!' << endl;
}
};
int main() {
ErrorBook errorBook;
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;
switch (choice) {
case 1:
errorBook.addError();
break;
case 2:
errorBook.displayErrors();
break;
case 3:
errorBook.searchErrors();
break;
case 4:
errorBook.deleteError();
break;
case 5:
errorBook.importFromFile();
break;
case 6:
errorBook.exportToFile();
break;
case 7:
return 0;
default:
cout << '无效的选择!' << endl;
break;
}
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/qqax 著作权归作者所有。请勿转载和采集!