CS50 - 输出无效的 ASCII 文本 - 第 2 周问题集 2 caesar

CS50 - output not valid ASCII text - week 2 problem set 2 caesar

当我 运行 check50 我得到这个:

:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:( encrypts "barfoo" as "yxocll" using 23 as key
output not valid ASCII text
:) 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
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
output not valid ASCII text
:) handles lack of argv[1]
:) handles non-numeric key
:) handles too many arguments

我不确定“无效的 ASCII 文本”是什么意思或错误是什么。当我 运行 我得到“错误”的问题时,我在终端中得到了正确的输出,但 check50 将其标记为错误。

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

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        return 1;
    }
    
    string bob=argv[1];
    
    int lower_letters[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    int upper_letters[]={'A', 'B', 'C', 'D', 'E','F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    
    for (int e=0; e<strlen(bob); e++)
    {
        for (int g=0; g<26; g++)
        {
            if ((bob[e])==(lower_letters[g]))
            {
                printf("do not enter something that is not a number \n");
                return 1;
            }
        }
    }
    

    int total = atoi(argv[1]);
    
    if (total<0)
    {
        printf("do not input something that is a negative value\n");
        return 1;
    }

    
    string word1 =get_string("plaintext: ");
    printf("ciphertext: ");

    
    for (int i =0; i<strlen(word1); i++)
    {
        if ((word1[i]==' ') || (word1[i]==',') || (word1[i]=='.') || (word1[i]=='!'))
            {
                printf("%c",word1[i]);
            }
        
        for (int a =0; a<26; a++)
        {
            if ((word1[i]==lower_letters[a]) && (a+total>26))
            {
                printf("%c",lower_letters[(a+total)-26]);
            }
            
            if (word1[i]==lower_letters[a])
            {
                printf("%c",lower_letters[a+total]);
            }
            
            
            if ((word1[i]==upper_letters[a]) && (a+total>26))
            {
                printf("%c",upper_letters[(a+total)-26]);
            }
            
            if (word1[i]==upper_letters[a])
            {
                printf("%c",upper_letters[a+total]);
            }
            
        }
        
    }
    printf("\n");
}

我可以帮忙解决“无效的 ASCII 文本”错误吗?

           if ((word1[i]==lower_letters[a]) && (a+total>26))
           {
               printf("%c",lower_letters[(a+total)-26]);
           }
           
           if (word1[i]==lower_letters[a])
           {
               printf("%c",lower_letters[a+total]);
           }

虽然您考虑了大于 26 的 a+total 的值,但您错误地允许第二个 if 主体也执行这些值,它没有考虑到这一点。此外,减法 -26 不适用于值 52 或更高的值。

你可以把上面的代码换成

            if (word1[i]==lower_letters[a])
                putchar(lower_letters[(a+total)%26]);

——当然upper_letters也是一样。