做一个聊天机器人的C++代码
下面是一个简单的聊天机器人的C++代码示例:
#include <iostream>
#include <string>
std::string getResponse(const std::string& userInput) {
if (userInput == "你好") {
return "你好!我是聊天机器人。";
} else if (userInput == "你叫什么名字") {
return "我叫小机器人。";
} else if (userInput == "再见") {
return "再见!祝你有美好的一天!";
} else {
return "抱歉,我不明白你的意思。";
}
}
int main() {
std::cout << "你好!我是聊天机器人。" << std::endl;
std::cout << "你可以问我一些问题,或者和我聊天。" << std::endl;
std::cout << "输入\"再见\"可以结束对话。" << std::endl;
std::string userInput;
std::string response;
while (true) {
std::cout << "> ";
std::getline(std::cin, userInput);
response = getResponse(userInput);
std::cout << "小机器人: " << response << std::endl;
if (userInput == "再见") {
break;
}
}
return 0;
}
这个简单的聊天机器人代码使用了一个 getResponse 函数来根据用户输入返回相应的回答。在 main 函数中,用户可以输入问题或者对话,并通过调用 getResponse 函数获取机器人的回答,并输出到终端上。如果用户输入了"再见",对话将结束
原文地址: https://www.cveoy.top/t/topic/inO7 著作权归作者所有。请勿转载和采集!