如何用36行写一个可以自娱自乐的聊天机器人?

· · 个人记录

#include <bits/stdc++.h>
using namespace std;

signed main() {
    cout << "您好,我可以陪您聊天!请问您想要干什么?\n";
    while (true) {
        char ch;
        string s;
        getline(cin, s);
        for (int i = 0; i < s.size(); i++) {
            if ((i == s.size() - 1 || (s[i] == '?' || s[i] == '!' || s[i] == ' ' || (s[i] >= 32 && s[i] <= 126)))) {
                if (s[i] == '?')
                    cout << "!";
                else
                    cout << s[i];
            } else {
                string word;
                word  = s[i];
                word += s[i + 1];
                if (word == "吗");
                else if (word == "?")
                    cout << "!";
                else if (word == "你")
                    cout << "我";
                else if (word == "我")
                    cout << "你";
                else
                    cout << word;
                i++;
            }
        }
        cout << '\n';
    }
    return 0;
}