2016-04-07 8 views
0

いいえ。ですから、私はVigenereのサイファーを作る必要があります。テキストとキーが両方とも小文字の大文字である場合、コードは正常にコンパイルされます。しかし、テキストとキーが大文字と異なる場合、コードは機能しません。その後、何も印刷されません。たとえば、キーがaaAAの場合です。そしてテキストはaBcDです。結果はaDです。誰かが私にヒントをくれますか? :)CS50 Pset2。 Vigenere。上のテキストは下のキーに、逆の場合は上のテキストになります。

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

int main (int argc, string argv[]) 
{ 
    string key = argv [1]; //argv [1] is the key. 0 is compile program 
{  
    if (argc != 2) 
    { 
     printf ("Please give one key: "); //if there are more or less then 2 argc, then have to try again 
    } 

for (int j = 0, n = strlen (key); j < n; j++) 
    if (!isalpha (key [j]))  
    { 
     printf ("Please give a key in alphabetic characters: "); 
     //key must be alphabetic. For loop to check every character of the key. 
     return 1; 
    } 
} 

string text = GetString(); //Get secret message from user 

int j = 0; 
for (int i = 0, n = strlen (text); i < n; i++) 
{ 
    if (isupper (text [i])) 
    { 
     if (isupper (key [j])) 
     { 
     /*Minus 65 to make count till 26 from text and key. Use modulo to wrap around key. And modulo to wrap around alphabet. 
     Plus 65 to go to correct ASCII character. */ 
     int u = ((((text [i] - 65) + (key [j % strlen (key)] - 65)) % 26) + 65); 
     printf ("%c", u); 
     } 
    } 
    else if (islower (text [i])) 
    { 
     if (islower (key[j])) 
     { 
     int l = ((((text [i] - 97) + (key [j % strlen (key)] - 97)) % 26) + 97); 
     printf ("%c", l); 
     } 
    } 
    else if (islower (text [i])) 
    { 
     if (isupper (key[j])) 
     { 
     int lu = ((((text [i] - 97) + (key [j % strlen (key)] - 65)) % 26) + 97); 
     printf ("%c", lu); 
     } 
    } 
    else if (isupper (text [i])) 
    { 
     if (islower (key[j])) 
     { 
     int ul = ((((text [i] - 65 + (key [j % strlen (key)] - 97)) % 26) + 65); 
     printf ("%c", ul); 
     } 
    } 
    else 
     { 
     // When character is non alphabetic print it in its original form. 
     printf ("%c", text [i]); 
     } 
    j++; 
} 
{ 
    printf ("\n"); 
    return 0; 
} 
} 

答えて

0

問題は、あなたの場合、他の-場合、... ELSE-IF文です。理由は、if isupper(text[i])がtrueを返し、if isupper(key[j])がfalseを返し、else ifステートメントを評価しないためです。あなたがこれを行う必要があり

if(isupper(text[i])){ 
    if(isupper(key[j])){ // Both upper 
     //do stuff 
    } 
    else if(islower(key[j])){ //Here key is lower and text is upper 
     //do stuff 
    } 
} 
else if (islower(text[i])){ 
    if (islower(key[j])){ //Both lower 
     //do stuff 
    } 
    else if(isupper(key[j])){ //Key upper and text lower 
     //do stuff 
    } 
} 
else{//It's not alpha 
    //do stuff 
} 

/***************NEW********************/ 
j = j%strlen(key); //I suggest using this at the end of the loop to avoid key[j] to go out of it's bounds 
// j = (j==strlen(key)) ? 0 : j; //Another alternative 

はまた、私は性格がそんなに

+0

さて、おかげアルファでない場合は、jを増やすべきではないと思います。上の下のキーが正しく機能しています。しかし、キーの周りを包み込むことはありません。このコードは初めてうまく動作します。しかし、テキストが長くなると、正しくラップされません。それを機能させるように見えることはできません。助言がありますか? – Kim

+0

@Kim申し訳ありませんが、問題はキーの 'if'ステートメントにあります。あなたは 'key [j%strlen(key)]'を使うべきです。そうしないと、その範囲外に出ます。私は答えを更新する –

+1

アハ、私は理解する。助けてくれてありがとう。最後にそれを終えた:) – Kim

関連する問題