2016-04-14 18 views
0

男私はそれを持っていると思った!私はVigenereの問題に取り組んできましたが、近づいてきましたが、チェックするとこのエラーが発生し続けます。キーがループバックしなければならない問題があるようです。思考?ここでCS50 Vigenere - 出力が正しくない

は誤りである:あなたのプログラムと

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

#define FALSE 0 
#define TRUE 1 

int main(int argc, string argv[]) 
{ 
    string key = argv[1]; 

    if (argc!= 2) 
    { 
     printf("please provide only one perameter \n"); 

     // stop the program 
     return 1; 
    } 

    // iterate over the key to make sure its all alpha 
    int i,j; 
    for (i = 0, j = strlen(key); i < j; i++) 
    { 
     if (!isalpha(key[i])) 
     { 
      printf("please use only alphanumeric values \n"); 
      return 1; 
     } 
    } 

    // now we have a key, "key" from the ONE perameter that is all alpha 

    string message = GetString(); 
    int k = 0; 
    int keyindex; 

    for (i = 0, j = strlen(message); i < j; i++, k++) 
    { 
     if (isalpha(message[i])) 
     { 
      keyindex = k % strlen(argv[1]); 
      // covering the upper case letters 
      if (isupper(message[i])) 
      { 
       // covering the upper case letters with upper case key letters 

       if isupper(key[i]) 
       { 
        // print cipher according to two upper case 
        int cipher = ((message[i] - 65 + key[keyindex] - 65) % 26) 
         + 65; 
        printf("%c", cipher); 
       } 
       else 
       { 
        // print according to upper case message lower case key 
        int cipher = ((message[i] - 65 + key[keyindex] - 97) % 26) 
         + 65; 
        printf("%c", cipher); 
       } 
      } 
      // this is for the non upper case letters 
      if (islower(message[i])) 
      { 
       if isupper(key[i]) 
       { 
        // print cipher according to lower case message and 
        // upper case key letter 
        int cipher = ((message[i] - 97 + key[keyindex] - 65) % 26) 
         + 97; 
        printf("%c", cipher); 
       } 
       else 
       { 
        // print according to lower case message and lower case key 
        int cipher = ((message[i] - 97 + key[keyindex] - 97) % 26) 
         + 97; 
        printf("%c", cipher); 
       } 
      } 

     } 
     // non alpha symbols 
     else 
     { 
      printf("%c", message[i]); 
     } 
    } 

    // end program after iterating 
    printf("\n"); 

} 

答えて

0

問題:コンパイルからそれを維持する必要があり

1)Sytaxエラー:

:) vigenere.c exists 

:) vigenere.c compiles 

:) encrypts "a" as "a" using "a" as keyword :(encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword \ expected output, but not "xoqmj, yfz gflkp!\n" 

:(encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaQAun\n" 

:(encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword \ expected output, but not "CAQAON\n" 

:) handles lack of argv[1] 

:) handles argc > 2 

:) rejects "Hax0r2" as keyword 

、ここでは私のコードです

if isupper(key[i]) - >if (isupper(key[i]))

これらは2つありますので、両方を修正してください。そこk

int k = 0; 
... 
for (i = 0, j = strlen(message); i < j; i++, k++) 
{ 
    if (isalpha(message[i])) 
    { 
     keyindex = k % strlen(argv[1]); 

2の増分

2)は、このアプローチを、いずれかのこれまで文字kをインクリメントまたはすべての手紙kをインクリメントすることでした。 >if isupper(key[keyindex])

二つがあります -

if isupper(key[i]):今、我々はそれを定義したことをkeyindexを使用してください)

int k = 0; 
... 
for (i = 0, j = strlen(message); i < j; i++) 
{ 
    if (isalpha(message[i])) 
    { 
     keyindex = k++ % strlen(argv[1]); 

3:この問題の設計者は、その代わりに、私たちが行う必要がある手紙を選びましたこれらの両方を修正するようにしてください。

これらの変更やスタイルのクリーンアップの少しを適用すると、我々が得る:

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

int main(int argc, string argv[]) 
{ 
    if (argc != 2) 
    { 
     printf("please provide only one parameter \n"); 

     return 1; // stop the program 
    } 

    string key = argv[1]; 

    // iterate over the key to make sure it's all alpha 

    for (int i = 0, j = strlen(key); i < j; i++) 
    { 
     if (!isalpha(key[i])) 
     { 
      printf("please use only alphanumeric values \n"); 

      return 1; 
     } 
    } 

    // now we have a key, "key" from the ONE parameter that is all alpha 

    string message = GetString(); 

    for (int i = 0, k = 0, j = strlen(message); i < j; i++) 
    { 
     if (isalpha(message[i])) 
     { 
      int keyindex = k++ % strlen(key); 

      if (isupper(message[i])) // covering the upper case letters 
      { 
       if (isupper(key[keyindex])) 
       { 
        // print cipher according to both upper case 
        int cipher = ((message[i] - 'A' + key[keyindex] - 'A') % 26) + 'A'; 
        printf("%c", cipher); 
       } 
       else 
       { 
        // print cipher according to upper case message and lower case key 
        int cipher = ((message[i] - 'A' + key[keyindex] - 'a') % 26) + 'A'; 
        printf("%c", cipher); 
       } 
      } 
      else // this is for the non upper case letters 
      { 
       if (isupper(key[keyindex])) 
       { 
        // print cipher according to lower case message and upper case key letter 
        int cipher = ((message[i] - 'a' + key[keyindex] - 'A') % 26) + 'a'; 
        printf("%c", cipher); 
       } 
       else 
       { 
        // print cipher according to both lower case 
        int cipher = ((message[i] - 'a' + key[keyindex] - 'a') % 26) + 'a'; 
        printf("%c", cipher); 
       } 
      } 

     } 
     else // non alpha symbols 
     { 
      printf("%c", message[i]); 
     } 
    } 

    printf("\n"); // end program after iterating 

} 

あなたのコードは、マイナーな変更を組み合わせることができ、ロジックの多くを複製 - 複製されたロジックは、ハードを見つけるための1つの方法ですエラーがコードに入り込む

+0

ありがとうございました!これは大きな違いになります。コードから一歩後退するのはとても難しいことです。私はあなたの洞察力を心に前進させていきます:) – Benjee

関連する問題