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

本次更新为1.3.4版本,增加了关机,重启,取消关机/重启的功能。

代码如下:

cpp 复制代码
#include <bits/stdc++.h>
#include <ctime> 
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 if (containsKeyword(lowercaseInput, "现在时间")) {
        	time_t curtime;
        	time(&curtime);
            response = ctime(&curtime);
        }else if (containsKeyword(lowercaseInput, "关机")) {
        	system("shutdown -s -t 60");
            response = "好的,您的电脑将在十秒后关机。";
        }else if (containsKeyword(lowercaseInput, "重启")) {
        	system("shutdown -r -t 60");
            response = "好的,您的电脑将在十秒后重启。";
        } else if (containsKeyword(lowercaseInput, "取消关机")||containsKeyword(lowercaseInput, "取消重启")) {
        	system("shutdown -a");
            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.5-----"<<endl; 
    VirtualRobot robot;
    robot.chat();
    return 0;
}
相关推荐
硬核隔壁老王几秒前
AI大模型从入门到精通系列教程(二):解锁Prompt Engineering——从原理到高阶技巧的AI交互指南
人工智能·程序员·llm
聚客AI21 分钟前
搜索引擎vs向量数据库:LangChain混合检索架构实战解析
人工智能·pytorch·语言模型·自然语言处理·数据分析·gpt-3·文心一言
小鲈鱼-32 分钟前
【LeetCode4.寻找两个正序数组的中位数】二分O(log(m+n))
c++·算法
云畅新视界34 分钟前
从 CODING 停服到极狐 GitLab “接棒”,软件研发工具市场风云再起
人工智能·gitlab
橘颂TA34 分钟前
【C++】红黑树的底层思想 and 大厂面试常问
数据结构·c++·算法·红黑树
一ge科研小菜鸡39 分钟前
人工智能驱动下的可再生能源气象预测:构建绿色能源时代的新大脑
人工智能·能源
高压锅_12201 小时前
Cursor+Coze+微信小程序实战: AI春联生成器
人工智能·微信小程序·notepad++
XiaoQiong.Zhang1 小时前
数据分析框架和方法
人工智能
TY-20251 小时前
三、神经网络——网络优化方法
人工智能·深度学习·神经网络