当输出看起来正确时,为什么这个凯撒加密是错误的?
Why is this caesar encryption wrong when output looks correct?
过去几天我一直在做 cs50 项目 'Caesar' 并设法让它在视觉输出方面工作,但是每当我 运行 check50该课程要求我 运行 它似乎不断给我多个错误,我无法直接找到这些错误,因为在查看视觉输出时似乎这是预期的。
这来自于在 cs50 IDE 中工作并关注此项目 link:
https://cs50.harvard.edu/x/2020/psets/2/caesar/
以下是 cs50 check50 函数给出的结果:
> :( encrypts "a" as "b" using 1 as key
> expected "ciphertext: b\...", not "ciphertext: b"
> Log
> running ./caesar 1...
> sending input a...
> checking for output "ciphertext: b\n"...
>
> Expected Output:
> ciphertext: b
> Actual Output:
> ciphertext: b
> :( encrypts "barfoo" as "yxocll" using 23 as key
> expected "ciphertext: yx...", not "ciphertext: yx..."
> Log
> running ./caesar 23...
> sending input barfoo...
> checking for output "ciphertext: yxocll\n"...
>
> Expected Output:
> ciphertext: yxocll
> Actual Output:
> ciphertext: yxocll
> :( encrypts "BARFOO" as "EDUIRR" using 3 as key
> expected "ciphertext: ED...", not "ciphertext: ED..."
> Log
> running ./caesar 3...
> sending input BARFOO...
> checking for output "ciphertext: EDUIRR\n"...
>
> Expected Output:
> ciphertext: EDUIRR
> Actual Output:
> ciphertext: EDUIRR
> :( encrypts "BaRFoo" as "FeVJss" using 4 as key
> expected "ciphertext: Fe...", not "ciphertext: Fe..."
> Log
> running ./caesar 4...
> sending input BaRFoo...
> checking for output "ciphertext: FeVJss\n"...
>
> Expected Output:
> ciphertext: FeVJss
> Actual Output:
> ciphertext: FeVJss
> :( encrypts "barfoo" as "onesbb" using 65 as key
> expected "ciphertext: on...", not "ciphertext: on..."
> Log
> running ./caesar 65...
> sending input barfoo...
> checking for output "ciphertext: onesbb\n"...
>
> Expected Output:
> ciphertext: onesbb
> Actual Output:
> ciphertext: onesbb
> :( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
> expected "ciphertext: ia...", not "ciphertext: ia..."
> Log
> running ./caesar 12...
> sending input world, say hello!...
> checking for output "ciphertext: iadxp, emk tqxxa!\n"...
>
>
> <<
以下是使用 cs50 库以 C 语言完成的代码,以实现更简单的功能:
//string validity
if (validity)
{
//prompt for plaintext
string plaintext = get_string("Plaintext: ");
printf("ciphertext: ");
int n = strlen(plaintext);
//calculate the encryption
for (int i = 0; i < n; i++)
{
if (isupper(plaintext[i]))
{
printf("%c", (plaintext[i] - 'A' + KEY) % 26 + 'A');
}
else if (islower(plaintext[i]))
{
printf("%c", (plaintext[i] - 'a' + KEY) % 26 + 'a');
}
else if( ispunct(plaintext[i]) || isspace(plaintext[i]))
{
printf("%c", plaintext[i]);
}
}
return 0;
}
}
}
我认为问题出在上面代码的输出部分,非常感谢您的帮助
答案来自'Some programmer dude'
的评论
事实证明,我必须在输出中打印最后一个 '\n' 才能使其正常工作 100%
过去几天我一直在做 cs50 项目 'Caesar' 并设法让它在视觉输出方面工作,但是每当我 运行 check50该课程要求我 运行 它似乎不断给我多个错误,我无法直接找到这些错误,因为在查看视觉输出时似乎这是预期的。
这来自于在 cs50 IDE 中工作并关注此项目 link:
https://cs50.harvard.edu/x/2020/psets/2/caesar/
以下是 cs50 check50 函数给出的结果:
> :( encrypts "a" as "b" using 1 as key
> expected "ciphertext: b\...", not "ciphertext: b"
> Log
> running ./caesar 1...
> sending input a...
> checking for output "ciphertext: b\n"...
>
> Expected Output:
> ciphertext: b
> Actual Output:
> ciphertext: b
> :( encrypts "barfoo" as "yxocll" using 23 as key
> expected "ciphertext: yx...", not "ciphertext: yx..."
> Log
> running ./caesar 23...
> sending input barfoo...
> checking for output "ciphertext: yxocll\n"...
>
> Expected Output:
> ciphertext: yxocll
> Actual Output:
> ciphertext: yxocll
> :( encrypts "BARFOO" as "EDUIRR" using 3 as key
> expected "ciphertext: ED...", not "ciphertext: ED..."
> Log
> running ./caesar 3...
> sending input BARFOO...
> checking for output "ciphertext: EDUIRR\n"...
>
> Expected Output:
> ciphertext: EDUIRR
> Actual Output:
> ciphertext: EDUIRR
> :( encrypts "BaRFoo" as "FeVJss" using 4 as key
> expected "ciphertext: Fe...", not "ciphertext: Fe..."
> Log
> running ./caesar 4...
> sending input BaRFoo...
> checking for output "ciphertext: FeVJss\n"...
>
> Expected Output:
> ciphertext: FeVJss
> Actual Output:
> ciphertext: FeVJss
> :( encrypts "barfoo" as "onesbb" using 65 as key
> expected "ciphertext: on...", not "ciphertext: on..."
> Log
> running ./caesar 65...
> sending input barfoo...
> checking for output "ciphertext: onesbb\n"...
>
> Expected Output:
> ciphertext: onesbb
> Actual Output:
> ciphertext: onesbb
> :( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
> expected "ciphertext: ia...", not "ciphertext: ia..."
> Log
> running ./caesar 12...
> sending input world, say hello!...
> checking for output "ciphertext: iadxp, emk tqxxa!\n"...
>
>
> <<
以下是使用 cs50 库以 C 语言完成的代码,以实现更简单的功能:
//string validity
if (validity)
{
//prompt for plaintext
string plaintext = get_string("Plaintext: ");
printf("ciphertext: ");
int n = strlen(plaintext);
//calculate the encryption
for (int i = 0; i < n; i++)
{
if (isupper(plaintext[i]))
{
printf("%c", (plaintext[i] - 'A' + KEY) % 26 + 'A');
}
else if (islower(plaintext[i]))
{
printf("%c", (plaintext[i] - 'a' + KEY) % 26 + 'a');
}
else if( ispunct(plaintext[i]) || isspace(plaintext[i]))
{
printf("%c", plaintext[i]);
}
}
return 0;
}
}
}
我认为问题出在上面代码的输出部分,非常感谢您的帮助
答案来自'Some programmer dude'
的评论事实证明,我必须在输出中打印最后一个 '\n' 才能使其正常工作 100%