关于我用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;
}
相关推荐
SEO_juper19 分钟前
驱动增长,而非浪费:8步整合SEO与PMax,解锁Google AI的隐藏流量
人工智能·搜索引擎·百度·seo·数字营销
IvanCodes25 分钟前
一、初识 LangChain:架构、应用与开发环境部署
人工智能·语言模型·langchain·llm
ouliten28 分钟前
C++笔记:std::variant
开发语言·c++·笔记
武子康37 分钟前
AI研究-117 特斯拉 FSD 视觉解析:多摄像头 - 3D占用网络 - 车机渲染,盲区与低速复杂路况安全指南
人工智能·科技·计算机视觉·3d·视觉检测·特斯拉·model y
Geoking.44 分钟前
PyTorch torch.unique() 基础与实战
人工智能·pytorch·python
Fr2ed0m1 小时前
卡尔曼滤波算法原理详解:核心公式、C 语言代码实现及电机控制 / 目标追踪应用
c语言·人工智能·算法
熊猫_豆豆1 小时前
神经网络的科普,功能用途,包含的数学知识
人工智能·深度学习·神经网络
Yupureki1 小时前
从零开始的C++学习生活 20:数据结构与STL复习课(4.4w字全解析)
c语言·数据结构·c++·学习·visual studio·1024程序员节
REDcker1 小时前
C++项目 OpenSSL 依赖最佳实践
开发语言·c++
笨蛋不要掉眼泪1 小时前
deepseek封装结合websocket实现与ai对话
人工智能·websocket·网络协议