增加头文件:
// 必须包含 Windows 头文件(Windows 系统专用)
#ifdef _WIN32
#include <windows.h>
// 兼容旧版编译器:如果未定义 CP_UTF8,手动定义(值为 65001)
#ifndef CP_UTF8
#define CP_UTF8 65001
#endif
#endif
main函数内增加:
// 强制设置输出流编码为 UTF-8(Windows/Linux/macOS 通用)
setlocale(LC_ALL, "zh_CN.UTF-8");
// 或针对 Windows 单独设置
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8); // 需要包含 windows.h
#endif