Created
April 12, 2011 16:37
-
-
Save resure/915863 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <locale.h> | |
#define STRLEN 255 | |
int decrypt(int c, int n, bool crypt = false) | |
{ | |
// Функцию можно вызывать как decrypt( c, n ), где n — ключ, а c - символ. | |
// А можно и так: decrypt( c, n, 1 ), тогда она будет шифровать символы. | |
if (crypt) | |
c = c - n; // Шифровка | |
else | |
c = c + n; // Расшифровка | |
if (c > 90) c = c - 26; // Циклический сдвиг вправо | |
if (c < 65) c = c + 26; // Циклический сдвиг влево | |
return c; | |
} | |
int main() | |
{ | |
setlocale(0, "russian"); | |
int n, i = 0; | |
char str[10][STRLEN]; | |
printf("Введите последовательность строк.\n"); | |
printf("Для прекращения ввода, нажмите enter, не набирая строки.\n"); | |
do | |
{ | |
fgets(str[i], sizeof(str[i]), stdin); | |
// Можно использовать и gets(), но эта fgets лучше и безопаснее | |
str[i][strlen(str[i]) - 1] = '\0'; | |
// Удаляем последний символ (это перенос строки) | |
i++; // Увеличиваем счетчик строк | |
} while (strlen(str[i - 1]) != 0); | |
// Идем дальше, когда последняя введенная строка пустая | |
int nm = i; // Запоминаем количество строк | |
printf("Введите ключ для (де)шифрования: "); | |
scanf("%d", &n); // Читаем ключ | |
printf("\n"); | |
for (int j = 0; j < nm; j++) | |
{ | |
for (int i = 0; i < strlen(str[j]); i++) | |
{ | |
printf("%c", decrypt(str[j][i], n)); | |
// Расшифровка. Можно продемонстрировать работу программы, набрав | |
// decrypt ( decrypt( str[j][i], n, 1 ), n ) — | |
// — так она сперва зашифрует, а потом расшифрует текст | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment