Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- const int rows = 8;
- const int columns = 16;
- const int H[rows][columns] = {
- {0,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0},
- {1,0,0,0,1,1,1,1, 0,1,0,0,0,0,0,0},
- {0,0,0,1,0,1,1,1, 0,0,1,0,0,0,0,0},
- {1,0,1,1,1,0,1,1, 0,0,0,1,0,0,0,0},
- {0,1,1,0,0,0,1,1, 0,0,0,0,1,0,0,0},
- {1,1,0,1,1,1,0,1, 0,0,0,0,0,1,0,0},
- {1,1,1,0,0,1,0,1, 0,0,0,0,0,0,1,0},
- {0,0,1,0,1,1,1,0, 0,0,0,0,0,0,0,1}
- };
- const int G[rows][columns] = {
- {1,0,0,0,0,0,0,0, 0,1,0,1,0,1,1,0},
- {0,1,0,0,0,0,0,0, 1,0,0,0,1,1,1,0},
- {0,0,1,0,0,0,0,0, 1,0,0,1,1,0,1,1},
- {0,0,0,1,0,0,0,0, 1,0,1,1,0,1,0,0},
- {0,0,0,0,1,0,0,0, 1,1,0,1,0,1,0,1},
- {0,0,0,0,0,1,0,0, 1,1,1,0,0,1,1,1},
- {0,0,0,0,0,0,1,0, 1,1,1,1,1,0,0,1},
- {0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,0}
- };
- std::vector<int> Generate(const int G[8][16],std::vector<int> msg){
- std::vector<int> code = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- for(int i = 0; i < columns; i++)
- {
- for ( int j = 0 ; j < rows ; j ++ )
- {
- code[i] += G[j][i] * msg[j];
- }
- code[i] %= 2;
- }
- return code;
- }
- std::vector<int> Check(std::vector<int> code){
- std::vector<int> result = {0,0,0,0,0,0,0,0};
- for(int i = 0; i < rows; i++){
- //code[i]=0;
- for(int j = 0; j < columns; j++ ){
- result[i] += H[i][j] * code[j];
- }
- result[i] %= 2;
- }
- return result;
- }
- void Change(std::vector<int> &code, int i) {
- code[i]=(code[i])?0:1;
- }
- void Repair(std::vector<int> &msg){
- std::vector<int> error = Check(msg);
- for(int i = 0; i < columns; i++){
- bool isSame = false;
- for(int j = 0; j < rows; j++)
- {
- if(error[j] == H[j][i]){
- isSame = true;
- }
- else {
- isSame = false;
- break;
- }
- }
- if(isSame)
- Change(msg, i);
- }
- for(int i = 0; i < columns; i++){
- for(int j = 0; j < columns; j++){
- for(int k = 0; k < rows; k++){
- if((H[k][i] ^ H[k][j]) != error[k]) {
- break;
- }
- if (k == rows - 1) {
- Change(msg, i);
- Change(msg, j);
- }
- }
- }
- }
- }
- void stringToInt(std::vector<int> &message, std::string text){
- for(int i = 0; i < text.size(); i++) {
- message.push_back((int)text[i] - 48);
- }
- }
- void coding(std::vector<int> &msg) {
- msg = Generate(G, msg);
- for(int i = 0; i < msg.size(); i++){
- std::cout<< msg[i];
- }
- }
- void decoding(std::vector<int> &msg) {
- std::vector<int> error = Check(msg);
- std::cout<<"\nWektor błędów:\n";
- for(int i = 0; i < error.size(); i++){
- std::cout << error[i];
- }
- std::cout << "\nOdkodowana wiadomość:\n";
- Repair(msg);
- error = Check(msg);
- for(int i = 0; i < error.size(); i++){
- if(error[i]) {
- std::cout << "Wiadomość ma więcej niż 2 błędy!\n";
- break;
- }
- }
- for(int i = 0; i < msg.size(); i++){
- std::cout << msg[i];
- }
- std::cout << '\n';
- }
- int main()
- {
- int choice;
- do {
- std::cout << "1. Kodowanie\n"
- "2. Dekodowanie\n"
- "3. Zakończ\n";
- std::cin >> choice;
- if(choice == 1) {
- std::ifstream inputFile;
- std::ofstream outputFile;
- inputFile.open("message.txt");
- outputFile.open("coded_message.txt");
- std::string inputData;
- while(getline(inputFile, inputData))
- {
- if(inputData.size() == 8)
- {
- std::vector<int> message;
- stringToInt(message, inputData);
- coding(message);
- for(int i = 0; i < message.size(); i++)
- {
- outputFile << message[i];
- }
- outputFile << '\n';
- std::cout << '\n';
- }
- }
- inputFile.close();
- outputFile.close();
- } else if (choice == 2) {
- std::ifstream inputFile;
- std::ofstream outputFile;
- inputFile.open("coded_message.txt");
- outputFile.open("decoded_message.txt");
- std::string inputData;
- while(getline(inputFile, inputData))
- {
- if(inputData.size() == columns)
- {
- std::vector<int> message;
- stringToInt(message, inputData);
- decoding(message);
- for(int i = 0; i < 8; i++)
- {
- outputFile << message[i];
- }
- outputFile << '\n';
- }
- }
- inputFile.close();
- outputFile.close();
- } else if (choice == 3)
- break;
- else
- std::cout << "Błędny wybór!\n";
- } while (true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement