Если быть короче, то надо алгоритм заполнения матрицы(спиралькой) начинать с верхенего правого угла и идти к центру против часовой стрелки. Мой код делает наоборот, из центра в правый верхний угол. В матрицу записывается фраза из файла, так же код может шифровать фразу из файла и расшировать шифрованное, и все это записываются в отдельные файлы. Фраза из исходного файла Say "Hello" To My Little Friend! .
Код:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
void Open(fstream& file, string path) {
try {
file.open(path);
if (!file.is_open()) {
throw "Ошибка открытия файла";
}
}
catch (const exception& ex) {
ex.what();
exit;
}
}
void Read(vector<char>& text, string path) {
fstream file;
Open(file, path);
while (!file.eof()) {
char buff;
file >> buff;
if (buff != ' ') {
text.push_back(buff);
}
}
text.pop_back();
file.close();
}
void Out(vector<char>& text) {
for (int i = 0; i < text.size(); i++) {
cout << text[i];
} cout << endl;
}
int main() {
srand(time(NULL));
setlocale(LC_ALL, "Russian");
while (true) {
cout << "1 - Шифровка фразы (из файла file.txt в файл encode.txt)\n2 - Расшифровка шифрованной фразы (из файла encode.txt в файл decode.txt)\nЧтобы выйти нажмите Esc\n";
if (_getch() == 27) break;
int choise; cout << "Ввод: "; cin >> choise; cout << endl;
if (choise == 1) {
vector<char>text;
Read(text, "file.txt");
cout << "Ваш изначальный текст: "; Out(text); cout << endl;
int matrix_size = 0;
if (sqrt(text.size()) == (int)sqrt(text.size())) matrix_size = sqrt(text.size());
else matrix_size = (int)sqrt(text.size()) + 1;
for (int i = text.size(); i < pow(matrix_size, 2); i++) {
text.push_back(char('а' + rand() % ('я' - 'а')));
}
cout << "Текст с добавкой букв: "; Out(text); cout << endl;
int index = 0;
if (matrix_size % 2 == 0) index = matrix_size / 2 - 1;
else index = matrix_size / 2;
char* matrix; matrix = new char[matrix_size * matrix_size];
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
*(matrix + i * matrix_size + j) = '0';
}
}
cout << endl << endl;
int i = index; int j = index; int step = 0; int counter = 1;
while (step < text.size()) {
for (int l = 0; l < counter; l++)
if (step < text.size()) { *(matrix + i * matrix_size + j) = text[step]; step++; j++; }
for (int l = 0; l < counter; l++)
if (step < text.size()) { *(matrix + i * matrix_size + j) = text[step]; step++; i++; }
counter++;
for (int l = 0; l < counter; l++)
if (step < text.size()) { *(matrix + i * matrix_size + j) = text[step]; step++; j--; }
for (int l = 0; l < counter; l++)
if (step < text.size()) { *(matrix + i * matrix_size + j) = text[step]; step++; i--; }
counter++;
}
cout << "Матрица: \t";
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
cout << *(matrix + i * matrix_size + j) << ' ' << ' ';
} cout << endl << "\t\t";
}
cout << endl << endl;
fstream encode; Open(encode, "encode.txt");
cout << "Зашифрованный текст: ";
for (int j = 0; j < matrix_size; j++) {
for (int i = 0; i < matrix_size; i++) {
cout << *(matrix + i * matrix_size + j);
encode << *(matrix + i * matrix_size + j);
}
} cout << endl << endl;
encode.close();
}
else if (choise == 2) {
vector<char>code;
Read(code, "encode.txt");
cout << endl << "Шифрованный текст: "; Out(code); cout << endl;
int matrix_size = 0;
if (sqrt(code.size()) == (int)sqrt(code.size())) matrix_size = sqrt(code.size());
else { cout << "\n\t===Ошибка! Неверный шифр===\n"; break; }
int index = 0;
if (matrix_size % 2 == 0) index = matrix_size / 2 - 1;
else index = matrix_size / 2;
char* matrix; matrix = new char[matrix_size * matrix_size]; int step = 0;
for (int j = 0; j < matrix_size; j++)
for (int i = 0; i < matrix_size; i++) {
*(matrix + i * matrix_size + j) = code[step]; step++;
}
fstream decode; Open(decode, "decode.txt");
int i = index; int j = index; step = 0; int counter = 1;
while (step < code.size()) {
for (int l = 0; l < counter; l++)
if (step < code.size()) { decode << *(matrix + i * matrix_size + j); step++; j++; }
for (int l = 0; l < counter; l++)
if (step < code.size()) { decode << *(matrix + i * matrix_size + j); step++; i++; }
counter++;
for (int l = 0; l < counter; l++)
if (step < code.size()) { decode << *(matrix + i * matrix_size + j); step++; j--; }
for (int l = 0; l < counter; l++)
if (step < code.size()) { decode << *(matrix + i * matrix_size + j); step++; i--; }
counter++;
}
decode.close();
vector<char>text;
Read(text, "decode.txt");
cout << "Расшифрованный текст: "; Out(text); cout << endl;
}
else {
cout << "Неверный ввод!" << endl;
}
}
return 0;
}