【问题标题】:Caesar cipher code is not cleared by check50, cannot figure out why凯撒密码没有被check50清除,不知道为什么
【发布时间】:2020-06-07 21:54:37
【问题描述】:

我对作业进行了编码,并尽我所能。除了一个之外,我可以传递所有 Check50 参数!帮助??验证是正确的,但是当我运行调试器时,它开始给我围绕加密部分的问题。谢谢!

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main(int arg, string argv[])
{

//KEY VALIDATION
if (arg != 2) //checks to make sure that arg is 2 (for # of arguments)
{
    printf("Usage: ./caesar key\n");
    return 1;
}

else if  (arg == 2)
{ // checks key for validity
    for(int i = 0, len = strlen(argv[1]); i < len; i++)
    {
        if (!isdigit(argv[1][i]))
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }
    //int key = atoi(argv[1]);
}
//ciphering


int key = atoi(argv[1]);
string plain = get_string("plaintext: ");
int len_plain = strlen(plain);
string cipher = plain;

 for (int x = 0; x < len_plain; x++)
        {
            if (plain[x] >= 'a' && plain[x] <= 'z')
            {
                cipher[x] = ((plain[x] + key)%122);
                if (cipher[x]<97)
                {
                    cipher[x] = cipher[x] + 96;
                }
            }
            else if (plain[x] >= 'A' && plain[x] <= 'Z')
            {
                cipher[x] = ((plain[x] + key)%90);
                if (cipher[x] < 65)
                {
                    cipher[x] = cipher[x] + 64;
                }
            }
            else
            {
                cipher[x] = plain[x];
            }
        }
    printf("ciphertext: %s\n", cipher);
}

我不断得到的 check50 结果是,

:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:) encrypts "barfoo" as "yxocll" using 23 as key
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
***:( encrypts "barfoo" as "onesbb" using 65 as key
output not valid ASCII text

【问题讨论】:

  • 当你手动运行./caesar 65并输入barfoo时,你得到了什么结果?
  • 它在终端内部没有显示任何内容,但是当我使用调试器查看它时,分配给 cipher 的值是 0x603270 "\211\210\231\215\226\226"。对我来说真的很奇怪。
  • b 是 ascii 98(98+65)%122 == 41 最后是 41 + 96 == 137。但是137 不是您想要的字母字符。只需逐行浏览代码即可查看相同内容并了解它对其他角色的作用。也就是说,您的第一个 if 块逻辑看起来不正确。
  • 因为key 值会影响最终密码字符是否在有效的 alpha 范围内。
  • 我没说关键是问题。我的意思是您对密钥的代码处理是问题所在。所以现在你知道你的数学/逻辑是错误的。所以你需要坐下来拿一张纸来解决这个问题,然后再回去编码。

标签: c cs50 caesar-cipher


【解决方案1】:
printf("ciphertext: ");  
for (int x = 0, y = len_plain; x < y; x++)
    {
        //Use islower and isupper instead of typing so much words
        if islower(code[i])
        {
            printf("%c", (((code[i] + k) - 97) % 26) + 97);
        }
        else if isupper(code[i])
        {
            printf("%c", (((code[i] + k) - 65) % 26) + 65);
        }
        //If neither then just print it
        else
        {
            printf("%c", code[i]);
        }
    }
printf("\n")

【讨论】:

  • 这应该可以,但由于分配的具体情况,check50 不会清除它。程序中最后一行代码必须打印出ciphertext: 后面跟着改变的文本
  • Exaimz,建议您只需插入那几行代码。既然问题中没有具体说明,我们怎么知道这个细节?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 2020-05-31
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
相关资源
最近更新 更多