class Question: def __init__(self, stem, options, answer): self.stem = stem self.options = options self.answer = answerclass QuestionBank: def __init__(self): self.questions = [] def add_question(self, question): self.questions.append(question) def remove_question(self, question): self.questions.remove(question) def get_random_questions(self, num): return random.sample(self.questions, num)class Paper: def __init__(self, questions): self.questions = questions self.answers = {} def answer_question(self, question, answer): self.answers[question] = answer def get_score(self): score = 0 for question, answer in self.answers.items(): if answer == question.answer: score += 1 return scoreclass Grader: def __init__(self, paper): self.paper = paper def grade(self): return self.paper.get_score()# Example usagequestion1 = Question("What is the capital of France?", ["Paris", "London", "Berlin", "Madrid"], "Paris")question2 = Question("What is the largest planet in the solar system?", ["Mercury", "Venus", "Earth", "Jupiter"], "Jupiter")question3 = Question("What is the highest mountain in the world?", ["K2", "Mount Everest", "Makalu", "Cho Oyu"], "Mount Everest")question_bank = QuestionBank()question_bank.add_question(question1)question_bank.add_question(question2)question_bank.add_question(question3)paper = Paper(question_bank.get_random_questions(2))paper.answer_question(question1, "Paris")paper.answer_question(question2, "Jupiter")grader = Grader(paper)score = grader.grade()print("Your score is:", score)将这个代码转为C++的
时间: 2024-02-19 21:57:23 浏览: 94
好的,以下是将 Python 代码转换为 C++ 代码的示例:
```cpp
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <random>
using namespace std;
class Question {
public:
string stem;
vector<string> options;
string answer;
Question(string s, vector<string> o, string a) {
stem = s;
options = o;
answer = a;
}
};
class QuestionBank {
public:
vector<Question> questions;
void add_question(Question q) {
questions.push_back(q);
}
void remove_question(Question q) {
questions.erase(remove(questions.begin(), questions.end(), q), questions.end());
}
vector<Question> get_random_questions(int num) {
vector<Question> res;
random_device rd;
mt19937 gen(rd());
shuffle(questions.begin(), questions.end(), gen);
for (int i = 0; i < num; ++i) {
res.push_back(questions[i]);
}
return res;
}
};
class Paper {
public:
vector<Question> questions;
map<Question, string> answers;
Paper(vector<Question> q) {
questions = q;
}
void answer_question(Question q, string a) {
answers[q] = a;
}
int get_score() {
int score = 0;
for (auto const& x : answers) {
if (x.second == x.first.answer) {
score++;
}
}
return score;
}
};
class Grader {
public:
Paper paper;
Grader(Paper p) {
paper = p;
}
int grade() {
return paper.get_score();
}
};
int main() {
Question question1("What is the capital of France?", {"Paris", "London", "Berlin", "Madrid"}, "Paris");
Question question2("What is the largest planet in the solar system?", {"Mercury", "Venus", "Earth", "Jupiter"}, "Jupiter");
Question question3("What is the highest mountain in the world?", {"K2", "Mount Everest", "Makalu", "Cho Oyu"}, "Mount Everest");
QuestionBank question_bank;
question_bank.add_question(question1);
question_bank.add_question(question2);
question_bank.add_question(question3);
vector<Question> random_questions = question_bank.get_random_questions(2);
Paper paper(random_questions);
paper.answer_question(question1, "Paris");
paper.answer_question(question2, "Jupiter");
Grader grader(paper);
int score = grader.grade();
cout << "Your score is: " << score << endl;
return 0;
}
```
注意,这只是一个简单的示例,实际情况下可能需要更多的代码来处理错误处理、用户输入等问题。
阅读全文