关于我用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;
}
相关推荐
Coding茶水间7 分钟前
基于深度学习的路面坑洞检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
梵得儿SHI13 分钟前
AI Agent 深度解析:高级架构、优化策略与行业实战指南(多智能体 + 分层决策 + 人类在环)
人工智能·多智能体系统·aiagent·分层决策系统·人类在环机制·agent系统完整解决方案·aiagent底层原理
Peter_Monster31 分钟前
大语言模型(LLM)架构核心解析(干货篇)
人工智能·语言模型·架构
Ma0407131 小时前
【机器学习】监督学习、无监督学习、半监督学习、自监督学习、弱监督学习、强化学习
人工智能·学习·机器学习
cooldream20091 小时前
LlamaIndex 存储体系深度解析
人工智能·rag·llamaindex
闻缺陷则喜何志丹1 小时前
【SOSDP模板 容斥原理 逆向思考】3757. 有效子序列的数量|分数未知
c++·算法·力扣·容斥原理·sosdp·逆向思考
Elastic 中国社区官方博客1 小时前
使用 A2A 协议和 MCP 在 Elasticsearch 中创建一个 LLM agent 新闻室:第二部分
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
知识浅谈1 小时前
我用Gemini3pro 造了个手控全息太阳系
人工智能
孤廖1 小时前
终极薅羊毛指南:CLI工具免费调用MiniMax-M2/GLM-4.6/Kimi-K2-Thinking全流程
人工智能·经验分享·chatgpt·ai作画·云计算·无人机·文心一言
aneasystone本尊1 小时前
学习 LiteLLM 的日志系统
人工智能