Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <stdio.h>
- #include <conio.h>
- #include <Windows.h>
- #define N 7 // длина слова на выходе
- #define M 4 // длина полинома и длина слова на входе
- using namespace std;
- int p74[4] = { 1, 0, 1, 1 };
- int deshifmass[N];
- int mas[N];
- int masmain[M];
- int massive_for_division[N];
- void Coding()
- {
- int i = 0;
- int flag = 0;
- while (i < N)
- {
- while (mas[i] == 0)
- {
- if (N - i < M) //проверяем условие, что делимое не короче делителя
- {
- flag = 1;
- break;
- }
- i++;
- if (N - i < M)//тоже самое
- {
- flag = 1;
- break;
- }
- }
- if (flag == 1)
- break;
- int j;
- j = 0;
- for (i; j < M; i++)
- {
- mas[i] = (mas[i] + p74[j]) % 2; //деление - это сложение по mod 2(остаток от деления на 2)(взаимоисключающее или)
- j++;
- }
- i = 0;
- }
- cout << endl << "Остаток: ";
- for (int j = 0; j < N; ++j)
- {
- cout << mas[j];
- }
- for (int i = 0; i < M; i++)
- {
- mas[i] = masmain[i];
- }
- }
- void Delenie()
- {
- int i, j;
- for (int k = 0; k < N; k++) {
- //ищем старший разряд единицы
- for (i = 0; i < N; i++) {
- if (massive_for_division[i] == 1) {
- break;
- }
- }
- if ((N - i - 1) < (M - 1))
- break;// делить нельзя, степень остатка N-i-1 меньше степени полинома M-1
- cout << endl;
- for (j = 0; j < M; j++) {
- massive_for_division[i + j] = (massive_for_division[i + j] + p74[j]) % 2;
- }
- }
- cout << "\nMassive_for_division: ";
- for (int j = 0; j < N; j++) {
- cout << massive_for_division[j];
- }
- }
- void RightShift() //сдвиг вправо
- {
- int temp;
- temp = deshifmass[N - 1];
- for (int i = N - 1; i > 0; i--)
- {
- deshifmass[i] = deshifmass[i - 1];
- }
- deshifmass[0] = temp;
- cout << endl << "Правый сдвиг: ";
- for (int i = 0; i < N; i++)
- {
- cout << deshifmass[i];
- }
- }
- void LeftShift()//сдвиг влево
- {
- int temp;
- temp = deshifmass[0];
- for (int i = 0; i < N - 1; i++) {
- deshifmass[i] = deshifmass[i + 1];
- }
- deshifmass[N - 1] = temp;
- cout << endl << "Левый сдвиг: ";
- for (int i = 0; i < N; i++) {
- cout << deshifmass[i];
- }
- }
- void out()
- {
- cout << endl << "Шифрованное слово: ";
- for (int i = 0; i < N; i++)
- {
- cout << mas[i];
- }
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- cout << "Кодирование информации" << endl;
- cout << "Введи слово (4 бита): ";
- for (int i = 0; i < M; i++)
- {
- cin >> mas[i];
- }
- for (int i = 0; i < M; i++)
- {
- masmain[i] = mas[i];
- }
- int j = 4;
- while (j < N) //добавляем контрольные биты (3 штуки)
- {
- mas[j] = 0;
- j++;
- }
- cout << "Массив после добавления контрольных битов: ";
- for (int i = 0; i < N; i++)
- cout << mas[i];
- Coding();
- out();
- int number_of_shift = 0;
- cout << endl << endl << "Декодирование информации" << endl;
- cout << "Введите слово: ";
- for (int i = 0; i < N; i++)
- {
- cin >> deshifmass[i];
- }
- int number_of_sdvig = 0;
- for (int k = 0; k < N; k++) {
- for (int p = 0; p < N; p++)
- {
- massive_for_division[p] = deshifmass[p];
- }
- Delenie();
- int ost = 0;
- for (int j = 0; j < N; j++)
- {
- ost += massive_for_division[j];
- }
- cout << " Остаток: " << ost;
- if (ost > 1) {
- LeftShift();
- number_of_sdvig++;
- continue;
- }
- if (ost == 1) {
- deshifmass[N - 1] = (deshifmass[N - 1] + 1) % 2;
- for (int j = 0; j <= number_of_sdvig - 1; j++)
- RightShift(); //если вес остатка равен 1, то мы суммируем по mod 2 сдвинутое число и остаток
- break;
- }
- if (ost == 0)
- cout << "\nОшибок нет!";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement