Моя программа шифрования Cs50 Caesar работает, но она не пройдет тесты check50 - PullRequest
0 голосов
/ 25 марта 2020

Моя программа работает, когда я тестирую ее самостоятельно, и ожидаемые результаты совпадают с фактическими выходами в тесте check50. Тем не менее, большинство тестов все еще не пройдено.

Вот мой код:

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

string get_plain(void);

int main(int argc, string argv[])
{
//checks to see if only one argument is inputted.
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
//Checks to see if the argument is an integer.
    int key = atoi(argv[1]);
    if (key == 0)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }

//Grabs plaintext input off user
    string plaintext = get_plain();
    int j = strlen(plaintext);
//creates an array the size of the user string input.
    char ciphar[j];
    printf("ciphertext: ");
    for (int i = 0; i <= j; i++)
    {
//Checks to see if the input is uppercase.
        if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')
        {
//Checks if the input and the key added do not exceed ascii limits on uppercase letters.
            if (plaintext[i] + (key % 26) > 90)
            {
                ciphar[i] = plaintext[i] + (key % 26) - 26;
                printf("%c", ciphar[i]);
            }
            else
            {
                ciphar[i] = plaintext[i] + (key % 26);
                printf("%c", ciphar[i]);
            }
        }
//Checks to see if the input is uppercase.
        else if (plaintext[i] >= 'a' && plaintext[i] <= 'z')
        {
//Checks if the input and the key added do not exceed ascii limits on lowercase letters.
            if (plaintext[i] + (key % 26) > 122)
            {
                ciphar[i] = plaintext[i] + (key % 26) - 26;
                printf("%c", ciphar[i]);
            }
            else
            {
                ciphar[i] = plaintext[i] + (key % 26);
                printf("%c", ciphar[i]);
            }
        }
        else
        {
            printf("%c", plaintext[i]);
        }
    }
    printf("\n");
    return 0;



}
//Grabs plaintext input off user
string get_plain(void)
{
    string plaintext = get_string("Plaintext:  ");
    return plaintext;
}

Вот вывод, который я получаю из проверки50

Results for cs50/problems/2020/x/caesar generated by check50 v3.0.10
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
    expected "ciphertext: b\...", not "ciphertext: b\..."
:( encrypts "barfoo" as "yxocll" using 23 as key
    expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
    expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
    expected "ciphertext: Fe...", not "ciphertext: Fe..."
:(  encrypts "barfoo" as "onesbb" using 65 as key
    expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
    expected "ciphertext: ia...", not "ciphertext: ia..."
:) handles lack of key
:( handles non-numeric key
    timed out while waiting for program to exit
:) handles too many arguments

Как вы можно увидеть, ожидаемый результат и фактический результат совпадают. Тем не менее, тест по-прежнему не проходит.

Если кто-нибудь может дать мне представление о том, что делать, я был бы очень благодарен. Я все еще учусь, поэтому любая критика моего кода, относящаяся к моему вопросу и не относящаяся к нему, также будет принята с благодарностью.

1 Ответ

1 голос
/ 25 марта 2020

Ожидаемый результат и только фактический результат выглядят одинаковыми для людей. Они разные, и разница не обнаруживается человеческим глазом.

Он печатает завершающий нулевой байт из обычного текста, который нельзя распечатать, поэтому вы не можете его увидеть. Проблема лежит здесь for (int i = 0; i <= j; i++).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...