2

从昨天开始,我一直在研究这个问题,经过一番努力,我成功地加密了消息。但是,我的输出缺少空格。

据我了解,发生这种情况的原因是因为我正在使用 isalpha()、isupper() 和 islower() 命令,因此忽略了原始输入中的空格。

有人可以帮助我如何保留原始空格和标点符号吗?

下面是我的代码——它远非优雅,任何关于风格的评论也将不胜感激!

(此外,虽然有很多关于 Caesar Cipher 的问题,但没有一个可以解决这个问题。由于这是我的第一周编程,我很难理解其中的语法。)

我的算法中有一个明显的错误,如果给定某些参数,它会导致它输出错误的值。例如 ak 为 13,在第 13 个字母(我认为是 m)之后输入任何东西都会输出一些非常奇怪的东西。我会修改这个并很快回来!在那之前,对我的代码持保留态度!

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


int main(int argc, string argv[])

{
    if (argc != 2)
    {
        printf("Please enter a valid number of arguments! \n");
        return 1;
    }

    string num = argv[1];
    int k = atoi(num);

        if (k < 0)
            {
                printf("Please enter a valid number! \n");
                return 1;
            }

    printf("Please type the message which needs to be encrypted: ");
    string p = GetString();

        for (int i = 0, n = strlen(p); i < n; i++)
        {
            int oldletter = p[i];
            int result1 = (oldletter + k);
            int result2 = (oldletter - 65 + k);
            int result3 = (result2) % 26;
            int result4 = (oldletter - 97 + k);
            int result5 = (result4) % 26;

                if (isalpha(p[i]) && isupper(p[i]) && k < 26)
                {
                    printf("%c", result1);
                }

                if (isalpha(p[i]) && isupper(p[i]) && k >= 26) 
                {
                    int result7 = (result3 + oldletter); 
                    printf("%c", result7);
                }


                if (isalpha(p[i]) && islower(p[i]) && k < 26)
                {
                    printf("%c", result1);
                }

                if (isalpha(p[i]) && islower(p[i]) && k >= 26)
                {
                    int result8 = (result5 + oldletter); 
                    printf("%c", result8);
                }

        }
        printf("\n");
}

更正的代码,工作正常:SPOILERS AHEAD

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

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Please enter a valid number of arguments! \n");
        return 1;
    }

    string num = argv[1];
    int k = atoi(num);

    if (k < 0)
        {
            printf("Please enter a valid number! \n");
            return 1;
        }

    printf("Message: ");
    string p = GetString();

        for (int i = 0, n = strlen(p); i < n; i++)
        {
            int oldletter = p[i];
            int result1 = (oldletter - 65 + k);
            int result2 = (result1) % 26;
            int result3 = (oldletter - 97 + k);
            int result4 = (result3) % 26;


                if (isalpha(p[i]) && isupper(p[i])) 
                {
                    int result5 = (result2 + 65); 
                    printf("%c", result5);
                }


                else if (isalpha(p[i]) && islower(p[i]))
                {
                    int result6 = (result4 + 97); 
                    printf("%c", result6);
                }

                else 
                {
                    printf("%c", p[i]);
                }

        }
        printf("\n");
}
4

2 回答 2

2

当人们实现这一点时,我看到的一个常见陷阱是直接使用 ascii 值。考虑制作一个字母数组,你可以只获取当前字母在其中的位置,然后确定修改后的字母应该是什么。

想象一下,使用 ascii 解决方案为此添加一个 '%' 字符,你最终会得到大量特殊的 if。在这种情况下,您可以选择忽略空格/等,如果您愿意,我个人会将它们添加到字母数组中,这样密文就不会显示空格(给出提示)。

于 2014-06-04T13:02:45.547 回答
1

您可能应该将if's as else if's 链接在一起,如果前一个条件在您的情况下已经成立,则无需评估 if 条件。这也将允许执行最终的 else 情况,isalpha就像false在有空格的情况下一样。

只需将 if 条件更改为:

if (isalpha(p[i]) && isupper(p[i]) && k < 26)
{
    printf("%c", result1);
}

else if (isalpha(p[i]) && isupper(p[i]) && k >= 26) 
{
    int result7 = (result3 + oldletter); 
    printf("%c", result7);
}


else if (isalpha(p[i]) && islower(p[i]) && k < 26)
{
    printf("%c", result1);
}

else if (isalpha(p[i]) && islower(p[i]) && k >= 26)
{
    int result8 = (result5 + oldletter); 
    printf("%c", result8);
}

else
{
    printf("%c", p[i]);
}

我想指出您的逻辑非常复杂,您还应该选择比result*您当前使用的变量更好的名称,在编程可读性和可维护性方面非常重要。由于您在那里编写的小程序,您可以轻松地完成作业而无需考虑它们,但这是一个好习惯。

我还参加了该课程(具有先前的 C 经验)并上传了我的最终解决方案,供您在完成后进行比较/改进。只是一个警告,我使用了一个函数,不确定在这个问题集之前是否已经解释过,但至少应该在不久之后解释一下。这是我的解决方案:http: //pastebin.com/vJqPY6Ne

于 2014-06-04T12:48:19.843 回答