版本比较
AI Code转换器 1.4 Open source.
标题
增加 1 行删除 1 行
11AI Code转换器 1.3(开源项目)AI Code转换器 1.4 Open source.正文
增加 107 行删除 64 行
1123545687891011121314151516171617202122232425181927202921222332242534352627282930317 行未改动3940414251434445554647484950515253545564656656575859606162639 行未改动737475768485777879878081828384858687888990919792939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461473 行未改动1511521531541091551111121561571581151591601611181191201211221621631641241251261271651661671681691701711721731321741751761771781791801811821401411421831841851861871881891901911921931511941951541551561571961971981992002012024 行未改动20720820921021121221321421513 行未改动2292302312321912332342352361952372382392402412422432022442452462472485 行未改动25425525625725825926026126226326426522322422526626726826922722822927027127223123227327427527627727827923623723823924028028128228328428528628712 行未改动## AI Code转换器1.3(已开源)### 项目介绍**【声明】:若您因为使用该程序被封或受到处罚,本人一概不负责。**
- 您有没有因为 `AI` 所写或为您调试的代码中存在着太多不合你意的变量却懒得改?或着注释太多?或者代码不像是人写的?那么我相信这段代码能帮到您。- 您有没有因为 `AI` 所写或为您调试的代码中存在着太多不合你意的变量却懒得改?或着注释太多?或者空行太多?那么我相信这段代码能帮到您。
### 程序功能::::info[功能一览表]::::info[功能一览表]{open}|功能具体内容|最后更新时间||:-:|:-:||替换变量名(除一些常用变量、单个字母的变量)|2026/01/27||增加 `tab` |2026/01/26||删除注释|2026/01/26||修改头文件|2026/03/11|
::::::::info[不会被更改的变量名]`dp`、`main`、`dfs`、`bfs`、`do`、`work`、`所有单个字符的便量`::::**请注意,目前程序仅支持 `C++11` 及以上版本的编译器。**
另外,我们目前的程序仅在试验阶段,有许多不足的地方,如果发现有bug,请联系[违规用户名1866836](https://www.luogu.com.cn/chat?uid=1866836),我们会尽快进行修改。### Bug的修复::::info[Bug一览表]|修改日期|发现日期| bug具体内容 | 错误程度|是否修改|报告人||:-:|:-:|:-:|:-:|:-:|:-:||2026/01/26|2026/01/26| main函数会被修改 |重大错误|是 |[违规用户名1866836](https://www.luogu.com.cn/user/1866836)|另外,我们目前的程序仅在试验阶段,有许多不足的地方,如果发现有bug,请联系@[违规用户名1866836](https://www.luogu.com.cn/chat?uid=1866836),我们会尽快进行修改。
::::### Code ::::info[请使用 C++11 及以上的编译器进行编译]{open}::::info[Version 1.4.]{open}```cpp/*Version 1.3.Version 1.4.Trial Phase.Start at 2026/01/26.Last change at 2026/01/27.Start at 2026-01-26.Last change at 2026-03-11.Open source.*/
#include <iostream>7 行未改动
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", "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(); initSpecialHeaders(); }
std::string simplifyCode(const std::string& inputCode) { std::string code = inputCode; code = removeAllComments(code); extractAndMapVariables(code); code = replaceVariables(code); code = removeAllComments(code); code = mergeHeaders(code); extractAndMapVariables(code); code = replaceVariables(code); code = formatWithIndentation(code); return code; }
9 行未改动 return input; }
private: std::vector<std::string> varNamePool; int currentVarIndex; std::vector<std::string> varNamePool; int currentVarIndex; std::unordered_map<std::string, std::string> varMapping; std::unordered_set<std::string> excludeSet; std::unordered_set<std::string> excludeSet; std::unordered_set<std::string> specialHeaders;
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 initSpecialHeaders() { specialHeaders = { "windows.h", "Winbase.h", "Winsock.h", "process.h", "dos.h", "conio.h" }; }
std::string mergeHeaders(const std::string& code) { std::stringstream ss(code); std::string line; std::vector<std::string> specialLines; std::vector<std::string> normalHeaders; std::string nonHeaderCode; bool hasBits = false;
while (std::getline(ss, line)) { std::string trimLine = line; trimLine.erase(0, trimLine.find_first_not_of(" \t"));
if (trimLine.compare(0, 8, "#include") == 0) { size_t left = trimLine.find('<'); size_t right = trimLine.find('>'); if (left != std::string::npos && right != std::string::npos) { std::string header = trimLine.substr(left + 1, right - left - 1); if (specialHeaders.count(header)) { specialLines.push_back(line); } else if (header == "bits/stdc++.h") { hasBits = true; normalHeaders.push_back(line); } else { if (!hasBits) { normalHeaders.push_back(line); } } } } else { nonHeaderCode += line + "\n"; } }
std::stringstream finalSS; for (const auto& h : normalHeaders) finalSS << h << "\n"; for (const auto& s : specialLines) finalSS << s << "\n"; finalSS << nonHeaderCode; return finalSS.str(); }
void extractAndMapVariables(const std::string& code) { varMapping.clear(); currentVarIndex = 0; std::unordered_set<std::string> varTypes = {3 行未改动
std::stringstream ss(code); std::string line; while (std::getline(ss, line)) { // 跳过预处理指令 if (line.find("#") == 0) continue; std::string token; std::string prevToken; std::string token, prevToken; std::stringstream lineSs(line); while (lineSs >> token) { if (varTypes.count(prevToken) > 0) { if (varTypes.count(prevToken)) { std::string pureVarName; for (char c : token) { if (isalnum(c) || c == '_') { pureVarName += c; } else { break; } if (isalnum(c) || c == '_') pureVarName += c; else break; } if (!pureVarName.empty() && pureVarName.size() > 1 // 只处理长度大于1的变量 && excludeSet.count(pureVarName) == 0 && varMapping.count(pureVarName) == 0) { if (!pureVarName.empty() && pureVarName.size() > 1 && excludeSet.count(pureVarName) == 0 && varMapping.count(pureVarName) == 0) { varMapping[pureVarName] = varNamePool[currentVarIndex % varNamePool.size()]; currentVarIndex++; } } prevToken = token; 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(); }); [](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()] != '_';
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 {4 行未改动 newCode += newLine + "\n"; } return newCode; }
std::string removeAllComments(const std::string& code) { std::string result; bool inMultiLineComment = false; bool inSingleLineComment = false;13 行未改动 } if (!inSingleLineComment) { if (c == '/' && next == '*' && !inMultiLineComment) { inMultiLineComment = true; i++; i++; continue; } else if (c == '*' && next == '/' && inMultiLineComment) { inMultiLineComment = false; i++; i++; continue; } } if (!inMultiLineComment) { if (c == '/' && next == '/') { inSingleLineComment = true; i++; i++; continue; } else if (c == '\n') { inSingleLineComment = false; result += c;5 行未改动 } } 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); })); std::string trimmedLine = line; trimmedLine.erase(trimmedLine.begin(), std::find_if(trimmedLine.begin(), trimmedLine.end(), [](unsigned char ch) { return !std::isspace(ch); }));
if (line.empty()) { formattedCode += "\n"; continue; if (trimmedLine.empty()) { continue; } size_t closeBracePos = line.find('}'); if (closeBracePos != std::string::npos) {
int closeCount = std::count(trimmedLine.begin(), trimmedLine.end(), '}'); for (int i = 0; i < closeCount; i++) { 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++; } formattedCode += trimmedLine + "\n";
int openCount = std::count(trimmedLine.begin(), trimmedLine.end(), '{'); indentLevel += openCount; } return formattedCode; }};12 行未改动