跳到正文

科技·工程

离谱AI

_czy_约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年4月22日
收录
2026年4月22日
更新
2026年11月16日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1
C++
//要求:工具[T] →编译选项[C] →编译时加入以下指令:-std=c++14

#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <sstream>
#include <cstdlib>

using namespace std;

map<string, string> brain;

// 转小写
string toLower(string s) {
    transform(s.begin(), s.end(), s.begin(), ::tolower);
    return s;
}

// 关键词分割
vector<string> getWords(const string& text) {
    vector<string> words;
    string temp;
    for (char c : text) {
        if (isspace(c)) {
            if (!temp.empty()) {
                words.push_back(temp);
                temp.clear();
            }
        } else {
            temp = temp+toLower(c+"");
        }
    }
    if (!temp.empty()) words.push_back(temp);
    return words;
}

// 模糊匹配
string findAnswer(const string& input) {
    string low = toLower(input);
    vector<string> words = getWords(low);
    for (auto& p : brain) {
        for (string& w : words) {
            if (p.first.find(w) != string::npos) {
                return p.second;
            }
        }
    }
    return "";
}

// 加载/保存
void loadBrain() {
    ifstream fin("ai_brain.txt");
    if (!fin) return;
    string q, a;
    while (getline(fin, q) && getline(fin, a)) brain[q] = a;
}

void saveBrain() {
    ofstream fout("ai_brain.txt");
    for (auto& p : brain) fout << p.first << endl << p.second << endl;
}

// 数学计算
bool isMath(const string& s) {
    for (char c : s) {
        if (!isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != ' ')
            return false;
    }
    return true;
}

double calc(const string& exp) {
    istringstream iss(exp);
    double val = 0, num;
    char op;
    iss >> val;
    while (iss >> op >> num) {
        if (op == '+') val += num;
        else if (op == '-') val -= num;
        else if (op == '*') val *= num;
        else if (op == '/') val /= num;
    }
    return val;
}

// 时间
string getTime() {
    time_t now = time(0);
    char buf[100];
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&now));
    return buf;
}

// 笑话
string getJoke() {
    vector<string> jokes = {
        "我问风扇我帅吗,它摇了一晚上头",
        "有人说我穷,我笑了,我是真的穷",
        "从前有个小蔡,后来被端走了?"
    };
    return jokes[rand() % jokes.size()];
}

// 初始化AI
void initAI() {
    brain["你好"] = "你好呀!我是你的AI小助手";
    brain["名字"] = "我叫小C,超聪明的";
    brain["年龄"] = "我永远18岁?";
    brain["喜欢"] = "最喜欢和你聊天啦";
}

// 颜色输出
void msgYou(string s) {
    cout << "你:" << s << "\n";
}

void msgAI(string s) {
    cout << "AI:" << s << "\n";
}

// ========================
// 只有一个 main() 函数!
// ========================
int main() {
    system("color 0B");
    srand(time(0));
    loadBrain();
    if (brain.empty()) initAI();

    cout << "=========================================\n";
    cout << "         彩色智能 AI \n";
    cout << "   支持:聊天 | 学习 | 计算 | 时间 | 笑话\n";
    cout << "=========================================\n\n";

    string input;
    while (true) {
        cout << "-----------------------------------------\n";
        cout << "请输入:";
        getline(cin, input);
        string low = toLower(input);

        if (low == "退出" || low == "bye") {
            msgAI("保存成功!下次见啦~ ");
            saveBrain();
            break;
        }

        msgYou(input);

        // 数学
        if (isMath(input)) {
            double res = calc(input);
            msgAI("计算结果:" + to_string(res));
            continue;
        }

        // 时间
        if (low.find("时间") != string::npos || low.find("几点") != string::npos) {
            msgAI(" 当前时间:" + getTime());
            continue;
        }

        // 笑话
        if (low.find("笑话") != string::npos) {
            msgAI(getJoke());
            continue;
        }

        // 回答
        string ans = findAnswer(input);
        if (!ans.empty()) {
            msgAI(ans);
        } else {
            msgAI(" 这个我不会,你教我吧~");
            cout << "请输入答案:";
            string a;
            getline(cin, a);
            brain[low] = a;
            msgAI("学会啦!我记住了!");
        }
    }
    return 0;
}
原文