跳到正文

科技·工程

AI Code转换器 1.3(开源项目)

Zim_o约 18 分钟
原文

这是 2026年1月27日 存档的第 3 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年1月26日
收录
2026年1月26日
更新
2026年11月22日
篇幅
约 18 分钟
获赞
1

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1

AI Code转换器1.3(已开源)

项目介绍

【声明】:若您因为使用该程序被封或受到处罚,本人一概不负责。

  • 您有没有因为 AI 所写或为您调试的代码中存在着太多不合你意的变量却懒得改?或着注释太多?或者代码不像是人写的?那么我相信这段代码能帮到您。

程序功能

功能一览表
功能具体内容最后更新时间
替换变量名(除一些常用变量、单个字母的变量)2026/01/27
增加 tab2026/01/26
删除注释2026/01/26
不会被更改的变量名

dp、main、dfs、bfs、do、work、所有单个字符的便量

请注意,目前程序仅支持 C++11 及以上版本的编译器。

另外,我们目前的程序仅在试验阶段,有许多不足的地方,如果发现有bug,请联系违规用户名1866836,我们会尽快进行修改。

Bug的修复

Bug一览表
修改日期发现日期bug具体内容错误程度是否修改报告人
2026/01/262026/01/26main函数会被修改重大错误是违规用户名1866836

Code

请使用 C++11 及以上的编译器进行编译
C++
/*
Version 1.3.
Trial Phase.
Start at 2026/01/26.
Last change at 2026/01/27.
Open source.
*/

#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <cctype>
#include <sstream>

class CodeSimplifier {
public:
    CodeSimplifier() : currentVarIndex(0) {
        // 变量名池
        varNamePool = {
            "ans", "res", "val", "x", "y", "z", "tmp", "num", "cnt", "ret",
            "buf", "data", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
            "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", 
            "idx", "len", "sum", "tot", "cur", "pre", "next", "low", "high",
            "left", "right", "top", "bot", "mid", "pos", "flag", "ok", "err"
        };
        initExcludeSet();
    }

    std::string simplifyCode(const std::string& inputCode) {
        std::string code = inputCode;
        code = removeAllComments(code); 
        extractAndMapVariables(code);   
        code = replaceVariables(code);  
        code = formatWithIndentation(code);
        return code;
    }

