版本比较
AI Code转换器 1.4 Open source.
标题
增加 1 行删除 1 行
11AI Code转换器 1.0AI Code转换器 1.4 Open source.正文
增加 181 行删除 125 行
1231234567891011121314151617181920212223724259102627282930313 行未改动3536373839404142252627282930313233343536434445464748495051525354555657585960616263646566675 行未改动73747576585960616277787980818283846566678586878870717289909175767778929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413580818213613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517685177878889909192939417817918018118218318418518618718818919019119219319419519619719819920020120220320420596972062072081001011021032092101052112122132142152162172182192201152212222232242251212262272282291261272302312321312332342352361352372382392401391402412422431442442452462472483 行未改动252253254255156256257159160161258259260164165166261262263264265266267268269169170270271272273173174175176177178179180181182183184185186187188189190191192193194274275276277196197198199200201202203204205206207208209210211212278214215216217218279280281220221222223224225226227228282283284230231285286287288289290291292293294295296297298299你有没有因为ai所写的代码中存在着太多不合你意的变量却懒得改?或着注释太多?或者代码不像是人写的?那么我相信这段代码能帮到你。
### AI Code转换器(已开源)### 项目介绍**【声明】:若您因为使用该程序被封或受到处罚,本人一概不负责。**
- 您有没有因为 `AI` 所写或为您调试的代码中存在着太多不合你意的变量却懒得改?或着注释太多?或者空行太多?那么我相信这段代码能帮到您。
### 程序功能::::info[功能一览表]{open}|功能具体内容|最后更新时间||:-:|:-:||替换变量名(除一些常用变量、单个字母的变量)|2026/01/27||增加 `tab` |2026/01/26||删除注释|2026/01/26||修改头文件|2026/03/11|
::::**请注意,目前程序仅支持 `C++11` 及以上版本的编译器。**
另外,我们目前的程序仅在试验阶段,有许多不足的地方,如果发现有bug,请联系@[违规用户名1866836](https://www.luogu.com.cn/chat?uid=1866836),我们会尽快进行修改。
### Code ::::info[Version 1.4.]{open}```cpp/*Version 1.0.Version 1.4.Trial Phase.Start at 2026/01/26.Last change at 2026/01/26.Start at 2026-01-26.Last change at 2026-03-11.Open source.*/
#include <iostream>3 行未改动#include <unordered_map>#include <algorithm>#include <cctype>#include <sstream>
class CodeSimplifier {public: CodeSimplifier() : currentVarIndex(0) { varNamePool.push_back("ans"); varNamePool.push_back("res"); varNamePool.push_back("val"); varNamePool.push_back("x"); varNamePool.push_back("y"); varNamePool.push_back("z"); varNamePool.push_back("tmp"); varNamePool.push_back("num"); varNamePool.push_back("cnt"); varNamePool.push_back("ret"); varNamePool.push_back("buf"); varNamePool.push_back("data"); 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(); initSpecialHeaders(); }
std::string simplifyCode(const std::string& inputCode) { std::string code = inputCode; code = removeAllComments(code); code = mergeHeaders(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)) {5 行未改动 return input; }
private: std::vector<std::string> varNamePool; int currentVarIndex; std::unordered_map<std::string, std::string> varMapping; std::unordered_set<std::string> excludeSet; // 初始化排除列表 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> specialHeaders;
void initExcludeSet() { excludeSet = { "dp", "int", "float", "double", "bool", "char", "void", "long", "short", "unsigned", "if", "else", "for", "while", "do", "switch", "case", "break", "return", "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", "include", "define", "ifdef", "ifndef", "endif", "pragma", "undef", "std", "cout", "cin", "endl", "string", "vector", "map", "set", "list", "array", "queue", "stack", "pair", "make_pair", "sort", "begin", "end", "size" "std", "cout", "cin", "endl" }; } // 判断是否是合法的变量名 bool isValidVarName(const std::string& name) { if (name.empty() || isdigit(name[0])) { return false;
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"; } } for (char c : name) { if (!isalnum(c) && c != '_') { return false;
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 = { "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, prevToken; std::stringstream lineSs(line); while (lineSs >> token) { if (varTypes.count(prevToken)) { std::string pureVarName; for (char c : token) { if (isalnum(c) || c == '_') pureVarName += c; else break; } if (!pureVarName.empty() && pureVarName.size() > 1 && excludeSet.count(pureVarName) == 0 && varMapping.count(pureVarName) == 0) { varMapping[pureVarName] = varNamePool[currentVarIndex % varNamePool.size()]; currentVarIndex++; } } prevToken = token; } } return excludeSet.find(name) == excludeSet.end(); } std::vector<std::string> splitToWords(const std::string& line) { std::vector<std::string> words; std::string word; for (char c : line) { if (isspace(c)) { if (!word.empty()) { words.push_back(word); word.clear();
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(); } } } else { word += c; } newCode += newLine + "\n"; } if (!word.empty()) { words.push_back(word); } return words; 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++; 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;3 行未改动 if (!inMultiLineComment && !inSingleLineComment) { result += c; } }
return result; } void extractAndMapVariables(const std::string& code) { initExcludeSet(); varMapping.clear();
std::string formatWithIndentation(const std::string& code) { std::stringstream ss(code); std::string line; std::vector<std::string> basicTypes = {"int", "float", "double", "bool", "char", "std::string", "vector", "map", "set", "list", "array"}; std::string line, formattedCode; int indentLevel = 0; const std::string tab = "\t";
while (std::getline(ss, line)) { std::string trimmedLine = line; trimmedLine.erase(trimmedLine.begin(), std::find_if(trimmedLine.begin(), trimmedLine.end(), [](unsigned char ch) { return !std::isspace(ch); }));
if (line.find("#") == 0) { continue; if (trimmedLine.empty()) { continue; }
std::vector<std::string> words = splitToWords(line); for (size_t i = 0; i < words.size(); ++i) { std::string word = words[i]; bool isBasicType = false; for (const std::string& type : basicTypes) { if (word == type) { isBasicType = true; break; } }
if (isBasicType && i+1 < words.size()) { std::string varName = words[i+1]; size_t sepPos = varName.find_first_of("=;,.()[]{}"); if (sepPos != std::string::npos) { varName = varName.substr(0, sepPos); } if (isValidVarName(varName) && varMapping.find(varName) == varMapping.end()) { varMapping[varName] = varNamePool[currentVarIndex % varNamePool.size()]; currentVarIndex++; } } int closeCount = std::count(trimmedLine.begin(), trimmedLine.end(), '}'); for (int i = 0; i < closeCount; i++) { indentLevel = std::max(0, indentLevel - 1); } } } // 替换代码中的变量名 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()] != '_'; formattedCode += std::string(indentLevel, '\t'); formattedCode += trimmedLine + "\n";
if (isWordStart && isWordEnd) { newLine.replace(pos, oldName.size(), newName); pos += newName.size(); } else { pos += oldName.size(); } } } newCode += newLine + "\n"; int openCount = std::count(trimmedLine.begin(), trimmedLine.end(), '{'); indentLevel += openCount; }
return newCode; 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;}```::::