:( 使用 1 作为密钥将“a”加密为“b”,预期为“密文:b\...”,而不是“明文:a\...”

问题描述 投票:0回答:1

我完成了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
c return command-line-arguments cs50 caesar-cipher
1个回答
0
投票

val = 编码( cipher_text )

在“cipher_text”两侧留出空间

这只是一个例子

不要这样使用 val = 编码( cipher_text) 或这个 val = 编码(cipher_text)

© www.soinside.com 2019 - 2024. All rights reserved.