C++ 实现凯撒密码自动偏移推导与解密(健壮无错版)
好的!你希望将原本的 Python 解密程序(凯撒密码偏移推导 + 解密)**用 C++ 重写**,并确保语法正确、可编译运行。 下面是一个 **标准、健壮、无语法错误的 C++17 版本**,功能与你的 Python 代码完全一致: ✅ 输入:三行字符串(`plain`, `cipher`, `to_decrypt`) ✅ 推导 shift = `(cipher[0] - plain[0]) mod 26` ✅ 对 `to_decrypt` 中每个大写字母做逆凯撒解密 ✅ 输出解密后的字符串(全大写) --- ### ✅ C++ 版本(`Main.cpp`)—— 直接复制即可编译 ```cpp #include <iostream> #include <string> #include <cctype> #include <vector> int main() { std::string plain, cipher, to_decrypt; std::getline(std::cin, plain); std::getline(std::cin, cipher); std::getline(std::cin, to_decrypt); // 去除可能的回车/空格(兼容 Windows/Linux 换行) auto trim = [](std::string& s) { size_t start = s.find_first_not_of(" \t\r\n"); size_t end = s.find_last_not_of(" \t\r\n"); if (start == std::string::npos) s.clear(); else s = s.substr(start, end - start + 1); }; trim(plain); trim(cipher); trim(to_decrypt); // 安全检查:非空且首字符为大写字母 if (plain.empty() || cipher.empty() || to_decrypt.empty()) { std::cerr << "Error: empty input\n"; return 1; } if (!std::isupper(plain[0]) || !std::isupper(cipher[0])) { std::cerr << "Error: plain/cipher must start with uppercase letter\n"; return 1; } char p0 = plain[0], c0 = cipher[0]; int shift = (c0 - p0 + 26) % 26; // +26 防止负数取模 std::string res; for (char ch : to_decrypt) { if (std::isupper(ch)) { int x = ch - 'A'; int dec_x = (x - shift + 26) % 26; // +26 保证非负 res += ('A' + dec_x); } else { res += ch; // 保留非大写字母(如空格、标点等) } } std::cout << res << std::endl; return 0; } ``` --- ### 🛠️ 编译 & 运行命令(任选其一) #### ✅ Linux/macOS(g++): ```bash g++ -std=c++17 -O2 Main.cpp -o main ./main ``` 输入示例(手动输入或重定向): ``` ABC DEF HIJ ``` 输出: ``` EFG ``` #### ✅ Windows(g++ via MinGW / WSL): ```cmd g++ -std=c++17 Main.cpp -o main.exe main.exe ``` #### ✅ 在线编译器(推荐测试): - [Compiler Explorer (godbolt.org)](https://godbolt.org/) → 粘贴代码,选 `x86-64 g++ 13.2` - [OnlineGDB](https://www.onlinegdb.com/online_c++_compiler) → 新建 C++ 文件粘贴运行 --- ### 🔑 关键 C++ 注意点(对比 Python) | Python | C++ 注意事项 | |--------|---------------| | `input().strip()` | → 用 `std::getline` + 手动 `trim`(C++20 有 `std::ranges::trim`,但兼容性选手动) | | `ord(c) - ord('A')` | → `c - 'A'`(`char` 是整数,安全) | | `(x - shift) % 26` | → C++ 中负数 `%` 可能为负 → 改为 `(x - shift + 26) % 26` | | `''.join(res)` | → `std::string res; res += ch;` 自然拼接 | | 自动内存管理 | → 无指针/`new`,全程栈+`std::string`,零内存泄漏风险 | --- ### 🚨 常见编译错误规避清单 | 错误现象 | 原因 | 解决 | |----------|------|------| | `error: ‘getline’ is not a member of ‘std’` | 忘了 `#include <string>` | ✅ 已包含 | | `error: ‘isupper’ was not declared in scope` | 忘了 `#include <cctype>` | ✅ 已包含 | | `no matching function for call to ‘getline’` | 用了 `cin >> str` 导致换行符残留 | ✅ 全部用 `std::getline` + `trim` | | 输出乱码/多空行 | `std::cout << res << std::endl;` 正确(不是 `\n` 结尾漏 `\n`) | ✅ 已规范 | --- 需要我为你生成: - ✅ **带输入样例的自动测试脚本(bash/Python)** - ✅ **支持小写字母/大小写混合的增强版** - ✅ **使用 `std::transform` 的 STL 函数式写法** - ✅ **编译成 Windows `.exe` 的一键批处理(`.bat`)** 欢迎随时告诉我 👇 ######[AI写代码神器 | 1330点数解答 | 2026-03-16 12:29:37]
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)340
- 解决 consoleapplication40.cpp 中 buildtree 模板参数推导失败错误的方法(字节豆包 | 138点数解答 | 2024-11-10 23:42:06)238
- Python实现AES加密解密:含代码示例与关键注意点(字节豆包 | 836点数解答 | 2025-10-14 16:35:32)65
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)308
- 二手车交易价格预测赛题:思路剖析与程序实现全步骤揭秘(讯飞星火 | 1207点数解答 | 2024-11-17 18:48:26)356
- 揭秘!40 万条数据二手车交易价格预测赛题思路与程序实现全步骤(字节豆包 | 557点数解答 | 2024-11-17 18:50:01)251
- 二手车交易价格预测赛题:从数据处理到模型调优全流程揭秘(阿里通义 | 1736点数解答 | 2024-11-17 18:50:36)281
- 超便捷!网站会员注册流程及严格信息验证揭秘 (阿里通义 | 138点数解答 | 2024-03-06 17:37:05)256
- Java实现含姓名、性别等信息的注册功能,附Servlet处理及表单代码( | 2286点数解答 | 2024-04-02 14:54:20)297
- Java实现带验证码功能的用户登录程序:含示例代码与详细步骤( | 1826点数解答 | 2024-04-22 16:44:01)256
- ASP.NET 实现含多控件的注册界面:点击注册显示全信息 (字节豆包 | 873点数解答 | 2024-10-17 08:54:48)268
- 巧用服务器控件打造注册界面:实现信息收集与心得分享 (字节豆包 | 161点数解答 | 2024-10-17 09:20:08)419