Друзья, скажите в чем проблема, задали написать задачу на шифрование, смысл в том что надо вбивать текст, он заполняется в матрицу, затем вбивать порядок строк и столбцов это шифрование, и сделать расшифровку - текст который получился в шифровании вбить в строку, затем вбить тот же порядок строк и столбцов и получить исходный текст. Шифрование работает но вот почему-то расшифровка не хочет, подскажите что-нибудь. (так же не работает проверка если можете подскажите и по ней)
Код:
// lab2.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int proverka(int *b, int block_size)
{
int a = 0;
for (int i = 0; i < block_size; i++)
{
if (b[i] > block_size)
{
a = 1;
}
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
{
a = 2;
}
}
} return a;
}
void shifr(string text, int str, int stolb, int c)
{
char **arr = new char*[str];
for (int i = 0; i < str; i++)
{
arr[i] = new char[stolb];
}
char **arr2 = new char*[str];
for (int i = 0; i < str; i++)
{
arr2[i] = new char[stolb];
}
int k = 0;
cout << endl;
for (int i = 0; i < str; i++)
{
for (int j = 0; j < stolb; j++)
{
arr[i][j] = text[k++];
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "Введите порядок строк через ПРОБЕЛ: " << endl;
int *strok = new int[str];
for (int i = 0; i < str; i++)
{
cin >> strok[i];
strok[i] -= 1;
}
if (proverka(strok, str - 1) == 1 && proverka(strok, str - 1) == 2)
{
if (proverka(strok, str - 1) == 1)
{
cout << endl << "ОШИБКА! Число больше кол-ва строк!" << endl << endl;
}
if (proverka(strok, str - 1) == 2)
{
cout << endl << "ОШИБКА! Повторяющиеся строки!" << endl << endl;
}
}
else
{
cout << "Введите порядок столбцов через ПРОБЕЛ: " << endl;
int *stl = new int[stolb];
for (int i = 0; i < stolb; i++)
{
cin >> stl[i];
stl[i] -= 1;
}
if (proverka(stl, stolb - 1) == 1 && proverka(stl, stolb - 1) == 2)
{
if (proverka(stl, stolb - 1) == 1)
{
cout << endl << "ОШИБКА! Число больше кол-ва столбцов!" << endl << endl;
}
if (proverka(stl, stolb - 1) == 2)
{
cout << endl << "ОШИБКА! Повторяющиеся столбцы!" << endl << endl;
}
}
else
{
if (c == 1)
{
for (int i = 0; i < str; i++)
{
for (int j = 0; j < stolb; j++)
{
arr2[i][j] = arr[strok[i]][stl[j]];
cout << arr2[i][j];
}
}
cout << endl << endl;
}
else
{
for (int i = 0; i < str; i++)
{
for (int j = 0 ; j < stolb; j++)
{
arr2[strok[i]][stl[j]] = arr[i][j];
cout << arr2[i][j];
}
}
cout << endl << endl;;
}
}
}
}
int main()
{
int otv;
do
{
setlocale(LC_ALL, "Russian");
int count;
cout << "1. Шифрование" << endl << "2. Расшифровка" << endl << "0. Выход" << endl;
cin >> count;
cout << endl;
if (count != 1 && count != 2 && count != 0)
{
cout << "Будьте внимательнее!" << endl << endl;
}
switch (count)
{
case 1:
{
cout << "Введите текст :" << endl;
string text;
cin.ignore(32676, '\n');
getline(cin, text);
int stolb = sqrt(text.length());
while (text.length() % stolb != 0)
{
text.append(".");
}
int str = text.length() / stolb;
shifr(text, str, stolb, 1);
break;
}
case 2:
{
cout << "Введите текст :" << endl;
string text;
cin.ignore(32676, '\n');
getline(cin, text);
int stolb = sqrt(text.length());
int str = text.length() / stolb;
if (text.length() % (str*stolb) != 0)
{
cout << endl << "Некорректный шифр!" << endl << endl;
}
else
{
shifr(text, str, stolb, 2);
}
break;
}
case 0:
{
exit(0);
}
}
} while (otv = 1);
_getch();
return 0;
}