在linux环境下的c++代码再写一遍
时间: 2024-03-04 11:52:53 浏览: 19
好的,以下是在Linux环境下的C++代码实现:
```c++
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL)); // 初始化随机数种子
const char* questions[] = { // 问题列表
"你叫什么名字?",
"你今年几岁了?",
"你喜欢吃什么?",
"你最喜欢的颜色是什么?",
"你最喜欢的电影是什么?"
};
const char* answers[] = { // 答案列表
"我是一个AI机器人,没有名字。",
"我是一个程序,没有年龄。",
"我不需要吃饭,也没有口味。",
"我是一个程序,没有喜恶。",
"我是一个程序,没有看过电影。"
};
int numQuestions = sizeof(questions) / sizeof(questions[0]); // 问题数量
while (true) {
// 随机选一个问题
int index = rand() % numQuestions;
const char* question = questions[index];
// 输出问题并等待用户输入
cout << question << endl;
string input;
getline(cin, input);
// 如果输入是 "再见",则退出循环
if (input == "再见") {
break;
}
// 随机选一个答案并输出
int numAnswers = sizeof(answers) / sizeof(answers[0]);
int answerIndex = rand() % numAnswers;
const char* answer = answers[answerIndex];
cout << answer << endl;
}
return 0;
}
```
这份代码在Linux环境下的编译命令为:
```
g++ main.cpp -o chatbot
```
执行后,可以通过输入问题与程序进行交互。当输入 "再见" 时,程序会退出。