gfdhy 2024-02-03 11:00 采纳率: 100%
浏览 8
已结题

帮忙把这个c++代码做一点可视化


​
#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;
}

​

帮忙者必关注哦~

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-02-03 11:01
    关注

    引自免费微信小程序:皆我百晓生

    要将上述C++代码实现的简单文本交互式聊天机器人进行可视化,我们可以将其与图形用户界面(GUI)结合,以便用户在窗口中输入和查看机器人的回复。这里我将提供一个基于Qt库的C++ GUI版本示例代码,由于实际生成代码较长,我将概述关键步骤,并给出核心代码片段。

    关键步骤:

    1. 安装Qt库并创建一个新的Qt项目。
    2. 在项目中添加一个QTextEdit控件用于显示聊天记录,一个QLineEdit控件供用户输入消息,以及一个QPushButton按钮来触发发送消息事件。
    3. 将原来的命令行输出逻辑改为更新QTextEdit的内容。

    以下是一个简化的Qt GUI版本的核心代码片段:

    // 首先,请确保已包含必要的Qt头文件
    #include <QApplication>
    #include <QWidget>
    #include <QHBoxLayout>
    #include <QTextEdit>
    #include <QLineEdit>
    #include <QPushButton>
    
    class VirtualRobotWidget : public QWidget {
        Q_OBJECT
    
    public:
        VirtualRobotWidget(QWidget *parent = nullptr) : QWidget(parent) {
            // 初始化布局和控件
            layout = new QHBoxLayout(this);
            chatHistory = new QTextEdit(this);
            chatHistory->setReadOnly(true);
            userInput = new QLineEdit(this);
            sendButton = new QPushButton("发送", this);
    
            // 连接信号槽
            connect(sendButton, &QPushButton::clicked, this, &VirtualRobotWidget::onSendClicked);
    
            // 添加到布局
            layout->addWidget(chatHistory);
            layout->addWidget(userInput);
            layout->addWidget(sendButton);
    
            setLayout(layout);
    
            // 初始化聊天历史
            displayChatMessage("-----chatrobot v1.3.5-----");
    
            // 调用chat函数开始交互
            chat();
        }
    
    private slots:
        void onSendClicked() {
            QString inputText = userInput->text();
            userInput->clear();
    
            // 转发给虚拟机器人处理
            handleUserInput(inputText.toStdString());
        }
    
        void handleUserInput(const std::string& input) {
            // 调用原有的generateResponse方法获取回复
            std::string response = generateResponse(input);
    
            // 更新聊天历史
            displayChatMessage("你: " + input);
            displayChatMessage("虚拟机器人: " + response);
        }
    
        void displayChatMessage(const std::string& message) {
            chatHistory->appendPlainText(QString::fromStdString(message));
        }
    
    private:
        // 移植原始类中的相关方法,如generateResponse等
        std::string generateResponse(const std::string& input) {
            // ... 原始generateResponse方法的实现 ...
        }
    
        QVBoxLayout* layout;
        QTextEdit* chatHistory;
        QLineEdit* userInput;
        QPushButton* sendButton;
    };
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
        VirtualRobotWidget robotWidget;
        robotWidget.show();
        return app.exec();
    }
    

    注意:上述代码仅为概念性演示,你需要将原始VirtualRobot类的方法移植到VirtualRobotWidget类中,并根据Qt编程实践进行相应的调整以适应GUI环境。此外,在实际开发中还需要考虑线程安全、异常处理等问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月17日
  • 已采纳回答 3月9日
  • 请采纳用户回复 2月4日
  • 修改了问题 2月3日
  • 展开全部