2016-04-07 5 views
0

Okay. Also, ich muss eine Vigenere-Chiffre machen. Wenn der Text und der Schlüssel beide Großbuchstaben in beiden Kleinbuchstaben sind, wird der Code korrekt kompiliert. Aber wenn der Text und der Schlüssel von Groß-/Kleinschreibung abweichen, funktioniert der Code nicht. Dann druckt es nichts. Zum Beispiel wenn der Schlüssel: aaAA ist. Und der Text ist ABCCD. Das Ergebnis ist: aD. Kann mir bitte jemand einen Hinweis geben? :)CS50 Pset2. Vigenere. Oberer Text, um Schlüssel zu senken und umgekehrt Probleme

#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; 
} 
} 

Antwort

0

Das Problem ist in Ihrem if, else-if, else-if ... Anweisungen. Der Grund ist, weil if isupper(text[i]) Wahr zurückgibt, und if isupper(key[j]) gibt false zurück, es wird niemals die else if-Anweisungen auswerten. Sie sollten diese

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 

auch tun, ich glaube, Sie nicht j Erhöhung werden sollte, wenn das Zeichen nicht alpha ist

+0

Okay, vielen Dank. Jetzt funktioniert die obere untere Taste richtig. Aber es umschließt den Schlüssel nicht. Der Code funktioniert beim ersten Mal einwandfrei. Aber wenn der Text länger ist, wird er nicht richtig umgebrochen. kann es scheinbar nicht funktionieren lassen. Irgendwelche Vorschläge? – Kim

+0

@Kim Sorry, das Problem liegt in den 'if' Anweisungen für den Schlüssel. Sie sollten 'key [j% strlen (key)]' verwenden oder Sie würden die Grenzen überschreiten. Ich werde die Antwort –

+1

aktualisieren Ahaa, ich verstehe. Vielen Dank für deine Hilfe. Endlich fertig :) – Kim