首页 > 解决方案 > CS50 Caesar 程序正在运行,但 check50 说它不是

问题描述

我创建了这个程序,但我在 CS50 上遇到错误,表明我没有正确执行任何操作。

要求如下:

在名为 caesar 的目录中的名为 caesar.c 的文件中实现您的程序。

你的程序必须接受一个命令行参数,一个非负整数。为了讨论,我们称它为 k。

如果您的程序在没有任何命令行参数或多个命令行参数的情况下执行,您的程序应该打印您选择的错误消息(使用 printf)并从 main 返回值 1(这往往表示错误) 立即地。

如果命令行参数的任何字符不是十进制数字,您的程序应该打印消息 Usage: ./caesar key 并从 main 返回值 1。

不要假设 k 会小于或等于 26。您的程序应该适用于 k 小于 2^31 - 26 的所有非负整数值。换句话说,您无需担心您的程序是否最终如果用户选择的 k 值太大或几乎太大而无法放入 int 中,则会中断。(回想一下 int 可能会溢出。)但是,即使 k 大于 26,程序输入中的字母字符也应该在程序输出中保持字母字符。例如,如果 k 是 27,

根据http://www.asciichart.com/[asciichart.com],即使 [ 在 ASCII 中距离 A 27 个位置,A 也不应该变成 [ ;A 应该变成 B,因为 B 离 A 有 27 个位置,前提是你从 Z 绕到 A。

您的程序必须输出明文:(不带换行符),然后提示用户输入明文字符串(使用 get_string)。

您的程序必须输出密文:(不带换行符)后跟明文对应的密文,明文中的每个字母字符“旋转”k 个位置;非字母字符应原样输出。

你的程序必须保持大小写:大写字母,虽然轮换,但必须保持大写字母;小写字母虽然旋转,但必须保持小写字母。

输出密文后,应打印换行符。然后你的程序应该通过从 main 返回 0 来退出。

我的代码:

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

int main(int argc, string argv[])
{
    //check if k inputed
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    //value k is the number after ./caesar
    int k = atoi(argv[1]) % 26;
    int x = 0;
    int s = strlen(argv[1]);
    //check if k is a positive integer
    if (k < 0)
    {
        printf("Usage: .caesar key\n");
        return 1;
    }
    else
    {
        //check for arguments
        for (int i = 0; i < s; i++)
        {
            if (isalpha (argv[1][i]))
            {
                continue;
            }
            else if (isalnum (argv[1][i]))
            {
                x++;
            }
            else
            {
                continue;
            }
        }
        if (x != s)
        {
            printf("Usage: ./caesar key\n");
        }
        else if (x == s)
        {
            //get plaintext
            string plain_text = get_string("plaintext: ");
            printf("ciphertext: ");
            for (int y = 0; y <= strlen(plain_text); y++)
            {
                //change letters
                if (isalpha(plain_text[y]))
                {
                    char p = plain_text[y];
                    int cipher_int = p + k;
                    if (isupper(p))
                    {
                        while(cipher_int >= 90)
                        {
                            cipher_int -= 26;
                        }
                            char cipher_text = cipher_int;
                            printf("%c", cipher_text);
                    }
                    if (islower(p))
                    {
                        while(cipher_int >= 122)
                        {
                            cipher_int -= 26;
                        }
                            char cipher_text = cipher_int;
                            printf("%c", cipher_text);
                    }
                }
                else
                {
                    printf("%c", plain_text[y]);
                }
            }
            printf("\n");
        }
    }
    return 0;
}

标签: cencryptioncs50caesar-cipher

解决方案


1您对数字的检查非常麻烦,如果参数不正确,不会导致程序按要求返回。

这是一个更简单的测试:

        //check for arguments
        for (int i = 0; i < s; i++) {
            if (!isdigit((unsigned char)argv[1][i])) {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }

另请注意,当索引 == 字符串的长度时,您应该停止编码循环。因此运营商应该是<

另一个问题是使用值和isalpha()类似的函数。这些函数对于负值是未定义的(除了)。某些平台默认定义为已签名,如果用户键入非 ASCII 文本,则会产生未定义的行为。铸造论点以避免这个问题。<ctype.h>charEOFcharisalpha(plaintext[y])(unsigned char)

此外,您不应该使用硬编码的 ASCII 值,例如90and 122,使用字符常量,例如'a'and'z'以获得更好的可读性。这样做会使编码循环中的另一个错误更加明显:while(cipher_int >= 90)应该是if (cipher_int > 'A')while(cipher_int >= 122)应该是if(cipher_int > 'z')

这是修改后的版本:

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

int main(int argc, string argv[])
{
    // check for a single command line argument
    if (argc != 2) {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    char *arg = argv[1];
    if (*arg == '\0') {
        printf("caesar: key cannot be an empty string\n");
        return 1;
    }
    // check that the argument is a non negative number
    for (size_t i = 0; arg[i]; i++) {
        if (!isdigit((unsigned char)arg[i])) {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }
    // value k is the shift number after ./caesar
    int k = atoi(argv[1]) % 26;

    // get plaintext
    string plain_text = get_string("plaintext: ");
    printf("ciphertext: ");
    for (size_t i = 0; plain_text[i] != '\0'; i++) {
        unsigned char c = plain_text[i];
        // change letters
        if (islower(c)) {
            putchar('a' + ((c - 'a') + k) % 26);
        } else
        if (isupper(c)) {
            putchar('A' + ((c - 'A') + k) % 26);
        } else {
            putchar(c);
        }
    }
    printf("\n");
    return 0;
}

推荐阅读