#include <iostream>
#include <string>
#include <map>
#include <fstream>
class ChatBot {
public:
void chat() {
loadKnowledgeBase();
std::cout << "你好!我是你的聊天机器人。你想和我聊些什么?" << std::endl;
while (true) {
std::cout << "你: ";
std::getline(std::cin, userInput);
if (userInput == "退出") {
saveKnowledgeBase();
std::cout << "聊天机器人: 再见!" << std::endl;
break;
}
std::cout << "聊天机器人: " << generateResponse(userInput) << std::endl;
}
}
private:
std::string userInput;
std::map<std::string, std::string> knowledgeBase;
void loadKnowledgeBase() {
std::ifstream file("knowledge_base.txt");
std::string input;
std::string response;
while (std::getline(file, input) && std::getline(file, response)) {
knowledgeBase[input] = response;
}
file.close();
}
void saveKnowledgeBase() {
std::ofstream file("knowledge_base.txt");
for (const auto& entry:knowledgeBase) {
file << entry.first << std::endl;
file << entry.second << std::endl;
}
file.close();
}
std::string generateResponse(const std::string& input) {
if (knowledgeBase.count(input) > 0) {
return knowledgeBase[input];
} else {
std::string response;
std::cout << "聊天机器人: 对不起,我不知道如何回答这个问题。请告诉我一个合适的回答:" << std::endl;
std::cout << "你: ";
std::getline(std::cin, response);
knowledgeBase[input] = response;
return response;
}
}
};
int main() {
ChatBot bot;
bot.chat();
return 0;
}
帮忙看一下,代码的问题出在哪儿了
【相关推荐】
#include <stdio.h>
#include <math.h>int GetFigures(int n)
{
if(n == 0)//do...while或者count = 1,再进行while循环
return 1;
int count = 0;
while(n != 0)
{
n = n / 10;
count++;
}
return count;
}
void PrintReverse(int n)//逆序输出n的每一位数
{
if(n < 0)
printf("-"),n=-n;
if(n == 0)//同上do...while
printf("0");
while(n != 0)
{
printf("%d ",n % 10);
n/=10;//丢弃个位数字
}
printf("\n");
}
//正序输出n的每一位数
void PrintOrder(int n)
{
int count = GetFigures(n)-1;
int power = (int)pow(10.0,count);
do
{
printf("%d ",n/power);
n %= power;
power /= 10;
}while(n != 0);
/*for(int i = GetFigures(n)-1;i>=0;i--)//函数调用太多,效率不高
{
printf("%d ",n/(int)pow(10.0,i));
//pow函数一般返回的是double型,故在使用时,注意强制转化类型
n %= (int)pow(10.0,i);
}*/
printf("\n");
}int main()
{
PrintOrder(12345789);
PrintOrder(0);
PrintOrder(-12345789);
PrintReverse(12345);
PrintReverse(1);
printf("%d\n",GetFigures(12345));
printf("%d\n",GetFigures(123456789));
printf("%d\n",GetFigures(0));
printf("%d\n",GetFigures(-123456789));
return 0;
}
具体什么问题
我怀疑你的代码是gpt生成的。
现在可好,人工回答专门给gpt擦屁股。
我建议你继续向gpt追问,让他继续帮你改。