    std::string readMultiLineInput() {
        std::cout << "请输入需要处理的C++代码(输入//END结束输入):\n";
        std::string input, line;
        while (std::getline(std::cin, line)) {
            if (line.find("//END") != std::string::npos) {
                break;
            }
            input += line + "\n";
        }
        return input;
    }

private:
    std::vector<std::string> varNamePool;   
    int currentVarIndex;                    
    std::unordered_map<std::string, std::string> varMapping;
    std::unordered_set<std::string> excludeSet; 
    void initExcludeSet() {
        excludeSet = {
            "dp", "main", "dfs", "bfs", "do", "work",
            "if", "else", "for", "while", "switch", "case", "break", "return",
            "class", "struct", "enum", "const", "static", "inline", "auto", "sizeof",
            "new", "delete", "try", "catch", "throw", "namespace", "using",
            "std", "cout", "cin", "endl"
        };
    }
    // 提取变量
    void extractAndMapVariables(const std::string& code) {
        varMapping.clear();
        currentVarIndex = 0;
        std::unordered_set<std::string> varTypes = {
            "int", "float", "double", "bool", "char", "long", "short", "unsigned",
            "std::string", "string", "vector", "map", "set", "list", "array"
        };

        std::stringstream ss(code);
        std::string line;
        while (std::getline(ss, line)) {
            // 跳过预处理指令
            if (line.find("#") == 0) continue;
            std::string token;
            std::string prevToken; 
            std::stringstream lineSs(line);
            while (lineSs >> token) {
                if (varTypes.count(prevToken) > 0) {
                    std::string pureVarName;
                    for (char c : token) {
                        if (isalnum(c) || c == '_') {
                            pureVarName += c;
                        } else {
                            break; 
                        }
                    }
                    if (!pureVarName.empty() 
                        && pureVarName.size() > 1  // 只处理长度大于1的变量
                        && excludeSet.count(pureVarName) == 0 
                        && varMapping.count(pureVarName) == 0) {
                        varMapping[pureVarName] = varNamePool[currentVarIndex % varNamePool.size()];
                        currentVarIndex++;
                    }
                }
                prevToken = token; 
            }
        }
    }
    std::string replaceVariables(const std::string& code) {
        std::string result = code;
        std::vector<std::pair<std::string, std::string>> sortedVars(varMapping.begin(), varMapping.end());
        std::sort(sortedVars.begin(), sortedVars.end(),
                  [](const std::pair<std::string, std::string>& a, const std::pair<std::string, std::string>& b) {
                      return a.first.size() > b.first.size();
                  });

        std::stringstream ss(result);
        std::string line, newCode;
        while (std::getline(ss, line)) {
            std::string newLine = line;
            for (const auto& pair : sortedVars) {
                const std::string& oldName = pair.first;
                const std::string& newName = pair.second;

                size_t pos = 0;
                while ((pos = newLine.find(oldName, pos)) != std::string::npos) {
                    bool isWordStart = (pos == 0) || !isalnum(newLine[pos-1]) && newLine[pos-1] != '_';
                    bool isWordEnd = (pos + oldName.size() == newLine.size()) || 
                                     !isalnum(newLine[pos + oldName.size()]) && newLine[pos + oldName.size()] != '_';

                    if (isWordStart && isWordEnd) {
                        newLine.replace(pos, oldName.size(), newName);
                        pos += newName.size();
                    } else {
                        pos += oldName.size();
                    }
                }
            }
            newCode += newLine + "\n";
        }
        return newCode;
    }
    std::string removeAllComments(const std::string& code) {
        std::string result;
        bool inMultiLineComment = false;
        bool inSingleLineComment = false;
        bool inString = false;

        for (size_t i = 0; i < code.size(); ++i) {
            char c = code[i];
            char next = (i+1 < code.size()) ? code[i+1] : '\0';
            if (c == '"' && !inMultiLineComment && !inSingleLineComment) {
                inString = !inString;
                result += c;
                continue;
            }
            if (inString) {
                result += c;
                continue;
            }
            if (!inSingleLineComment) {
                if (c == '/' && next == '*' && !inMultiLineComment) {
                    inMultiLineComment = true;
                    i++; 
                    continue;
                } else if (c == '*' && next == '/' && inMultiLineComment) {
                    inMultiLineComment = false;
                    i++; 
                    continue;
                }
            }
            if (!inMultiLineComment) {
                if (c == '/' && next == '/') {
                    inSingleLineComment = true;
                    i++; 
                    continue;
                } else if (c == '\n') {
                    inSingleLineComment = false;
                    result += c;
                    continue;
                }
            }
            if (!inMultiLineComment && !inSingleLineComment) {
                result += c;
            }
        }
        return result;
    }
    std::string formatWithIndentation(const std::string& code) {
        std::stringstream ss(code);
        std::string line, formattedCode;
        int indentLevel = 0;
        const std::string tab = "\t";

        while (std::getline(ss, line)) {
            line.erase(line.begin(), std::find_if(line.begin(), line.end(), [](unsigned char ch) {
                return !std::isspace(ch);
            }));

            if (line.empty()) {
                formattedCode += "\n";
                continue;
            }
            size_t closeBracePos = line.find('}');
            if (closeBracePos != std::string::npos) {
                indentLevel = std::max(0, indentLevel - 1);
            }
            formattedCode += std::string(indentLevel, '\t');
            formattedCode += line + "\n";
            size_t openBracePos = line.find('{');
            if (openBracePos != std::string::npos && openBracePos == line.size() - 1) {
                indentLevel++;
            }
        }
        return formattedCode;
    }
};

int main() {
    CodeSimplifier simplifier;
    std::string inputCode = simplifier.readMultiLineInput();
    std::string simplifiedCode = simplifier.simplifyCode(inputCode);
    system("cls");
    std::cout << "\n===处理后的代码===\n";
    std::cout << simplifiedCode << std::endl;
    return 0;
}