简易在线考试系统

结对搭档:2452734

  1. 算法设计思路
    对于整个考试系统,主要分为三个工作流程:出题,答题,结束。先通过 importQuestions 函数引导用户交互式输入题目数量、每道题的题干、四个选项、正确答案和分值,将题目存入题目集中,随后循环遍历题目输出题目,调用getUserAnswer函数检查用户输入的答案,输出“答案正确”和“答案错误”继而累加得分。
    再明确思路后,基于整体框架上使用ai对用户交互体验做出了改善,如整个流程通过菜单循环控制,利用 system("cls") 和 system("pause") 实现清屏与暂停,提升交互体验等等。

  2. 程序代码

点击查看代码
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <cstdlib>

using namespace std;


struct Question {
    string text;           
    vector<string> options; 
    int score;             
    char correctAnswer;    
};

class ExamSystem {
private:
    vector<Question> questions;
    int currentIndex;
    int totalScore;
    int userScore;

public:
    ExamSystem() : currentIndex(0), totalScore(0), userScore(0) {}

  
    void importQuestions() {
        int n;
        cout << "请输入要导入的题目数量:";
        cin >> n;
        cin.ignore(); 

        for (int i = 0; i < n; i++) {
            Question q;

            cout << "\n====== 第 " << (i + 1) << " 道题 ======" << endl;
            cout << "请输入题目内容:";
            getline(cin, q.text);
            string optionA, optionB, optionC, optionD;
            cout << "请输入选项A:";
            getline(cin, optionA);
            q.options.push_back("A:" + optionA);

            cout << "请输入选项B:";
            getline(cin, optionB);
            q.options.push_back("B:" + optionB);

            cout << "请输入选项C:";
            getline(cin, optionC);
            q.options.push_back("C:" + optionC);

            cout << "请输入选项D:";
            getline(cin, optionD);
            q.options.push_back("D:" + optionD);

            cout << "请输入正确答案(A/B/C/D):";
            cin >> q.correctAnswer;
            q.correctAnswer = toupper(q.correctAnswer);

            cout << "请输入本题分值:";
            cin >> q.score;
            cin.ignore();

            questions.push_back(q);
            totalScore += q.score;
        }

        cout << "\n成功导入 " << questions.size() << " 道题目!" << endl;
        cout << "总分:" << totalScore << " 分" << endl;
        system("pause");
    }

    void startExam() {
        if (questions.empty()) {
            cout << "错误:没有题目,请先导入题目!" << endl;
            system("pause");
            return;
        }

        currentIndex = 0;
        userScore = 0;

        system("cls");

        cout << "\n========== 考试开始 ==========" << endl;
        cout << "总题数:" << questions.size() << " 道" << endl;
        cout << "总分:" << totalScore << " 分" << endl;
        cout << "==============================" << endl;
        cout << "按回车键开始答题..." << endl;
        cin.ignore();
        cin.get();

        while (currentIndex < questions.size()) {
            system("cls");
            displayCurrentQuestion();

            char answer = getUserAnswer();

            if (answer == questions[currentIndex].correctAnswer) {
                userScore += questions[currentIndex].score;
                cout << "\n? 回答正确! +" << questions[currentIndex].score << "分" << endl;
            }
            else {
                cout << "\n? 回答错误!" << endl;
            }

            currentIndex++;

            if (currentIndex < questions.size()) {
                cout << "\n按回车键继续下一题..." << endl;
                cin.get();
                cin.get();
            }
        }

        cout << "\n\n==================== 考试完成 ====================" << endl;
        cout << "恭喜你完成了所有题目!" << endl;
        cout << "您的得分:" << userScore << " / " << totalScore << " 分" << endl;
        cout << "==================================================" << endl;

        system("pause");
    }

    void displayCurrentQuestion() {
        Question& q = questions[currentIndex];
        cout << "\n第 " << (currentIndex + 1) << " / " << questions.size() << " 题" << endl;
        cout << "----------------------------------------" << endl;
        cout << q.text << " (" << q.score << "分)" << endl;
        cout << endl;

        for (int i = 0; i < q.options.size(); i++) {
            cout << q.options[i] << endl;
        }
        cout << "----------------------------------------" << endl;
        cout << "请输入你的答案(A/B/C/D):";
    }

    char getUserAnswer() {
        char answer;
        while (true) {
            cin >> answer;
            answer = toupper(answer);
            if (answer == 'A' || answer == 'B' || answer == 'C' || answer == 'D') {
                return answer;
            }
            cout << "输入无效,请输入 A、B、C 或 D:";
        }
    }
};

int main() {
    ExamSystem exam;
    int choice;

    while (true) {
        system("cls");
        cout << "==================== 考试系统 ====================" << endl;
        cout << "|                1. 导入题目                      |" << endl;
        cout << "|                2. 开始考试                      |" << endl;
        cout << "|                0. 退出系统                      |" << endl;
        cout << "=================================================" << endl;
        cout << "请选择操作:";
        cin >> choice;

        switch (choice) {
        case 1:
            exam.importQuestions();
            break;
        case 2:
            exam.startExam();
            break;
        case 0:
            cout << "感谢使用考试系统!再见!" << endl;
            system("pause");
            return 0;
        default:
            cout << "无效选择!请重新输入!" << endl;
            system("pause");
        }
    }

    return 0;
}

  1. 运算结果

初始状态:
7
错误情况:
image

输入题目,答案,分值:
8

回归目录:
9
开始答题:
10

11

12
答题结束,给出答题结果:
13
输入错误选项:
image

答题结束:
14

  1. 作业体会
    这次结对编程,我们两个人一起合作写代码,效率很高。一个人主要想整体怎么设计,另一个人负责具体输入输出的细节,遇到问题随时讨论,很快就定好了各自要做什么。写代码的过程中,我们互相检查,发现了很多自己写的时候容易忽略的小毛病,比如用户输入不对的时候怎么提示、没有题目时怎么办等等。碰到卡住的地方,两个人一起琢磨,比一个人闷头想快多了。轮流写代码和看着对方写,也让我们学到了对方好的习惯。总的来说,两个人配合着干活,代码质量更好,写起来也更轻松,真正体会到了“1+1>2”的感觉。
posted @ 2026-04-21 16:54  卜渴鲤郁  阅读(12)  评论(0)    收藏  举报