gpt4 book ai didi

c - 在 C 中使用简单的 Cesar 密码

转载 作者:行者123 更新时间:2023-12-04 09:28:23 27 4
gpt4 key购买 nike

我在大学的一个项目中工作,他们使用的问题类似于典型的 Cesar Cipher。它更像是一个功能程序,需要是最基本的。

程序将从用户那里收到一个从 6590 的数字,例如当用户插入 65 时将显示 68。将添加 3 数字,但当用户输入 90 时,将输入 6790+3 ---->90,65,66,67。这是一个从 6590 的循环。

#include <stdio.h>

int cesar_encrypted(int x)
{
return (x+3);
}


void test_cesar_encrypted(void)
{
int x;
scanf("%d", &x);
int z = cesar_encrypted(x);
printf("%s\n", z);
}

int main(){
test_cesar_basic();

}

我做了这个示例代码,但我们只能更进一步,如果你给 90 他会给 93 而我想要 67

谁能帮我把它绕到 90 左右?

最佳答案

您可以使用模运算符,它给出除法的余数:

int cesar_encrypted(int x)
{
return (x - 65 + 3)%(91 - 65) + 65;
}

实现 Sulthan 的建议(见评论),它看起来像这样:

int cesar_encrypted(int x)
{
const int n_chars = 'Z' - 'A' + 1;
const int shift = 3;
return (x - 'A' + shift)%n_chars + 'A';
}

关于c - 在 C 中使用简单的 Cesar 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32736540/

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