2016-04-17 38 views
0

私はpset2のためにシーザー暗号の暗号化装置と解読装置を2つ作成しました。私の暗号化装置は動作し、それはcaesar.cと呼ばれますが、私のdecryptor(decryptor.c)は動作せず、なぜ私はその理由を理解できませんか?ここに私のコードです。デクリプタが動作しない

Caesar.c

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


int main(int argc, string argv[]) { 
    //checks for enough args 
    if (argc >= 2) { 
     //converts the second arg to an integer 
     int k = atoi(argv[1]); 
     string stringToBeEncrypted = GetString(); 
     printf("%s\n", encryptor(stringToBeEncrypted, k)); 
    } else { 
     printf("Please add a key argument\n"); 
     return 1; 
    } 
} 

Decryptor.c

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

    // almost same as caesar 


    int main(void) { 
     const string stringToBeEncrypted = GetString(); 
      // string stringToBeEncrypted = GetString(); 
      //checks all possible decryptions 
      for(int k=1;k<26;k++) { 
       printf("%s\n", encryptor(stringToBeEncrypted, k)); 
      } 
    } 

encryptorFunction.c

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

string encryptor(string encryptString, int key) { 
    //loops through every character 
    string encryptStringCopy = encryptString; 
    for (int i = 0, n = strlen(encryptStringCopy); i < n; i++) { 
     //converts the specific char to its ascii value 
     int ascii = (int) encryptStringCopy[i]; 
     //makes key 31 is the same as key 5 
     key = key % 26; 
     //checks if it is an alphabetical letter 
     if (isalpha(encryptStringCopy[i])) { 
      //checks if lowercase 
      if (islower(encryptStringCopy[i])) { 
       //handles the corner case of being z since 26 % 26 is not 26 it's 0 
       if ((ascii-96+key) == 26) { 
        ascii = 122; 
       }else { 
        //takes the lowercase alphabetical numbers to the 1-26 alphabet then checks mod 26 to cycle back and adds it back. 
        ascii = ((ascii-96+key) % 26) + 96; 
       } 
       //else part is for uppercase case 
      }else { 
       //same logic as above 
       if ((ascii-64+key) == 26) { 
        ascii = 90; 

       }else { 
        ascii = ((ascii-64+key) % 26) + 64; 
       } 
      } 
     } 
     //converts the ascii back to a char 
     char newChar = (char) ascii; 
     //sets the new char into the string 
     encryptStringCopy[i] = newChar; 
    } 
    return encryptStringCopy; 
} 
+0

あなたは本当に '暗号化()'関数を呼び出すコードファイル 'Decryptor.c'を意図しました、 'Caesar.c'で呼ばれる同じ関数ですか? –

+0

はい、それがそれを解読する方法です。 –

+0

どうしたのが分かりますか? –

答えて

2

あなたencryptor()機能は、代わりに入力文字列(引数encryptString)を変更します。その結果、同じ入力に対して繰り返し呼び出すと、予期しない結果が発生します。

この関数は、むしろそれが渡されたものを修正するよりも、返すように新しい文字列を作成する必要があります。

+0

私はそれに気付きましたが、実際に修正しようとしましたが、うまく機能しませんでした。 –

+0

例を挙げてください。私は作ってみましたが、うまくいきませんでした。 –

+0

何を試しましたか?何が起こったのですか? 「うまくいきませんでした」というのは、おそらくあなたほど曖昧です。 – duskwuff