关于我用AI编写了一个聊天机器人……(6)

此次更新为1.3.3版本,优化代码(其实就是加了using namespace std;再去掉代码里的std::)。

代码如下:

​
#include 
using namespace std;
string userInput;
class VirtualRobot {
public:
    void chat() {
        cout << "你好!我是你的虚拟机器人。你想和我聊些什么?" << endl;
        while (true) {
            cout << "你: ";
            getline(cin, userInput);
            if (userInput == "退出") {
                cout << "虚拟机器人: 再见!" << endl;
                break;
            } else {
                cout << "虚拟机器人: " << generateResponse(userInput) << endl;
            }
        }
    }
private:
    string generateResponse(const string& input) {
        string response;
        // 转换输入字符串为小写,便于匹配
        string lowercaseInput = input;
        transform(lowercaseInput.begin(), lowercaseInput.end(), lowercaseInput.begin(), ::tolower);
        if (containsKeyword(lowercaseInput, "你是谁")) {  
            response = "我是你的虚拟机器人" ;  
        } else if (containsKeyword(lowercaseInput, "退出")) {  
            response = "好的,再见!";
        } else if (containsKeyword(lowercaseInput, "打开")) {  
            response = "对不起,我不能打开任何东西。";  
        } else if (containsKeyword(lowercaseInput, "谢谢")) {
            response = "不客气!";
        } else if (containsKeyword(lowercaseInput, "你好")) {
            response = "你好!有什么我可以帮到你的吗?";
        } else if (containsKeyword(lowercaseInput, "你能干什么")||containsKeyword(lowercaseInput, "你的功能")) {
            response = "我可以和你聊天,并回答你关于编程、技术和计算机相关的问题。";
        } else if (containsKeyword(lowercaseInput, "什么语言")) {
            response = "我是用C++开发的。";
        } else if (containsKeyword(lowercaseInput, "感谢")) {
            response = "不客气,有什么我可以帮到你的吗?";
        } else {
            response = "抱歉,我还不知道该如何回答这个问题。";
        }
        return response;
    }
    bool containsKeyword(const string& input, const string& keyword) {
        // 检查输入字符串是否包含关键词
        return input.find(keyword) != string::npos;
    }
};

int main() {
	cout<<"-----chatrobot v1.3.3-----"<

你可能感兴趣的:(人工智能,c++,机器人)