关于我用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;
}
相关推荐
心勤则明41 分钟前
Spring AI 会话记忆实战:从内存存储到 MySQL + Redis 双层缓存架构
人工智能·spring·缓存
ARM+FPGA+AI工业主板定制专家3 小时前
基于GPS/PTP/gPTP的自动驾驶数据同步授时方案
人工智能·机器学习·自动驾驶
长鸳词羡3 小时前
wordpiece、unigram、sentencepiece基本原理
人工智能
ㄣ知冷煖★3 小时前
【GPT5系列】ChatGPT5 提示词工程指南
人工智能
科士威传动3 小时前
丝杆支撑座在印刷设备如何精准运行?
人工智能·科技·自动化·制造
Predestination王瀞潞4 小时前
IO操作(Num22)
开发语言·c++
taxunjishu4 小时前
DeviceNet 转 Modbus TCP 协议转换在 S7-1200 PLC化工反应釜中的应用
运维·人工智能·物联网·自动化·区块链
kalvin_y_liu5 小时前
智能体框架大PK!谷歌ADK VS 微软Semantic Kernel
人工智能·microsoft·谷歌·智能体
爱看科技5 小时前
智能眼镜行业腾飞在即,苹果/微美全息锚定“AR+AI眼镜融合”之路抢滩市场!
人工智能·ar
宋恩淇要努力5 小时前
C++继承
开发语言·c++