Цезарь CS50, кажется, работает, но при проверке подходит - PullRequest
1 голос
/ 13 апреля 2020

Я работал над этим кодом ceasar в c для курса cs50 в течение пары дней, и всякий раз, когда я тестирую его, он, кажется, работает хорошо, и когда я запускаю check50 cs50 / problem / 2020 / x / caesar в cs50 ide говорит, что ожидаемые результаты не верны, но при взгляде на результаты они выглядят одинаково

результаты ниже:

check50 cs50 / problem / 2020 / х / цезарь :) цезарь. c существует. Журнал проверяет, что цезарь. c существует ... :) Цезарь. c компилируется. Журнал запускается clang caesar. c -o caesar -std = c11 -ggdb -lm -lcs50 ... :( шифрует «a» как «b», используя 1 в качестве ожидаемого ключа «ciphertext: b ...», not » ciphertext: b "Журнал работает ./caesar 1 ... отправка ввода a ... проверка вывода" 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"...

Expected Output:
ciphertext: iadxp, emk tqxxa!
Actual Output:
ciphertext: iadxp, emk tqxxa!
:) handles lack of key
Log
running ./caesar...
checking that program exited with status 1...
:) handles non-numeric key
Log
running ./caesar 2x...
checking that program exited with status 1...
:) handles too many arguments
Log
running ./caesar 1 2...
checking that program exited with status 1...
<<

код здесь:

    #include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, string argv[])
{
   if (argc != 2 )
   {
       printf(" Enter positive integer key\n");
       return 1;
   }
   else if(argc == 2 )
   {
       //convert key to int 
       int KEY = atoi(argv[1]);
       //determin string length
       int m = strlen(argv[1]);

        //string validity
       bool validity= true;
       for (int i = 0; i < m; i++)
       {
           if (KEY <= 0 || !isdigit(argv[1][i]))
           {
                printf("Enter positive digit\n");
                validity = false;
                return 1;
            }
       }


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

пожалуйста, помогите мне свести меня с ума и не знаете, почему это происходит

...