【问题标题】:Why is this caesar encryption wrong when output looks correct?当输出看起来正确时,为什么这种凯撒加密是错误的?
【发布时间】:2020-04-13 10:49:45
【问题描述】:

在过去的几天里,我一直在做 cs50 项目“Caesar”,并设法让它在视觉输出方面达到一定的工作水平,但是每当我运行课程要求我运行的 check50 时,它似乎继续给我多个我无法直接找到的错误,因为在查看视觉输出时,这似乎是预期的。

这来自于在 cs50 IDE 中工作并遵循此项目链接:

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;
            }
       }
    }

我相信问题出在上面代码的输出部分,非常感谢您的帮助

【问题讨论】:

  • checking for output "ciphertext: yxocll\n"... 请注意,预期输出中的最后一个 \n。你确定要打印吗?
  • 非常感谢我完全忽略了这一点。看起来很简单..

标签: c encryption cs50


【解决方案1】:

答案来自“某程序员老兄”的 cmets

事实证明,我必须打印输出中的最后一个 '\n' 才能使其 100% 工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2015-01-08
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多