chatrobot v1.3.6
更新内容:关闭输入输出流的同步,因为我不想用scanf和printf了。
代码:
#include <bits/stdc++.h>
#include <ctime>
using namespace std;
string uI; //userInput
class ViR {
public:
void chat() {
cout << "你好!我是你的虚拟机器人。你想和我聊些什么?" << endl;
while (true) {
cout << "你: ";
getline(cin, uI);
if (uI == "退出") {
cout << "虚拟机器人: 再见!" << endl;
break;
} else {
cout << "虚拟机器人: " << gR(uI) << endl;
}
}
}
private:
string gR(const string& input) {
string r;
string lcI = input; //lowercaseInput
transform(lcI.begin(), lcI.end(), lcI.begin(), ::tolower);
if (cKw(lcI, "你是谁")) {
r = "我是你的虚拟机器人" ;
} else if (cKw(lcI, "退出")) {
r = "好的,再见!";
} else if (cKw(lcI, "打开")) {
r = "对不起,我不能打开任何东西。";
} else if (cKw(lcI, "谢谢")) {
r = "不客气!";
} else if (cKw(lcI, "你好")) {
r = "你好!有什么我可以帮到你的吗?";
} else if (cKw(lcI, "你能干什么")||cKw(lcI, "你的功能")) {
r = "我可以和你聊天,并回答你关于编程、技术和计算机相关的问题。";
} else if (cKw(lcI, "什么语言")) {
r = "我是用C++开发的。";
} else if (cKw(lcI, "感谢")) {
r = "不客气,有什么我可以帮到你的吗?";
}else if (cKw(lcI, "几点")) {
time_t curtime;
time(&curtime);
r = ctime(&curtime);
}else if (cKw(lcI, "关机")) {
system("shutdown -s -t 30");
r = "好的,您的电脑将在30秒后关机。";
}else if (cKw(lcI, "重启")) {
system("shutdown -r -t 30");
r = "好的,您的电脑将在30秒后重启。";
} else if (cKw(lcI, "取消关机")||cKw(lcI, "取消重启")) {
system("shutdown -a");
r = "好的,已取消。";
} else {
r = "抱歉,我还不知道该如何回答这个问题。";
}
return r;
}
bool cKw(const string& input, const string& keyword) { //containsKeyword
return input.find(keyword) != string::npos;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout<<"-----chatrobot v1.3.6-----"<<endl;
ViR robot;
robot.chat();
return 0;
}