【问题标题】:CS50 - Pset2 - substitution help meCS50 - Pset2 - 替换帮助我
【发布时间】:2021-09-02 15:06:10
【问题描述】:

我看不到我的 pset2 替换代码缺少什么。当我使用 check50 测试程序时,它会返回以下结果:

:) substitution.c exists

:) substitution.c compiles

:( encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
    expected "ciphertext: Z\...", not "ciphertext: Z\..."

:( encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
    expected "ciphertext: z\...", not "ciphertext: z\..."

:( encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key
    expected "ciphertext: NJ...", not "ciphertext: NJ..."

:( encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key
    expected "ciphertext: Ke...", not "ciphertext: Ke..."

:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key

:) encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key

:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key

:( encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
expected "ciphertext: Rq...", not "ciphertext: Rq..."
:) handles lack of key

:) handles invalid key length

:) handles invalid characters in key

:) handles duplicate characters in key

:) handles multiple duplicate characters in key

我的代码是:

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

int main(int argc, string argv[])
{
    // VALIDATE THE KEY
    // for argc
    if (argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }
    
    // var n for nubers of argv
    int n = 26;
    // for alphabit
    for (int i = 0; i < n; i++)
    {
        if (isalpha(argv[1][i]) == 0)
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
    }
    // for length of  argv
    if (n != 26)
    {
        printf("Key must contain 26 characters.\n");
        return 1;
    }
    // repeated charcters
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            // step over for the character
            if (i == j)
            {
                j += 1;
            }
            // error message for repeted character
            if (argv[1][i] == argv[1][j])
            {
                printf("Key must not contain repeated characters.\n");
                return 1;
            }
        }
    }
    // GET PLAINTEXT
    string plaintext = get_string("plaintext: ");
    int npi = strlen(plaintext);
    // ENCIPHER AND PRINT CIPHERTEXT
    // var for ciphertext
    char ci[npi];
    // print ciphertext
    printf("ciphertext: ");
    // encipher
    for (int i = 0; i < npi;)
    {
        for (int j = 0; j < n; j++)
        {
            // for not alphabits
            if (isalpha(plaintext[i]) == 0)
            {
                printf("%c", plaintext[i]);
                i++;
                j = 26;
            }
            // for upper case
            if (isupper(plaintext[i]) && ('A' + j) == plaintext[i])
            {
                ci[i] = toupper(argv[1][j]);
                printf("%c", ci[i]);
                i++;
                j = 0;
            }
            // for lower case
            else if (islower(plaintext[i]) && ('a' + j) == plaintext[i])
            {
                ci[i] = tolower(argv[1][j]);
                printf("%c", ci[i]);
                i++;
                j = 0;
            }
        }
    }
    printf("\n");
    return 0;
}

【问题讨论】:

  • 请编辑您的帖子以准确说明您的输出中的问题所在。
  • 我们对 CS50 psets 不熟悉,请添加程序应该做什么的描述。
  • 旁白:if (i == j) { j += 1; } 你应该在哪里做if (i == j) { continue; } 否则你可能索引超出范围。
  • @ryyker 我怀疑 OP 对问题的了解比 check50 程序告诉我们的更多。
  • 也许你应该在CS50发帖

标签: c cs50


【解决方案1】:

程序输出不可打印的字符,这就是 check50 输出令人困惑的原因,它不是“打印”它们。

在命令行试试这个并输入a作为明文查看结果:

./substitution ZYXWVUTSRQPONMLKJIHGFEDCBA | cat --show-nonprinting

程序的根本缺陷是更改循环变量值循环(ij)。在明文“a”示例中,i 大于 npi。把它写在纸上。

【讨论】:

    猜你喜欢
    • 2020-08-09
    • 1970-01-01
    • 2022-07-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多