Vigenere cs50 在缺少第二个参数时不会抱怨

Posted

技术标签:

【中文标题】Vigenere cs50 在缺少第二个参数时不会抱怨【英文标题】:Vigenere cs50 doesn't complain when lacks second arg 【发布时间】:2016-05-01 01:28:01 【问题描述】:

我不明白为什么它不起作用。当有 3 个或更多参数时它会抱怨,但当只有一个 Vigenere 参数时不会抱怨。我看过其他有同样问题的人,他们说这可以解决......不知道我在这里错过了什么。当我运行./vigenere 时,出现分段错误。它与./vigenere bard 之类的 2 个参数一起正常工作,并在给出额外参数(例如./vigenere bard dfads)时抱怨。

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




int main(int argc,string argv[])
  

string sKeyWord = argv[1];                   
int iKeyLength = strlen(sKeyWord);           
int iKey[iKeyLength];                        
string sPlainText = "";
int counter = 0;
int iAccept = 0;
do

    if(argc != 2) // <-----this should work whats wrong?
        
            printf("Invalid argument! Please enter program name and         keyword.\n");
            return 1;
        
    else if(argv[1])
        
        for(int i = 0; i < iKeyLength; i++)
            
                if (!isalpha(argv[1][i]))
                
                    printf("Invalid entry, please use letters   only.\n");
                    return 1;
                
                else
                
                    iAccept = 1;
                
            
            
while(iAccept == 0);


for(int i = 0; i < iKeyLength; i++)           
    
        iKey[i] = toupper(sKeyWord[i]) - 65;     
    

sPlainText = GetString();                    
int iPlainText = strlen(sPlainText);        


for(int j = 0; j < iPlainText; j++)
    
        if(!isalpha(sPlainText[j]))
            
                printf("%c",sPlainText[j]);
                counter++;
            
        if(islower(sPlainText[j]))
            
                printf("%c",((((sPlainText[j] - 97) + iKey[(j - counter)%iKeyLength])%26)+ 97));
            
        if(isupper(sPlainText[j]))
            
                printf("%c",((((sPlainText[j] - 65) + iKey[(j - counter)%iKeyLength])%26)+ 65));
            

    
printf("\n");

return 0;

【问题讨论】:

您应该尽早调查。例如在string sKeyWord = argv[1]; 之前 卫生署!感谢bluepixy 高五我的救星!改为字符串 skeyWord = "";然后在 do while 循环结束时将 argv[1] 分配给它。 为什么要检查 do-while 循环中的参数? (提示:我认为您不需要将此代码置于循环中。) 为什么?因为我是菜鸟哈哈。会调查的。在移动了一些东西之后,我打破了加密过程。今晚会尝试自己调试。 @luser droog 好的,我将我的 OP 更新为我现在拥有的代码。除非在关键字中使用“z”,否则一切正常。它以错误的方式转移明文。似乎所有其他字母都可以正常工作。这是我的支票50 【参考方案1】:

我会像这样重写程序的顶部参数处理部分。

int main(int argc, char **argv)
  
    if (argc != 2)
    
        fprintf(stderr, "Usage: %s key\n", argv[0]);
        return 1;
    
    char *sKeyWord = argv[1];                   
    int iKeyLength = strlen(sKeyWord);           
    int iKey[iKeyLength];                        

    for (int i = 0; i < iKeyLength; i++)
    
        if (!isalpha(sKeyword[i]))
        
            fprintf(stderr, "%s: Invalid character '%c' in key; please use letters only.\n",
                    argv[0], sKeyword[i]);
            return 1;
        
        iKey[i] = toupper(sKeyWord[i]) - 'A';
    

    …your code to read the text to be enciphered and encipher it, etc…

关键点是在尝试对它进行任何操作之前检查是否存在argv[1]。我消除了do … while (…); 循环,因为该参数不会在第二次迭代中改变。这允许消除iAccept 变量。请注意,错误是在标准错误上报告的,而不是标准输出。另请注意,消息前面是程序名称 (argv[0])。 “使用”消息通常是报告问题的最佳方式;这是对那些运行程序的人的简单提醒。还要注意,字母检查的错误消息报告了错误的字符;这有助于人们了解程序认为错误的地方。

这或多或少是 cmets 建议应该做的。

我没有查看加密代码;也可能存在未诊断的问题。不过,有许多关于 SO 的相关问题可以为您提供任何此类问题的答案。

【讨论】:

以上是关于Vigenere cs50 在缺少第二个参数时不会抱怨的主要内容,如果未能解决你的问题,请参考以下文章

关于 CS50 pset2 vigenere

CS50 Vigenere - 输出不正确

CS50 Vigenere - 奇怪的图案

Vigenere.c CS50 浮点异常(核心转储)

为啥 Vigenere 失败 (CS50x)?

cs50 的 Vigenere 密码程序错误