你有没有因为ai所写的代码中存在着太多不合你意的变量却懒得改?或着注释太多?或者代码不像是人写的?那么我相信这段代码能帮到你。
AI Code转换器(已开源)
【声明】:若您因为使用该程序被封或受到处罚,本人一概不负责。
/*
Version 1.0.
Trial Phase.
Start at 2026/01/26.
Last change at 2026/01/26.
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.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");
}
std::string simplifyCode(const std::string& inputCode) {
std::string code = inputCode;
code = removeAllComments(code);
extractAndMapVariables(code);
code = replaceVariables(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",
"int", "float", "double", "bool", "char", "void", "long", "short", "unsigned",
"if", "else", "for", "while", "do", "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"
};
}
// 判断是否是合法的变量名
bool isValidVarName(const std::string& name) {
if (name.empty() || isdigit(name[0])) {
return false;
}
for (char c : name) {
if (!isalnum(c) && c != '_') {
return false;
}
}
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();
}
} else {
word += c;
}
}
if (!word.empty()) {
words.push_back(word);
}
return words;
}
// 删除所有注释
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;
}
void extractAndMapVariables(const std::string& code) {
initExcludeSet();
varMapping.clear();
std::stringstream ss(code);
std::string line;
std::vector<std::string> basicTypes = {"int", "float", "double", "bool", "char", "std::string",
"vector", "map", "set", "list", "array"};
while (std::getline(ss, line)) {
if (line.find("#") == 0) {
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++;
}
}
}
}
}
// 替换代码中的变量名
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;
}
};
int main() {
CodeSimplifier simplifier;
std::string inputCode = simplifier.readMultiLineInput();
std::string simplifiedCode = simplifier.simplifyCode(inputCode);
std::cout << "\n===处理后的代码===\n";
std::cout << simplifiedCode << std::endl;
return 0;
}