gpt4 book ai didi

c++ - 扩展凯撒密码 C++

转载 作者:行者123 更新时间:2023-11-28 02:51:11 29 4
gpt4 key购买 nike

我正在尝试制作和扩展版本的 Caesar Cipher,包括用于 ASCII 值 32(这是一个空格)到 126(这是波浪号)的所有 7 位 ASCII 字符,但问题是我如何确保我的程序只使用那些 ASCII 字符,而不是跳转到使用奇怪的 DEL 符号或扩展的 ASCII 图表。

例如 - 如果我键入小写字母“u”并输入 10 作为键,我将得到 del 符号,但我想在键入小写字母“u”时得到一个空格

这是我目前的代码

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>

using namespace std;

string Encrypt(string, int);

int main(int argc, char *argv[])
{
string Source;
int Key;

cout << "Source: ";
getline(cin, Source);

cout << "Key: ";
cin >> Key;

cout << "Encrypted: " << Encrypt(Source, Key) << endl;
system("pause");
}

string Encrypt(string Source, int Key)
{
string Crypted = Source;

for (int Current = 0; Current < Source.length(); Current++)
Crypted[Current] += Key;

return Crypted;
}

最佳答案

简单:只需复制任何超出祝福范围的输入值而不做修改,你只需要两次比较:

if(x<' ' || x>126)
copy();
else
encode();

凯撒密码的算法是:

  1. 将所有有效符号映射到以0开头的连续非负数
  2. 添加 key 对符号计数取模
  3. 映射回去。

旁白:您可能还必须在应用前映射 key 。

关于c++ - 扩展凯撒密码 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23017981/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com