【工程】代码混淆
混淆一份代码,让洛谷无法看出你抄袭。
相当于加强版不可读代码风格转换器。
:::info[Ver 1.0] 使用方法:
把你的代码复制到
欢迎反馈
代码:
#include <iostream>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <cctype>
#include <random>
#include <algorithm>
// ----------------------------------------------------------------------
// 保留标识符集合(关键字 + 常用标准库名称 + 宏)
// 可根据需要自行扩充
// ----------------------------------------------------------------------
static const std::unordered_set<std::string> keywords = {
"alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit",
"atomic_noexcept", "auto", "bitand", "bitor", "bool", "break", "case", "catch",
"char", "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "const",
"consteval", "constexpr", "constinit", "const_cast", "continue", "contract_assert",
"co_await", "co_return", "co_yield", "decltype", "default", "delete", "do",
"double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false",
"float", "for", "friend", "goto", "if", "inline", "int", "long", "mutable",
"namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or",
"or_eq", "private", "protected", "public", "reflexpr", "register", "reinterpret_cast",
"requires", "return", "short", "signed", "sizeof", "static", "static_assert",
"static_cast", "struct", "switch", "synchronized", "template", "this", "thread_local",
"throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned",
"using", "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq",
// ========== 标准库命名空间 ==========
"std",
// ========== 核心容器 ==========
"array", "vector", "deque", "list", "forward_list",
"set", "map", "multiset", "multimap",
"unordered_set", "unordered_map", "unordered_multiset", "unordered_multimap",
"stack", "queue", "priority_queue",
"pair", "tuple", "optional", "variant", "any",
"bitset", "valarray",
// ========== 迭代器 ==========
"iterator", "back_inserter", "front_inserter", "inserter",
"reverse_iterator", "istream_iterator", "ostream_iterator",
"begin", "end", "advance", "next", "prev", "distance",
// ========== 算法(常用) ==========
"for_each", "count", "find", "search", "copy", "move", "transform",
"replace", "remove", "reverse", "rotate", "fill", "partition", "sort",
"stable_sort", "partial_sort", "nth_element", "lower_bound", "upper_bound",
"binary_search", "merge", "includes", "set_union", "set_intersection",
"set_difference", "make_heap", "push_heap", "pop_heap", "sort_heap",
"min", "max", "minmax", "min_element", "max_element", "equal",
"lexicographical_compare", "is_permutation", "next_permutation", "prev_permutation",
"all_of", "any_of", "none_of",
// ========== 字符串与字符处理 ==========
"string", "wstring", "u16string", "u32string", "string_view",
"to_string", "stoi", "stol", "stod",
"isalnum", "isalpha", "isdigit", "isspace", "toupper", "tolower",
"strcpy", "strncpy", "strcat", "strncat", "strcmp", "strlen", "strstr", "strchr",
// ========== 数学与数值 ==========
"abs", "sqrt", "pow", "sin", "cos", "log", "ceil", "floor", "round",
"complex", "numeric_limits", "ratio", "random_device", "mt19937",
"uniform_int_distribution", "uniform_real_distribution", "normal_distribution",
// ========== 输入/输出流 ==========
"cin", "cout", "cerr", "clog", "wcin", "wcout", "wcerr", "wclog",
"ifstream", "ofstream", "fstream", "istringstream", "ostringstream", "stringstream",
"endl", "flush", "boolalpha", "hex", "setprecision", "setw", "setfill",
"istream", "ostream", "iostream",
// ========== 函数对象与工具 ==========
"function", "bind", "ref", "cref",
"plus", "minus", "multiplies", "divides", "modulus", "negate",
"less", "greater", "less_equal", "greater_equal", "equal_to", "not_equal_to",
"logical_and", "logical_or", "logical_not", "bit_and", "bit_or", "bit_xor",
// ========== 内存管理 ==========
"shared_ptr", "unique_ptr", "weak_ptr", "allocator", "addressof",
// ========== 时间与日期 ==========
"chrono", "system_clock", "steady_clock", "high_resolution_clock",
"duration", "time_point", "time", "localtime", "strftime",
// ========== 错误与异常处理 ==========
"exception", "runtime_error", "logic_error", "bad_alloc", "out_of_range",
"error_code", "error_condition", "system_error", "assert",
// ========== 并发支持 ==========
"thread", "this_thread", "mutex", "recursive_mutex", "timed_mutex",
"lock_guard", "unique_lock", "condition_variable", "atomic",
"future", "promise", "async", "packaged_task",
// ========== 正则表达式 ==========
"regex", "smatch", "regex_match", "regex_search", "regex_replace",
// ========== 元编程与类型支持 ==========
"is_integral", "is_floating_point", "is_same", "remove_const", "remove_pointer",
"enable_if", "conditional", "decay", "declval",
"int8_t", "int16_t", "int32_t", "int64_t", "uint8_t", "uint16_t", "uint32_t", "uint64_t",
"type_index", "type_info",
// ========== 范围库 (C++20) ==========
"ranges", "views", "filter", "transform", "take", "drop",
// ========== C 库函数(常见) ==========
"malloc", "free", "calloc", "realloc", "exit", "abort", "system",
"printf", "scanf", "fprintf", "fscanf", "sprintf", "sscanf",
"fopen", "fclose", "fread", "fwrite", "fgetc", "fputc", "fgets", "fputs",
"static", "const", "std", "unordered_set", "unordered_map", "string",
"vector", "size_t", "substr", "push_back", "reserve", "begin", "end",
"find", "sync_with_stdio", "tie", "istreambuf_iterator", "cout", "cin",
// 类型与命名空间
"ios",
// 成员函数
"size", "second", "find_if", "istreambuf_iterator",
// 其他常用
"begin", "end", "cbegin", "cend", // 部分已存在,确保有
"rbegin", "rend",
"push_back", "pop_back", "reserve", "capacity", "clear", "empty",
"substr", "compare", "find", "rfind",
"c_str", "data", "copy", "swap",
"getline", "ignore", "peek", "putback",
"sync_with_stdio", "tie", "rdbuf",
"good", "fail", "bad", "eof",
"open", "close", "is_open",
"precision", "width", "fill", "setf", "unsetf",
"join", "detach", "lock", "unlock", "try_lock",
"get", "reset", "release", "use_count", "unique",
"main",
// 常用标准库函数
"getchar", "putchar", "getc", "putc", "ungetc",
// 标准库类型别名
"size_t", "ptrdiff_t", "nullptr_t",
// 流相关
"ios", "istream", "ostream", "iostream",
"istreambuf_iterator", "ostreambuf_iterator",
// 算法函数对象
"find_if", "find_if_not", "remove_if",
// pair 成员
"first", "second",
// 其他常用
"endl", "ends", "flush",
"boolalpha", "noboolalpha", "hex", "dec", "oct",
"setw", "setfill", "setprecision",
// 异常类
"exception", "runtime_error", "logic_error",
// 智能指针成员函数(已在列表,但确认)
"get", "reset", "release", "use_count",
// 线程成员
"join", "detach", "swap",
// 互斥锁
"lock", "unlock", "try_lock",
// 条件变量
"wait", "notify_one", "notify_all",
// 原子操作
"load", "store", "exchange", "compare_exchange_weak",
// 文件流
"open", "close", "is_open", "rdbuf",
// string 成员(部分已存在)
"c_str", "data", "copy", "find", "rfind", "find_first_of", "find_last_of",
"substr", "compare", "replace", "insert", "erase", "push_back", "pop_back",
"clear", "empty", "size", "length", "capacity", "reserve", "shrink_to_fit",
// 迭代器操作
"advance", "distance", "next", "prev",
// 容器成员
"begin", "end", "cbegin", "cend", "rbegin", "rend", "crbegin", "crend",
"front", "back", "at", "assign", "swap",
// 数值限制
"numeric_limits", "epsilon", "round_error", "min", "max", "lowest",
// 随机数引擎
"random_device", "mt19937", "mt19937_64", "uniform_int_distribution",
// 时间库
"chrono", "duration", "time_point", "system_clock", "steady_clock",
// 正则表达式
"regex", "smatch", "regex_match", "regex_search", "regex_replace",
// 类型特性
"is_same", "is_integral", "is_floating_point", "remove_const", "remove_pointer",
// 函数式
"function", "bind", "ref", "cref", "placeholders",
// 内存管理
"shared_ptr", "unique_ptr", "weak_ptr", "make_shared", "make_unique",
// C 库
"malloc", "free", "calloc", "realloc", "exit", "abort", "atexit",
"printf", "scanf", "fprintf", "fscanf", "sprintf", "sscanf",
"fopen", "fclose", "fread", "fwrite", "fgetc", "fputc", "fgets", "fputs",
"strcpy", "strncpy", "strcat", "strncat", "strcmp", "strncmp", "strlen", "strchr", "strstr",
"isalnum", "isalpha", "isdigit", "isspace", "toupper", "tolower",
// 数学函数
"abs", "labs", "llabs", "fabs", "sqrt", "pow", "sin", "cos", "tan",
"asin", "acos", "atan", "atan2", "exp", "log", "log10", "ceil", "floor", "round",
// 算法常用
"for_each", "count", "count_if", "find", "find_if", "find_end", "find_first_of",
"search", "search_n", "copy", "copy_if", "copy_n", "move", "transform",
"replace", "replace_if", "remove", "remove_if", "reverse", "rotate",
"fill", "fill_n", "generate", "generate_n", "partition", "stable_partition",
"sort", "stable_sort", "partial_sort", "nth_element", "lower_bound", "upper_bound",
"binary_search", "merge", "inplace_merge", "includes", "set_union", "set_intersection",
"set_difference", "set_symmetric_difference", "make_heap", "push_heap", "pop_heap",
"sort_heap", "min", "max", "minmax", "min_element", "max_element", "minmax_element",
"equal", "lexicographical_compare", "is_permutation", "next_permutation", "prev_permutation",
"all_of", "any_of", "none_of",
// 标准宏(通常在预处理阶段已展开,但为避免替换,加入保留集)
"EOF", "NULL", "BUFSIZ", "FILENAME_MAX", "FOPEN_MAX", "TMP_MAX",
"L_tmpnam", "SEEK_CUR", "SEEK_END", "SEEK_SET",
"EXIT_SUCCESS", "EXIT_FAILURE", "RAND_MAX", "CLOCKS_PER_SEC",
"EDOM", "ERANGE", "EILSEQ",
// 流操作符(iomanip)
"fixed", "scientific", "hexfloat", "defaultfloat",
"setprecision", "setw", "setfill", "setbase",
"left", "right", "internal", "showbase", "noshowbase",
"showpoint", "noshowpoint", "showpos", "noshowpos",
"uppercase", "nouppercase", "unitbuf", "nounitbuf",
// 其他常用标准库名称
"endl", "ends", "flush", "boolalpha", "noboolalpha",
"hex", "dec", "oct", "ws", "skipws", "noskipws",
// C++ 关键字
"alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor",
"bool", "break", "case", "catch", "char", "char8_t", "char16_t", "char32_t",
"class", "compl", "concept", "const", "consteval", "constexpr", "constinit",
"const_cast", "continue", "co_await", "co_return", "co_yield", "decltype",
"default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit",
"export", "extern", "false", "float", "for", "friend", "goto", "if", "inline",
"int", "long", "mutable", "namespace", "new", "noexcept", "not", "not_eq",
"nullptr", "operator", "or", "or_eq", "private", "protected", "public",
"register", "reinterpret_cast", "requires", "return", "short", "signed",
"sizeof", "static", "static_assert", "static_cast", "struct", "switch",
"template", "this", "thread_local", "throw", "true", "try", "typedef",
"typeid", "typename", "union", "unsigned", "using", "virtual", "void",
"volatile", "wchar_t", "while", "xor", "xor_eq",
// 标准库命名空间
"std",
// 常用类型
"string", "vector", "unordered_set", "unordered_map", "array", "list",
"deque", "forward_list", "set", "map", "multiset", "multimap", "pair",
"tuple", "optional", "variant", "any", "bitset", "valarray", "complex",
"function", "shared_ptr", "unique_ptr", "weak_ptr", "allocator", "ios",
"istream", "ostream", "iostream", "fstream", "sstream", "istringstream",
"ostringstream", "stringstream", "ifstream", "ofstream", "random_device",
"mt19937", "uniform_int_distribution", "uniform_real_distribution",
"chrono", "duration", "time_point", "system_clock", "steady_clock",
"regex", "smatch", "thread", "mutex", "condition_variable", "atomic",
"future", "promise", "async", "packaged_task",
// 常用函数
"getchar", "putchar", "getc", "putc", "ungetc", "getchar", "putchar",
"printf", "scanf", "fprintf", "fscanf", "sprintf", "sscanf", "fopen",
"fclose", "fread", "fwrite", "fgetc", "fputc", "fgets", "fputs",
"malloc", "free", "calloc", "realloc", "exit", "abort", "atexit",
"abs", "labs", "llabs", "fabs", "sqrt", "pow", "sin", "cos", "tan",
"asin", "acos", "atan", "atan2", "exp", "log", "log10", "ceil", "floor",
"round", "isalnum", "isalpha", "isdigit", "isspace", "toupper", "tolower",
"strcpy", "strncpy", "strcat", "strncat", "strcmp", "strncmp", "strlen",
"strchr", "strstr", "memcpy", "memmove", "memset",
// 常用成员函数
"size", "length", "empty", "clear", "reserve", "capacity", "substr",
"find", "rfind", "find_first_of", "find_last_of", "c_str", "data",
"copy", "compare", "replace", "insert", "erase", "push_back", "pop_back",
"push_front", "pop_front", "begin", "end", "rbegin", "rend", "cbegin",
"cend", "front", "back", "at", "assign", "swap", "shrink_to_fit",
"get", "reset", "release", "use_count", "unique", "join", "detach",
"lock", "unlock", "try_lock", "wait", "notify_one", "notify_all",
"load", "store", "exchange", "compare_exchange_weak", "open", "close",
"is_open", "rdbuf", "sync_with_stdio", "tie", "good", "fail", "bad",
"eof", "precision", "width", "fill", "setf", "unsetf",
// 算法函数
"for_each", "count", "count_if", "find", "find_if", "find_end",
"find_first_of", "search", "search_n", "copy", "copy_if", "copy_n",
"move", "transform", "replace", "replace_if", "remove", "remove_if",
"reverse", "rotate", "fill", "fill_n", "generate", "generate_n",
"partition", "stable_partition", "sort", "stable_sort", "partial_sort",
"nth_element", "lower_bound", "upper_bound", "binary_search", "merge",
"inplace_merge", "includes", "set_union", "set_intersection",
"set_difference", "set_symmetric_difference", "make_heap", "push_heap",
"pop_heap", "sort_heap", "min", "max", "minmax", "min_element",
"max_element", "minmax_element", "equal", "lexicographical_compare",
"is_permutation", "next_permutation", "prev_permutation", "all_of",
"any_of", "none_of",
// 流操作符
"fixed", "scientific", "hexfloat", "defaultfloat", "setprecision",
"setw", "setfill", "setbase", "left", "right", "internal", "showbase",
"noshowbase", "showpoint", "noshowpoint", "showpos", "noshowpos",
"uppercase", "nouppercase", "unitbuf", "nounitbuf", "endl", "ends",
"flush", "boolalpha", "noboolalpha", "hex", "dec", "oct", "ws",
"skipws", "noskipws",
// 宏
"EOF", "NULL", "BUFSIZ", "FILENAME_MAX", "FOPEN_MAX", "TMP_MAX",
"L_tmpnam", "SEEK_CUR", "SEEK_END", "SEEK_SET", "EXIT_SUCCESS",
"EXIT_FAILURE", "RAND_MAX", "CLOCKS_PER_SEC", "EDOM", "ERANGE", "EILSEQ",
// 其他常用
"main", "size_t", "ptrdiff_t", "nullptr_t", "int8_t", "int16_t",
"int32_t", "int64_t", "uint8_t", "uint16_t", "uint32_t", "uint64_t",
"first", "second"
};
// ----------------------------------------------------------------------
// 生成 4 位随机字符串(首字母为字母)
// ----------------------------------------------------------------------
std::string randomFour() {
static const char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static const char alnum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> letDist(0, sizeof(letters)-2);
static std::uniform_int_distribution<> alnDist(0, sizeof(alnum)-2);
std::string result;
result.reserve(4);
result.push_back(letters[letDist(gen)]);
for (int i = 0; i < 3; ++i) {
result.push_back(alnum[alnDist(gen)]);
}
return result;
}
// ----------------------------------------------------------------------
// 删除所有注释
// ----------------------------------------------------------------------
std::string removeComments(const std::string& code) {
std::string result;
size_t i = 0;
const size_t n = code.size();
while (i < n) {
if (code[i] == '"') {
result += code[i++];
while (i < n) {
if (code[i] == '\\') {
result += code[i++];
if (i < n) result += code[i++];
} else if (code[i] == '"') {
result += code[i++];
break;
} else {
result += code[i++];
}
}
} else if (code[i] == '\'') {
result += code[i++];
while (i < n) {
if (code[i] == '\\') {
result += code[i++];
if (i < n) result += code[i++];
} else if (code[i] == '\'') {
result += code[i++];
break;
} else {
result += code[i++];
}
}
} else if (code[i] == '/' && i + 1 < n) {
if (code[i + 1] == '/') {
i += 2;
while (i < n && code[i] != '\n') ++i;
if (i < n && code[i] == '\n') {
result += '\n';
++i;
}
} else if (code[i + 1] == '*') {
i += 2;
while (i + 1 < n && !(code[i] == '*' && code[i + 1] == '/')) ++i;
if (i + 1 < n) i += 2;
} else {
result += code[i++];
}
} else {
result += code[i++];
}
}
return result;
}
// ----------------------------------------------------------------------
// 替换标识符,保留预处理指令和宏名
// ----------------------------------------------------------------------
std::string replaceIdentifiers(const std::string& code) {
std::string result;
size_t i = 0;
const size_t n = code.size();
std::unordered_map<std::string, std::string> idMap;
// 辅助函数:判断字符是否为空白(非换行)
auto isSpace = [](char ch) { return std::isspace(static_cast<unsigned char>(ch)) && ch != '\n'; };
while (i < n) {
// 预处理指令检测:跳过行首空白,判断是否为 '#'
size_t line_start = i;
while (line_start < n && isSpace(code[line_start])) ++line_start;
if (line_start < n && code[line_start] == '#') {
// 找到行尾
size_t line_end = line_start;
while (line_end < n && code[line_end] != '\n') ++line_end;
// 如果是 #define,提取宏名
if (line_end - line_start >= 7 && code.substr(line_start, 7) == "#define") {
size_t pos = line_start + 7;
while (pos < line_end && isSpace(code[pos])) ++pos;
if (pos < line_end && (std::isalpha(code[pos]) || code[pos] == '_')) {
size_t name_start = pos;
while (pos < line_end && (std::isalnum(code[pos]) || code[pos] == '_')) ++pos;
std::string macroName = code.substr(name_start, pos - name_start);
// 将宏名加入 idMap(映射到自身),避免被替换
idMap[macroName] = macroName;
}
}
// 输出整行(包括行首空白)
result += code.substr(i, line_end - i);
if (line_end < n && code[line_end] == '\n') {
result += '\n';
++line_end;
}
i = line_end;
continue;
}
// 字符串字面量
if (code[i] == '"') {
result += code[i++];
while (i < n) {
if (code[i] == '\\') {
result += code[i++];
if (i < n) result += code[i++];
} else if (code[i] == '"') {
result += code[i++];
break;
} else {
result += code[i++];
}
}
continue;
}
// 字符常量
if (code[i] == '\'') {
result += code[i++];
while (i < n) {
if (code[i] == '\\') {
result += code[i++];
if (i < n) result += code[i++];
} else if (code[i] == '\'') {
result += code[i++];
break;
} else {
result += code[i++];
}
}
continue;
}
// 数字字面量
if (std::isdigit(static_cast<unsigned char>(code[i])) ||
(code[i] == '.' && i+1 < n && std::isdigit(static_cast<unsigned char>(code[i+1])))) {
size_t start = i;
// 十六进制
if (code[i] == '0' && i+1 < n && (code[i+1] == 'x' || code[i+1] == 'X')) {
i += 2;
while (i < n && std::isxdigit(static_cast<unsigned char>(code[i]))) ++i;
} else {
while (i < n && std::isdigit(static_cast<unsigned char>(code[i]))) ++i;
if (i < n && code[i] == '.') {
++i;
while (i < n && std::isdigit(static_cast<unsigned char>(code[i]))) ++i;
}
if (i < n && (code[i] == 'e' || code[i] == 'E')) {
++i;
if (i < n && (code[i] == '+' || code[i] == '-')) ++i;
if (i < n && std::isdigit(static_cast<unsigned char>(code[i]))) {
while (i < n && std::isdigit(static_cast<unsigned char>(code[i]))) ++i;
} else {
i = start;
result += code[i++];
continue;
}
}
}
while (i < n && (code[i] == 'u' || code[i] == 'U' ||
code[i] == 'l' || code[i] == 'L' ||
code[i] == 'f' || code[i] == 'F')) ++i;
if (i+1 < n && ((code[i-1] == 'l' || code[i-1] == 'L') &&
(code[i] == 'l' || code[i] == 'L'))) ++i;
result += code.substr(start, i - start);
continue;
}
// 标识符
if (std::isalpha(static_cast<unsigned char>(code[i])) || code[i] == '_') {
size_t start = i;
while (i < n && (std::isalnum(static_cast<unsigned char>(code[i])) || code[i] == '_')) ++i;
std::string ident = code.substr(start, i - start);
// 先查 idMap(包含宏名和已映射的用户标识符)
auto it = idMap.find(ident);
if (it != idMap.end()) {
result += it->second;
continue;
}
// 再查保留集
if (keywords.find(ident) != keywords.end()) {
result += ident;
continue;
}
// 用户自定义标识符,生成随机名
std::string newName;
do {
newName = randomFour();
} while (keywords.find(newName) != keywords.end() ||
std::find_if(idMap.begin(), idMap.end(),
[&](const auto& p) { return p.second == newName; }) != idMap.end());
idMap[ident] = newName;
result += newName;
continue;
}
// 其他字符
result += code[i++];
}
return result;
}
// ----------------------------------------------------------------------
// 将代码拆分为 token,每个 token 后加换行
// ----------------------------------------------------------------------
std::string tokenizeAndInsertNewlines(const std::string& code) {
static const std::vector<std::string> multiOps = {
">>=", "<<=", "->*", ".*", "::", "++", "--", "->", "&&", "||",
"<<", ">>", "<=", ">=", "==", "!=", "+=", "-=", "*=", "/=", "%=",
"&=", "|=", "^=", "##"
};
std::vector<std::string> tokens;
size_t i = 0;
const size_t n = code.size();
auto isSpace = [](char ch) { return std::isspace(static_cast<unsigned char>(ch)); };
while (i < n) {
// 跳过空白
if (isSpace(code[i])) {
++i;
continue;
}
// 预处理指令整行作为一个 token
size_t line_start = i;
while (line_start < n && isSpace(code[line_start]) && code[line_start] != '\n') ++line_start;
if (line_start < n && code[line_start] == '#') {
size_t line_end = line_start;
while (line_end < n && code[line_end] != '\n') ++line_end;
tokens.push_back(code.substr(line_start, line_end - line_start));
i = line_end;
continue;
}
// 字符串字面量
if (code[i] == '"') {
size_t start = i;
++i;
while (i < n) {
if (code[i] == '\\') {
++i;
if (i < n) ++i;
} else if (code[i] == '"') {
++i;
break;
} else {
++i;
}
}
tokens.push_back(code.substr(start, i - start));
continue;
}
// 字符常量
if (code[i] == '\'') {
size_t start = i;
++i;
while (i < n) {
if (code[i] == '\\') {
++i;
if (i < n) ++i;
} else if (code[i] == '\'') {
++i;
break;
} else {
++i;
}
}
tokens.push_back(code.substr(start, i - start));
continue;
}
// 数字字面量
if (std::isdigit(static_cast<unsigned char>(code[i])) ||
(code[i] == '.' && i+1 < n && std::isdigit(static_cast<unsigned char>(code[i+1])))) {
size_t start = i;
if (code[i] == '0' && i+1 < n && (code[i+1] == 'x' || code[i+1] == 'X')) {
i += 2;
while (i < n && std::isxdigit(static_cast<unsigned char>(code[i]))) ++i;
} else {
while (i < n && std::isdigit(static_cast<unsigned char>(code[i]))) ++i;
if (i < n && code[i] == '.') {
++i;
while (i < n && std::isdigit(static_cast<unsigned char>(code[i]))) ++i;
}
if (i < n && (code[i] == 'e' || code[i] == 'E')) {
++i;
if (i < n && (code[i] == '+' || code[i] == '-')) ++i;
if (i < n && std::isdigit(static_cast<unsigned char>(code[i]))) {
while (i < n && std::isdigit(static_cast<unsigned char>(code[i]))) ++i;
} else {
i = start;
tokens.push_back(std::string(1, code[i++]));
continue;
}
}
}
while (i < n && (code[i] == 'u' || code[i] == 'U' ||
code[i] == 'l' || code[i] == 'L' ||
code[i] == 'f' || code[i] == 'F')) ++i;
if (i+1 < n && ((code[i-1] == 'l' || code[i-1] == 'L') &&
(code[i] == 'l' || code[i] == 'L'))) ++i;
tokens.push_back(code.substr(start, i - start));
continue;
}
// 标识符
if (std::isalpha(static_cast<unsigned char>(code[i])) || code[i] == '_') {
size_t start = i;
while (i < n && (std::isalnum(static_cast<unsigned char>(code[i])) || code[i] == '_')) ++i;
tokens.push_back(code.substr(start, i - start));
continue;
}
// 多字符操作符
bool matched = false;
for (const auto& op : multiOps) {
if (i + op.size() <= n && code.substr(i, op.size()) == op) {
tokens.push_back(op);
i += op.size();
matched = true;
break;
}
}
if (!matched) {
tokens.push_back(std::string(1, code[i]));
++i;
}
}
std::string result;
for (const auto& tok : tokens) {
result += tok;
result += '\n';
}
return result;
}
// ----------------------------------------------------------------------
// 主函数
// ----------------------------------------------------------------------
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string code((std::istreambuf_iterator<char>(std::cin)),
std::istreambuf_iterator<char>());
code = removeComments(code);
code = replaceIdentifiers(code);
code = tokenizeAndInsertNewlines(code);
std::cout << code;
return 0;
}
``
:::