【问题标题】:My Cs50 Caesar cipher program works but it won't pass the check50 tests我的 Cs50 凯撒密码程序有效,但无法通过 check50 测试
【发布时间】:2020-03-25 15:44:49
【问题描述】:

当我自己测试时,我的程序可以正常工作,并且预期输出与 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;
}

这是我从 Check50 收到的输出

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

如您所见,预期输出和实际输出是相同的。然而,测试仍然失败。

如果有人能告诉我该怎么做,我将不胜感激。我仍在学习,因此对我的代码相关和与我的问题无关的任何批评也将不胜感激。

【问题讨论】:

  • if (plaintext[i] &gt;= 'A' &amp;&amp; plaintext[i] &lt;= 'Z') 最好写成if ( isupper( ( unsigned char ) plaintext[ i ] ) )

标签: c cs50 caesar-cipher


【解决方案1】:

预期输出和实际输出仅在人类看来看起来相同。它们是不同的,并且人眼无法检测到差异。

它正在从纯文本打印终止的空字节,这是不可打印的,所以你看不到它。问题出在for (int i = 0; i &lt;= j; i++)

【讨论】:

  • 啊,非常感谢。现在大部分测试都通过了。但是,我仍然收到此错误::( 在等待程序退出时处理非数字键超时
  • 知道这意味着什么。我试过输入非数字键,它按预期工作。
  • “2x”是否按预期工作?使用atoi 为时过早(在程序中)。您还不知道命令行参数是否全是数字。想想isdigit()
  • 啊,它没有。非常感谢。你是救命稻草。
猜你喜欢
  • 2014-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多