用c++写一个中英翻译选择题的代码
时间: 2024-02-12 17:08:38 浏览: 22
下面是一个简单的示例代码,用于演示如何使用C++实现中英翻译选择题功能:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
// 定义选择题结构体
struct Question
{
string question; // 英文问题
string answer; // 中文答案
vector<string> options; // 选项
int correctIndex; // 正确选项索引
};
// 定义选择题集合类型
typedef vector<Question> QuestionSet;
// 初始化选择题集合
void initQuestions(QuestionSet& questions)
{
// 添加选择题
Question question1;
question1.question = "What does 'hello' mean in Chinese?";
question1.answer = "你好";
question1.options.push_back("再见");
question1.options.push_back("你好");
question1.options.push_back("谢谢");
question1.correctIndex = 1;
questions.push_back(question1);
Question question2;
question2.question = "What does 'apple' mean in Chinese?";
question2.answer = "苹果";
question2.options.push_back("菠萝");
question2.options.push_back("香蕉");
question2.options.push_back("苹果");
question2.correctIndex = 2;
questions.push_back(question2);
Question question3;
question3.question = "What does 'computer' mean in Chinese?";
question3.answer = "电脑";
question3.options.push_back("电视");
question3.options.push_back("手机");
question3.options.push_back("电脑");
question3.correctIndex = 2;
questions.push_back(question3);
}
// 随机生成选择题
Question generateQuestion(const QuestionSet& questions)
{
int index = rand() % questions.size();
return questions[index];
}
// 打印选择题
void printQuestion(const Question& question)
{
cout << question.question << endl;
for (size_t i = 0; i < question.options.size(); ++i)
{
cout << i + 1 << ". " << question.options[i] << endl;
}
cout << endl;
}
// 用户选择答案
int getAnswer()
{
int answer = 0;
cout << "Please enter your answer (1-3): ";
cin >> answer;
return answer;
}
// 判断答案是否正确
bool isCorrectAnswer(const Question& question, int answer)
{
return answer == question.correctIndex + 1;
}
int main()
{
// 初始化随机数种子
srand(time(nullptr));
// 初始化选择题集合
QuestionSet questions;
initQuestions(questions);
// 提示用户进行选择题
cout << "Welcome to the English-Chinese translation quiz!" << endl;
cout << "You will be asked to translate some English words into Chinese." << endl;
cout << "Good luck!" << endl;
// 开始选择题
int score = 0;
int total = 0;
while (true)
{
// 随机生成选择题
Question question = generateQuestion(questions);
// 打印选择题
printQuestion(question);
// 用户选择答案
int answer = getAnswer();
// 判断答案是否正确
if (isCorrectAnswer(question, answer))
{
cout << "Correct!" << endl;
++score;
}
else
{
cout << "Wrong! The correct answer is: " << question.options[question.correctIndex] << endl;
}
// 统计得分和题目数量
++total;
// 询问用户是否继续
cout << "Do you want to continue? (y/n): ";
char choice;
cin >> choice;
if (choice == 'n' || choice == 'N')
{
break;
}
}
// 输出最终得分
cout << "Your final score is: " << score << " / " << total << endl;
return 0;
}
```
以上代码使用了C++ STL中的vector容器来存储选择题集合,使用了rand和srand函数来生成随机选择题。每次循环中,使用generateQuestion函数随机生成选择题,使用printQuestion函数打印选择题,使用getAnswer函数获取用户输入的答案,使用isCorrectAnswer函数判断答案是否正确。循环结束后,输出用户的最终得分。
当然,这只是一个简单的示例代码,实际生产环境中需要考虑更多的因素,比如错误处理、输入校验、界面设计等。