2016-08-02 73 views
-3

我在运行以下代码时遇到了问题,这些代码是为vigenere cipher.even设计的.even彻底彻底后,我无法调试问题。它显示错误:由server.please帮助杀死。Vigenere Cipher

/** 
* 
* vigenere.c 
* 
* Abhishek kumar 
* encrypts entered string using vigenere cipher 
* */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <cs50.h> 
#include <ctype.h> 
#include <string.h> 

int main(int argc, string argv[]) 
{ 
    if (argc != 2) 
    { 
     printf("Usage: /home/cs50/pset2/vigenere <keyword>"); 
     return 1; 
    } 

    if (argc == 2) 
    { string key = argv[1]; 
     for(int k = 0,l = strlen(key);k < l; k++) 
     { 
      if(!isalpha(key[k])) 
      { 
       printf("Keyword must only contain letters A-Z and a-z"); 
       exit(1); 
      } 

     } 


     string txt = GetString(); 
     int i = 0,j = 0,c = 0; 
     int n = strlen(txt); 
     int m = strlen(key); 
     while(i < n) 
     { 
      if (isupper(txt[i])) 
      { 
       if(isupper(key[j])) 
       { 
        c = ((((int) txt[i] - 65 + (int) key[j] -65)%26) + 65); 
        printf("%c", (char) c); 
        i++; 
        j++; 
       } 
       if(islower(key[j])) 
       { 
        c = ((((int) txt[i] - 65 + (int) key[j] -97)%26) + 65); 
        printf("%c", (char) c); 
        i++; 
        j++; 
       } 

      } 
      else if (islower(txt[i])) 
      { 
       if(isupper(key[j])) 
       { 
        c = ((((int) txt[i] - 97 + (int) key[j] -65)%26) + 97); 
        printf("%c", (char) c); 
        i++; 
       } 
       if(islower(key[j])) 
       { 
        c = ((((int) txt[i] - 97 + (int) key[j] -97)%26) + 97); 
        printf("%c", (char) c); 
        j++; 
       } 


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

      } 
      if (j == m-1) 
      { 
       j = 0; 
      } 
     } 




    } 
} 

下面是一些失败的测试用例。

:) vigenere.c exists 
:) vigenere.c compiles 
:(encrypts "a" as "a" using "a" as keyword 
    \ killed by server 
:(encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword 
    \ killed by server 
:(encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword 
    \ expected output, but not "CGSFpp" 
:(encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword 
    \ expected output, but not "CASFPO" 
:) handles lack of argv[1] 
:) handles argc > 2 
:) rejects "Hax0r2" as keyword 
+0

请添加一些示例输入和所需的输出。 – merlin2011

+2

请提问 – dustinroepsch

+0

已编辑,现在结帐.. –

回答

0

islower(txt[i])部分,你不能增加在所有情况下ij。在不增加i的地方,即键和文本的第一个字符都是小写字母的地方,最后会出现无限循环。

isupper(txt[i])部分,您在isupper(key[j])部分增加ij,然后你进入islower(key[j])一部分,因为你使用if而不是else if

对于两个以上的,更改if(islower(key[j]))else if(islower(key[j])),并且每个内if块之后移动j++printf。至于i,将while更改为for并将i作为其中的一部分。

当您查看是否应该重置j时,表示您已关闭1. m-1key的有效索引,因此您不想重置。当j == m

此外,用他们代表的实际字符替换ASCII码,以便更清楚你在做什么。演员也不需要。

for (i=0; i < n; i++) 
    { 
     if (isupper(txt[i])) 
     { 
      if(isupper(key[j])) 
      { 
       c = (((txt[i] - 'A' + key[j] -'A')%26) + 'A'); 
      } 
      else if(islower(key[j])) 
      { 
       c = (((txt[i] - 'A' + key[j] -'a')%26) + 'A'); 
      } 
      printf("%c", c); 
      j++; 
     } 
     else if (islower(txt[i])) 
     { 
      if(isupper(key[j])) 
      { 
       c = (((txt[i] - 'a' + key[j] -'A')%26) + 'a'); 
      } 
      else if(islower(key[j])) 
      { 
       c = (((txt[i] - 'a' + key[j] -'a')%26) + 'a'); 
      } 
      printf("%c", c); 
      j++; 
     } 
     else 
     { 
      printf("%c",txt[i]); 
     } 
     if (j == m) 
     { 
      j = 0; 
     } 
    } 
+0

thanx一吨人。 –