c - :( 将“a”加密为“b”,使用 1 作为密钥预期“密文:b\...”,而不是“明文:a\...”
问题描述
我完成了一个 Cypher 代码的编写,但是当我在 cs50 ide 上编译和运行时,它运行良好,没有任何错误,但是当我使用 check50 时,它会抛出一些让我感到困惑的错误。非常感谢任何帮助。这是我的错误:
:( encrypts "a" as "b" using 1 as key
expected "ciphertext: b\...", not "plaintext: a\..."
:( encrypts "barfoo" as "yxocll" using 23 as key
expected exit code 0, not 1
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
expected exit code 0, not 1
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
expected exit code 0, not 1
:( encrypts "barfoo" as "onesbb" using 65 as key
expected exit code 0, not 1
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: ia...", not "plaintext: wo..."
这是我写的代码:
int main(int argc, char* argv[])
{
int index[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
string str;
string plaintext;
int n;
char cyphertext[100];
int key;
if (argc == 2)
{
str = argv[1];
for(int i = 0;i < strlen(str); i++)
{
if(!isdigit(str[i]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
}
else
{
printf("Usage: ./caesar key\n");
}
if(argc == 2)
{
key = atoi(str); //convert string to int
plaintext = get_string("Text: "); // get plain text
for(int i = 0; i<strlen(plaintext); i++)
{
if(isupper(plaintext[i]))
{
n = 0;
n = index[plaintext[i] - 'A'];
cyphertext[i] = ((n + key) % 26) + 'A' ;
}
else if(islower(plaintext[i]))
{
n = 0;
n = index[plaintext[i] - 'a'];
cyphertext[i] = ((n + key) % 26 ) + 'a';
}
else
{
cyphertext[i] = plaintext[i];
}
}
printf("plaintext: %s\n",plaintext);
printf("ciphertext: %s\n",cyphertext);
}
return 1;
}
它说预期的退出代码 0,而不是 1 ,当我返回 0 时,它说:
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: ia...", not "plaintext: wo..."
:( handles lack of argv[1]
expected exit code 1, not 0
解决方案
推荐阅读
- actions-on-google - Actions on Google 和 Action Releases 中的新 Alpha 用户
- html - 当 Bootstrap 响应式汉堡菜单打开时,下拉菜单会破坏导航
- flutter - Null Check 运算符在使用 Sqflite 颤振时用于空值
- jestjs - 将 Vue 2 项目单元测试到 Vue 3 (vue-cli) 时出错
- javascript - 图像被 onclick 旋转截断
- django - Asyncwebsocketconsumer 断开连接方法在一些延迟后运行
- android - 如何在触控画布上显示线条?
- docker - 端口映射不适用于使用 tomcat 映像创建的容器
- angular - Angular 动态创建的组件未保持其状态
- github - 是否可以从 Shiny Dashboard 触发 GitHub 操作工作流?