Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5.  
  6. const int rows = 8;
  7. const int columns = 16;
  8.  
  9. const int H[rows][columns] = {
  10.         {0,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0},
  11.         {1,0,0,0,1,1,1,1, 0,1,0,0,0,0,0,0},
  12.         {0,0,0,1,0,1,1,1, 0,0,1,0,0,0,0,0},
  13.         {1,0,1,1,1,0,1,1, 0,0,0,1,0,0,0,0},
  14.         {0,1,1,0,0,0,1,1, 0,0,0,0,1,0,0,0},
  15.         {1,1,0,1,1,1,0,1, 0,0,0,0,0,1,0,0},
  16.         {1,1,1,0,0,1,0,1, 0,0,0,0,0,0,1,0},
  17.         {0,0,1,0,1,1,1,0, 0,0,0,0,0,0,0,1}
  18. };
  19.  
  20. const int G[rows][columns] = {
  21.         {1,0,0,0,0,0,0,0, 0,1,0,1,0,1,1,0},
  22.         {0,1,0,0,0,0,0,0, 1,0,0,0,1,1,1,0},
  23.         {0,0,1,0,0,0,0,0, 1,0,0,1,1,0,1,1},
  24.         {0,0,0,1,0,0,0,0, 1,0,1,1,0,1,0,0},
  25.         {0,0,0,0,1,0,0,0, 1,1,0,1,0,1,0,1},
  26.         {0,0,0,0,0,1,0,0, 1,1,1,0,0,1,1,1},
  27.         {0,0,0,0,0,0,1,0, 1,1,1,1,1,0,0,1},
  28.         {0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,0}
  29. };
  30.  
  31. std::vector<int> Generate(const int G[8][16],std::vector<int> msg){
  32.     std::vector<int> code = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  33.     for(int i = 0; i < columns; i++)
  34.     {
  35.         for ( int j = 0 ; j < rows ; j ++ )
  36.         {
  37.             code[i] += G[j][i] * msg[j];
  38.         }
  39.         code[i] %= 2;
  40.     }
  41.     return  code;
  42. }
  43.  
  44. std::vector<int> Check(std::vector<int> code){
  45.     std::vector<int> result = {0,0,0,0,0,0,0,0};
  46.     for(int i = 0; i < rows; i++){
  47.         //code[i]=0;
  48.         for(int j = 0; j < columns; j++ ){
  49.             result[i] += H[i][j] * code[j];
  50.         }
  51.         result[i] %= 2;
  52.     }
  53.     return  result;
  54. }
  55.  
  56. void Change(std::vector<int> &code, int i) {
  57.     code[i]=(code[i])?0:1;
  58.  
  59. }
  60.  
  61. void Repair(std::vector<int> &msg){
  62.     std::vector<int> error = Check(msg);
  63.     for(int i = 0; i < columns; i++){
  64.         bool isSame = false;
  65.         for(int j = 0; j < rows; j++)
  66.         {
  67.             if(error[j] == H[j][i]){
  68.                 isSame = true;
  69.             }
  70.             else {
  71.                 isSame = false;
  72.                 break;
  73.             }
  74.         }
  75.         if(isSame)
  76.             Change(msg, i);
  77.  
  78.     }
  79.     for(int i = 0; i < columns; i++){
  80.         for(int j = 0; j < columns; j++){
  81.             for(int k = 0; k < rows; k++){
  82.                 if((H[k][i] ^ H[k][j]) != error[k]) {
  83.                     break;
  84.                 }
  85.                 if (k == rows - 1) {
  86.                     Change(msg, i);
  87.                     Change(msg, j);
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94. void stringToInt(std::vector<int> &message, std::string text){
  95.     for(int i = 0; i < text.size(); i++) {
  96.         message.push_back((int)text[i] - 48);
  97.     }
  98. }
  99.  
  100. void coding(std::vector<int> &msg) {
  101.     msg = Generate(G, msg);
  102.     for(int i = 0; i < msg.size(); i++){
  103.         std::cout<< msg[i];
  104.     }
  105. }
  106.  
  107. void decoding(std::vector<int> &msg) {
  108.  
  109.  
  110.     std::vector<int> error = Check(msg);
  111.     std::cout<<"\nWektor błędów:\n";
  112.     for(int i = 0; i < error.size(); i++){
  113.         std::cout << error[i];
  114.     }
  115.     std::cout << "\nOdkodowana wiadomość:\n";
  116.     Repair(msg);
  117.     error = Check(msg);
  118.     for(int i = 0; i < error.size(); i++){
  119.         if(error[i]) {
  120.             std::cout << "Wiadomość ma więcej niż 2 błędy!\n";
  121.             break;
  122.         }
  123.     }
  124.     for(int i = 0; i < msg.size(); i++){
  125.         std::cout << msg[i];
  126.     }
  127.     std::cout << '\n';
  128. }
  129.  
  130. int main()
  131. {
  132.     int choice;
  133.     do {
  134.         std::cout << "1. Kodowanie\n"
  135.                      "2. Dekodowanie\n"
  136.                      "3. Zakończ\n";
  137.  
  138.         std::cin >> choice;
  139.         if(choice == 1) {
  140.             std::ifstream inputFile;
  141.             std::ofstream outputFile;
  142.  
  143.             inputFile.open("message.txt");
  144.             outputFile.open("coded_message.txt");
  145.             std::string inputData;
  146.  
  147.             while(getline(inputFile, inputData))
  148.             {
  149.                 if(inputData.size() == 8)
  150.                 {
  151.                     std::vector<int> message;
  152.                     stringToInt(message, inputData);
  153.                     coding(message);
  154.                     for(int i = 0; i < message.size(); i++)
  155.                     {
  156.                         outputFile << message[i];
  157.                     }
  158.                     outputFile << '\n';
  159.                     std::cout << '\n';
  160.                 }
  161.             }
  162.  
  163.             inputFile.close();
  164.             outputFile.close();
  165.         } else if (choice == 2) {
  166.  
  167.             std::ifstream inputFile;
  168.             std::ofstream outputFile;
  169.  
  170.             inputFile.open("coded_message.txt");
  171.             outputFile.open("decoded_message.txt");
  172.             std::string inputData;
  173.  
  174.             while(getline(inputFile, inputData))
  175.             {
  176.                 if(inputData.size() == columns)
  177.                 {
  178.                     std::vector<int> message;
  179.                     stringToInt(message, inputData);
  180.                     decoding(message);
  181.                     for(int i = 0; i < 8; i++)
  182.                     {
  183.                         outputFile << message[i];
  184.                     }
  185.                     outputFile << '\n';
  186.                 }
  187.             }
  188.             inputFile.close();
  189.             outputFile.close();
  190.  
  191.         } else if (choice == 3)
  192.             break;
  193.  
  194.         else
  195.             std::cout << "Błędny wybór!\n";
  196.  
  197.     } while (true);
  198.  
  199.     return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement