2016-11-30 4 views
0

Я создал следующую программу, чтобы сделать шифр Цезаря и Вигнера. Я получил шифр Цезаря, чтобы работать правильно, однако я не могу заставить Vigenere работать правильно.Проблема с языком-языком C

Я хочу, чтобы мои инструкции if «ломали» все различные числа с интервалом 5. Однако всякий раз, когда я запускаю программу, мой шифр Viegenere выводит тот же самый вход. Я считаю, что это потому, что я ошибаюсь в своих операторах if, но я не уверен, что это такое.

Начало шифра Viegenere комментируется в коде как // Vigenere Cipher-- ключевого слова «яблоко»

#include <stdio.h> 


int main(){ 
int i=0; 
//setting the individual slot number for the array-- later used in the while loop 
char guy[100]; 
printf("Enter the plain text:"); 
fgets(guy,100,stdin); //takes user's input-- such as "abc" and puts it into its respective slot in the array guy[10] r-right? 

while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string 


    if ((guy[i]) >= 'A' && (guy[i]<= 'Z')){ //moves capital letter values up 1 
     if (guy[i]=='Z'){ 
      guy[i]=guy[i]-25; 
     } 
     else { 
     guy[i]=guy[i]+1; //makes the current "slot" number go up 1 value. Example: a = 97 + 1 -> b = 98 
     } 
     } 
    if ((guy[i]) >= 'a' && (guy[i]) <= 'z'){// moves lower case letter values up 1 
     if (guy[i]=='z'){ 
      guy[i]=guy[i]-25; 
     } 
     else{ 
     guy[i]=guy[i]+1; 
     } 
    } 
    i++; //moves the array's interval up to the next "slot" 

} 
printf("Encrypted text is: %s\n",guy); 

//Vigenere Cipher-- keyword is "apple" 
//a = 1 value shift 
//p = 16 value shift 
//p = 16 value shift 
//l = 17 value shift 
//e = 5 value shift 

printf("Enter the plain text: "); 
fgets(guy,100,stdin);//takes user's input 

while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string 

    if (i%5==0 || i==0){ //checks to see which character it is in the string, for instance the numbers 0,5,10,15,20 should all be added by 1 
    guy[i] = guy[i]+1; 
    } 

    if ((i-1)%5==0 || i==1){ //all numbers that are second in the key word 'apple', such as 1,6,11,16 
     guy[i]=guy[i]+16; 
    } 
    if ((i-2)%5==0 || i==2){// all numbers that are third to the key word 'apple' , such as 2,7,12,17,22 
     guy[i]=guy[i]+16; 
    } 
    if((i-3)%5==0 || i==3){// all numbers that are fourth to the key word 'apple', such as 3,8,13,18 
     guy[i]=guy[i]+17; 
    } 
    if((i-4)%5==0 || i==4){// all numbers that are fifth in the key word 'apple', such as 4,9,14,19 
     guy[i]=guy[i]+5; 
    } 
    else { 
    i++; 
    } 
    } 
printf("Encrypted text is: %s\n",guy); 
} 
+0

Я не могу воспроизвести вашу ошибку. Но я из практики с stdio, так что, возможно, у вас есть ошибка где-то, что вызывает неопределенное поведение. Я предлагаю вам жесткий код с четким текстом и посмотреть, не исчезла ли проблема. – Beta

+0

l = 12 сдвиг значения. и используйте '%' – BLUEPIXY

+0

. Вы должны прочитать немного больше об операторе modulo. '((i-3)% 5 == 0 || i == 3)' такое же, как '(i% 5 == 3 || i == 3)', и это то же самое, что и '(i% 5 == 3) '. – Gerhardh

ответ

4

Перед повторным шифрованием данных - вы должны повторно инициализировать значение " я»переменная

#include <stdio.h> 


    int main(){ 
    int i = 0; 
    //setting the individual slot number for the array-- later used in the while loop 
    char guy[100]; 
    printf("Enter the plain text:"); 
    fgets(guy, 100, stdin); //takes user's input-- such as "abc" and puts it into its respective slot in the array guy[10] r-right? 

    while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string 


     if ((guy[i]) >= 'A' && (guy[i] <= 'Z')){ //moves capital letter values up 1 
      if (guy[i] == 'Z'){ 
       guy[i] = guy[i] - 25; 
      } 
      else { 
       guy[i] = guy[i] + 1; //makes the current "slot" number go up 1 value. Example: a = 97 + 1 -> b = 98 
      } 
     } 
     if ((guy[i]) >= 'a' && (guy[i]) <= 'z'){// moves lower case letter values up 1 
      if (guy[i] == 'z'){ 
       guy[i] = guy[i] - 25; 
      } 
      else{ 
       guy[i] = guy[i] + 1; 
      } 
     } 
     i++; //moves the array's interval up to the next "slot" 

    } 
    printf("Encrypted text is: %s\n", guy); 

    //Vigenere Cipher-- keyword is "apple" 
    //a = 1 value shift 
    //p = 16 value shift 
    //p = 16 value shift 
    //l = 17 value shift 
    //e = 5 value shift 
    printf("The value of i = %d\n", &i); 
    i = 0; 
    printf("Enter the plain text: "); 
    fgets(guy, 100, stdin);//takes user's input 

    while (guy[i] != '\0'){ //while loop that runs until it reaches the end of the string 

     if (i % 5 == 0 || i == 0){ //checks to see which character it is in the string, for instance the numbers 0,5,10,15,20 should all be added by 1 
      guy[i] = guy[i] + 1; 
     } 

     if ((i - 1) % 5 == 0 || i == 1){ //all numbers that are second in the key word 'apple', such as 1,6,11,16 
      guy[i] = guy[i] + 16; 
     } 
     if ((i - 2) % 5 == 0 || i == 2){// all numbers that are third to the key word 'apple' , such as 2,7,12,17,22 
      guy[i] = guy[i] + 16; 
     } 
     if ((i - 3) % 5 == 0 || i == 3){// all numbers that are fourth to the key word 'apple', such as 3,8,13,18 
      guy[i] = guy[i] + 17; 
     } 
     if ((i - 4) % 5 == 0 || i == 4){// all numbers that are fifth in the key word 'apple', such as 4,9,14,19 
      guy[i] = guy[i] + 5; 
     } 
     else { 
      i++; 
     } 
    } 
    printf("Encrypted text is: %s\n", guy); 
    getchar(); 
} 

Выход: -

Введите текст: яблочный Зашифрованный текст: bqqmf

Значение i = 1900200 - Значение i перед повторной инициализацией.

Введите текст: яблочный Зашифрованный текст: BCC}

+0

он не должен выходить за пределы z. –