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

此次更新为v1.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 {
            response = "抱歉,我还不知道该如何回答这个问题。";
        }
        return response;
    }
    bool containsKeyword(const string& input, const string& keyword) {
        // 检查输入字符串是否包含关键词
        return input.find(keyword) != string::npos;
    }
};

int main() {
	cout<<"-----chatrobot v1.3.4-----"<<endl; 
    VirtualRobot robot;
    robot.chat();
    return 0;
}

c++获取系统时间参考:https://blog.csdn.net/qq_33210042/article/details/119822899

相关推荐
wayz114 分钟前
Day 6 编程实战:决策树与过拟合分析
算法·决策树·机器学习
ฅ ฅBonnie12 分钟前
vLLM 推理后端简介
人工智能·python·算法
历程里程碑13 分钟前
Linux 50 IP协议深度解析:从报头结构到子网划分与NAT
java·linux·开发语言·网络·c++·python·智能路由器
贾斯汀玛尔斯13 分钟前
每天学一个算法--堆排序(Heap Sort)
数据结构·算法
programhelp_15 分钟前
ZipRecruiter CodeSignal OA 2026|最新真题分享 + 速通攻略
数据结构·经验分享·算法·面试
犹怜草木青30 分钟前
const关键字
c++
liuyao_xianhui35 分钟前
map和set_C++
java·开发语言·数据结构·c++·算法·宽度优先
香蕉鼠片39 分钟前
八股C++
开发语言·c++
feng_you_ying_li40 分钟前
C++之智能指针的介绍与实现
c++
QYR_Jodie41 分钟前
从智能制造升级与机器人普及驱动到高增扩容:全球机器人关节电磁制动器2025年2.12亿,2032年达4.30亿,2026-2032年CAGR11.1%
机器人·制造·市场报告