首页 > 解决方案 > 无法找出为什么我的程序没有做它应该做的事情

问题描述

作为我目前正在参加的 CS50 哈佛编程课程的一部分,我正在做一个名为“Vigenere”的练习。

我的程序做的一切都是正确的(它逐行进行并且满足了预期的行为),除非我收到以b.

一旦它到达输入字符串的末尾,它就不会循环回到数组中的第一个字符,但是如果输入不是以 ab 开头的,它就会像它应该的那样工作。

一直在谷歌搜索,调试,但就是想不通。也尝试过很多其他不同的方式,但我就是无法让它发挥作用。

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

    int main(int argc, char *argv[])
    {
        if (argc != 2)
        {
            printf("Error.\n");
            return (1);
        }
        string input = argv[1];

        for (int i = 0; input[i] != '\0'; i++)
        {
            if (!isalpha(input[i]))
            {
                printf("Error.\n");
                return (1);
            }
        }
        string plaintext = get_string("plaintext: ");
        string cipher = argv[1];
        printf("ciphertext: ");
        int i = 0;
        int j = 0;
        int code = 0;

        while (plaintext[i] != '\0')
        {
            if (cipher[j] >= 97)
            {
                cipher[j] = cipher[j] - 97;
            }
            if (isupper(cipher[j]))
            {
               cipher[j] = cipher[j] - 65;
            }
            if (islower(plaintext[i]))
            {
                printf("%c", 'a' + (plaintext[i] - 'a' + cipher[j]) % 26);
                j++;
            }
            if (isupper(plaintext[i]))
            {
                printf("%c", 'A' + (plaintext[i] - 'A' + cipher[j]) % 26);
                j++;
            }
            if (plaintext[i] == ' ')
            {
                printf("%c", plaintext[i]);
            }
            if (!isalpha(plaintext[i]) && !isspace(plaintext[i]))
            {
                printf("%c", plaintext[i]);
            }
            if (cipher[j] == '\0' && plaintext[i] != ' ')
            {
                j = 0;
            }
            i++;
        }
        printf("\n");
        return (0);
    }

如上所述,每当我的命令行输入以 ab 开头时,程序就无法按预期工作。当输入不是 b 时不会发生。

标签: ccs50vigenere

解决方案


以下建议的代码:

  1. 不使用非便携式库cs50
  2. 正确输出错误信息到stderr
  3. 执行所需的功能
  4. 干净地编译

现在,建议的代码:

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

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf( stderr, "USAGE: %s cypherKey\n", argv[0] );
        exit( EXIT_FAILURE );
    }
    // implied else, user entered correct number of parameters

    char * cipher = argv[1];

    for (int i = 0; cipher[i] != '\0'; i++)
    {
        if (!isalpha( cipher[i]))
        {
            fprintf( stderr, "key must be all alphabetic characters\n");
            exit( EXIT_FAILURE );
        }
    }
    // implied else, key all alphabetic characters

    char plaintext[1024];
    puts( "Please enter the plain text" );
    if( !fgets( plaintext, sizeof(plaintext), stdin ) )
    {
        perror( "fgets to input plain text failed" );
        exit( EXIT_FAILURE );
    }

    //implied else, fgets successful

    // remove possible trailing newline
    plaintext[ strcspn( plaintext, "\n" ) ] = '\0';

    printf("ciphertext: ");


    for( int j = 0, i = 0; plaintext[i]; i++ )
    {
        if (cipher[j] >= 97)
        {
            cipher[j] = (char)(cipher[j] - 97);
        }

        if (isupper(cipher[j]))
        {
           cipher[j] = (char)(cipher[j] - 65);
        }

        if (islower(plaintext[i]))
        {
            printf("%c", 'a' + (plaintext[i] - 'a' + cipher[j]) % 26);
            j++;
        }

        if (isupper(plaintext[i]))
        {
            printf("%c", 'A' + (plaintext[i] - 'A' + cipher[j]) % 26);
            j++;
        }

        if (plaintext[i] == ' ')
        {
            printf("%c", plaintext[i]);
        }

        if (!isalpha(plaintext[i]) && !isspace(plaintext[i]))
        {
            printf("%c", plaintext[i]);
        }

        if (cipher[j] == '\0' && plaintext[i] != ' ')
        {
            j = 0;
        }
    }

    printf("\n");
    return (0);
}

以下是该程序的几个典型运行

./untitled 是可执行文件

./untitled
USAGE: ./untitled cypherKey

./untitled abcd
Please enter the plain text
This is a plain text message
ciphertext: Tikv it c slbkq tfzw mfuvahg

./untitled bcda
Please enter the plain text
This is a plain text message
ciphertext: Ujls ju d qndjp wfzw ngvtcjf

推荐阅读