- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在研究凯撒密码的解密函数。我根据我编写的加密函数对其进行了建模,与解密函数不同,它工作得很好。
我的代码编译没有任何错误并执行。它能够解密五个字母或更少的单词,但不能解密超过五个字母以及包含两个或更多单词的句子。
当键的值 <=12 时,它还会为文本生成错误输出。为什么会出现这些错误?任何形式的帮助将不胜感激。提前谢谢你。
#include <stdio.h>
/
}
最佳答案
问题是模运算符并不总是返回正值:-1 % 26
给出 -1
,这使得您的 decrypt_ltr
函数返回 'a'
之外的字符 - 'z'
范围(或 'A'
-'Z'
范围)。例如,当用 key 2解密'a
'时,你将得到:'a' - 'a' - key
= -key
。然后你将执行 -key + 'a'
,这仍然小于 'a'
。
由于您的 key 保证在 1 到 26 之间,您可以简单地将 26 添加到 (alpha-'A') - key
值,如下所示:
char decrypt_ltr(char alpha, int key)
{
if (alpha >= 'A' && alpha <= 'Z')
{
alpha = ((alpha-'A') - key + 26) % 26 + 'A'; // the 26 ensures that the value
// is positive before the modulo
// operator is applied and is
// removed by the modulo operator
}
else if(alpha >= 'a' && alpha <= 'z')
{
alpha = ((alpha-'a') - key + 26) % 26 + 'a';
}
return alpha;
}
关于c - 为什么我的凯撒密码解密函数会产生错误的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55859798/
我刚刚开始学习 C 语言类(class),并且遇到了命令行参数的问题。分配是这样的(还有更多,但这是开头有关命令行参数的部分): - 你的程序必须接受一个命令行参数,一个非负整数。 - 如果您的程序在
我需要检查命令行参数中是否有非数字字符。例如: ./problem 20x 应该打印出“不是数字”,因为它包含一个 x。我的代码似乎没有循环遍历命令行参数中的所有字符。 我基本上尝试了不同类型的循环,
这里我有从标准输入将字符流输入到数组中的代码。然后将该数组转换为二维数组。然后,它将该数组从行列顺序更改为列行顺序。然后它打印出创建凯撒移位加密的新数组。我遇到的问题是我的数组开始使用第二个用户输入的
我有点被这个问题困住了。当我运行程序时,由于某种原因,循环经过 z 的所有字母都不会打印。问题来自这个链接:http://docs.cs50.net/2016/x/ap/problems/caesar
我是一名优秀的程序员,十分优秀!