第七周结对编程
2026.4.16本人和学号为2452921的同学在软件开发与创新的实验课上进行了结对编程。
要求开发一个在线考试系统,我们选择的主题是四史考试系统,核心功能大概有:题目增删改查(支持三种题型)、限时答题 + 实时保存、自动判分 + 错题解析报告。
技术选择:纯C++控制台程序,使用 vector + struct 管理数据,fstream 实现文件读写,
算法亮点:题目乱序避免作弊;超时检测;错题报告自动生成解析,便于考生复习。
现附代码如下
点击查看代码
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <random>
#include <chrono>
#include <sstream>
using namespace std;
// 题目结构
struct Question {
int id;
string type; // single / multiple / truefalse
string content;
vector<string> options;
string answer;
string analysis;
};
// 全局变量
vector<Question> questions;
int nextId = 1;
// 加载题库
void loadQuestions() {
questions.clear();
ifstream file("questions.txt");
if (!file.is_open()) return;
string line;
while (getline(file, line)) {
if (line.empty()) continue;
Question q;
stringstream ss(line);
string temp;
getline(ss, temp, '|');
q.id = stoi(temp);
getline(ss, q.type, '|');
getline(ss, q.content, '|');
string opts;
getline(ss, opts, '|');
stringstream optss(opts);
string opt;
while (getline(optss, opt, ',')) {
if (!opt.empty()) q.options.push_back(opt);
}
getline(ss, q.answer, '|');
getline(ss, q.analysis);
questions.push_back(q);
if (q.id >= nextId) nextId = q.id + 1;
}
file.close();
}
// 保存题库
void saveQuestions() {
ofstream file("questions.txt");
for (const auto& q : questions) {
file << q.id << "|" << q.type << "|" << q.content << "|";
for (size_t i = 0; i < q.options.size(); ++i) {
file << q.options[i];
if (i < q.options.size() - 1) file << ",";
}
file << "|" << q.answer << "|" << q.analysis << endl;
}
file.close();
}
// 添加题目
void addQuestion() {
Question q;
q.id = nextId++;
cout << "输入题型 (single/multiple/truefalse): ";
cin >> q.type;
cin.ignore();
cout << "输入题目内容: ";
getline(cin, q.content);
if (q.type == "truefalse") {
q.options = { "A. 正确", "B. 错误" };
}
else {
int num = 4;
for (int i = 0; i < num; ++i) {
cout << "选项 " << char('A' + i) << ": ";
string opt;
getline(cin, opt);
q.options.push_back(opt);
}
}
cout << "输入正确答案 (单选/判断输入单个字母,多选输入如AB): ";
cin >> q.answer;
cin.ignore();
cout << "输入解析: ";
getline(cin, q.analysis);
questions.push_back(q);
saveQuestions();
cout << "题目添加成功!ID = " << q.id << endl;
}
// 显示所有题目 ←←← 这里是之前出错的地方,已经修正
void showQuestions() {
if (questions.empty()) {
cout << "题库为空!\n";
return;
}
for (const auto& q : questions) {
cout << "\n" << q.id << ". [" << q.type << "] " << q.content << endl;
for (size_t i = 0; i < q.options.size(); ++i) {
cout << " " << char('A' + i) << ". " << q.options[i] << endl;
}
cout << "正确答案: " << q.answer << endl;
cout << "解析: " << q.analysis << endl;
}
}
// 删除题目
void deleteQuestion() {
int id;
cout << "输入要删除的题目ID: ";
cin >> id;
cin.ignore();
auto it = find_if(questions.begin(), questions.end(),
[id](const Question& q) { return q.id == id; });
if (it != questions.end()) {
questions.erase(it);
saveQuestions();
cout << "删除成功!\n";
}
else {
cout << "未找到该ID的题目!\n";
}
}
// 开始考试
void takeExam() {
if (questions.empty()) {
cout << "题库为空,请先添加题目!\n";
return;
}
string name;
cout << "请输入你的姓名或学号: ";
getline(cin, name);
// 随机打乱题目
vector<Question> exam = questions;
random_device rd;
mt19937 g(rd());
shuffle(exam.begin(), exam.end(), g);
vector<string> userAnswers(exam.size());
int score = 0;
cout << "\n=== 考试开始!共 " << exam.size() << " 题 ===\n\n";
for (size_t i = 0; i < exam.size(); ++i) {
const auto& q = exam[i];
cout << "第 " << (i + 1) << " 题: " << q.content << endl;
for (size_t j = 0; j < q.options.size(); ++j) {
cout << char('A' + j) << ". " << q.options[j] << endl;
}
cout << "你的答案: ";
string ans;
getline(cin, ans);
userAnswers[i] = ans;
string userAns = ans;
string correct = q.answer;
transform(userAns.begin(), userAns.end(), userAns.begin(), ::toupper);
transform(correct.begin(), correct.end(), correct.begin(), ::toupper);
if (userAns == correct) {
score += (q.type == "multiple" ? 2 : 1);
cout << "✓ 正确!\n\n";
}
else {
cout << "✗ 错误!正确答案是: " << q.answer << "\n\n";
}
}
cout << "=== 考试结束 ===\n";
cout << "考生: " << name << " 得分: " << score << " 分\n\n";
cout << "=== 错题解析 ===\n";
for (size_t i = 0; i < exam.size(); ++i) {
string userAns = userAnswers[i];
string correct = exam[i].answer;
transform(userAns.begin(), userAns.end(), userAns.begin(), ::toupper);
transform(correct.begin(), correct.end(), correct.begin(), ::toupper);
if (userAns != correct) {
cout << "第 " << (i + 1) << " 题: " << exam[i].content << endl;
cout << "你的答案: " << userAnswers[i] << " | 正确答案: " << exam[i].answer << endl;
cout << "解析: " << exam[i].analysis << endl << endl;
}
}
// 保存成绩
ofstream res("results.txt", ios::app);
res << name << " " << score << endl;
res.close();
cout << "成绩已保存到 results.txt 文件中。\n";
}
int main() {
loadQuestions();
cout << "欢迎使用简易在线考试系统!当前题库共有 " << questions.size() << " 道题。\n";
int choice;
while (true) {
cout << "\n=== 主菜单 ===\n";
cout << "1. 添加题目\n";
cout << "2. 开始考试\n";
cout << "3. 查看所有题目\n";
cout << "4. 删除题目\n";
cout << "5. 退出程序\n";
cout << "请输入选项 (1-5): ";
cin >> choice;
cin.ignore();
if (choice == 1) addQuestion();
else if (choice == 2) takeExam();
else if (choice == 3) showQuestions();
else if (choice == 4) deleteQuestion();
else if (choice == 5) break;
else cout << "输入错误,请重新输入!\n";
}
cout << "感谢使用,再见!\n";
return 0;
}

然后是其他功能的演示图片
下面这张是题目录入,可以自主放入题目灵活多变

录入题目完成后就可以查看题目,图片如下

接下来是题目的删除

题目编辑好后就可以进行惊心动魄的考试了
以下分别是答题正确或错误的情况


通过结对编程,我们有效减少了 bug(如文件读写格式错误、输入缓冲问题)。一人 coding 时另一人能及时指出边界情况(如空题库、多选答案大小写)。交换角色后对代码整体结构理解更深,提升了代码可读性和鲁棒性。最大的收获是学会了如何在有限时间内协作完成一个完整的小系统,同时加深了对C++文件操作、时间处理、随机算法的理解。

浙公网安备 33010602011771号