帮我写一个可以录入试题的题库控制台应用代码
时间: 2023-08-20 22:36:58 浏览: 133
好的,以下是可以录入试题的题库控制台应用代码:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Question {
public:
string question_text;
vector<string> options;
int correct_answer_index;
};
class QuestionBank {
public:
vector<Question> questions;
void add_question(Question q) {
questions.push_back(q);
}
void print_question(int index) {
Question q = questions[index];
cout << "Question " << index+1 << ": " << q.question_text << endl;
for (int i = 0; i < q.options.size(); i++) {
cout << i+1 << ") " << q.options[i] << endl;
}
}
int get_num_questions() {
return questions.size();
}
};
int main() {
QuestionBank q_bank;
while (true) {
cout << "Enter a question (or 'q' to quit): ";
string input;
getline(cin, input);
if (input == "q") {
break;
}
Question q;
q.question_text = input;
for (int i = 0; i < 4; i++) {
cout << "Enter option " << i+1 << ": ";
getline(cin, input);
q.options.push_back(input);
}
cout << "Enter the index of the correct answer (1-4): ";
getline(cin, input);
q.correct_answer_index = stoi(input) - 1;
q_bank.add_question(q);
}
cout << endl << "Question Bank:" << endl;
for (int i = 0; i < q_bank.get_num_questions(); i++) {
q_bank.print_question(i);
cout << endl;
}
return 0;
}
```
这个代码可以让用户逐个输入问题、选项和正确答案,然后将其保存在一个题库中。用户可以随时退出输入并查看整个题库。
阅读全文