Advertisement
Ahmed_Negm

Untitled

May 13th, 2023
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. // hamming code implementation
  6.  
  7. int parity(int k) {
  8.     int r = 0;
  9.     while ((1 << r) < k + r + 1) r++;
  10.     return r;
  11. }
  12.  
  13.  
  14. void encode(vector<int> &data, vector<int> &code) {
  15.     int k = data.size();
  16.     int n = code.size();
  17.     int r = n - k;
  18.  
  19.     for (int i = 0; i < r; i++) {
  20.         int p = 1 << i;
  21.         int cnt = 0;
  22.         int x = 0;
  23.         bool flag = true;
  24.         for (int j = p - 1; j < n; j++) {
  25.             if (p == cnt) {
  26.                 flag = !flag;
  27.                 cnt = 0;
  28.             }
  29.             if (!flag) {
  30.                 cnt++;
  31.             } else {
  32.                 if (code[j] != -1)
  33.                     x ^= code[j];
  34.                 cnt++;
  35.             }
  36.         }
  37.  
  38.         code[p - 1] = x;
  39.     }
  40.  
  41. }
  42.  
  43.  
  44. int check_error(vector<int> &code) {
  45.     int n = code.size();
  46.     int r = log2(n);
  47.     int error = 0;
  48.     for (int i = 0; i <= r; i++) {
  49.         int p = 1 << i;
  50.         int cnt = 0;
  51.         int x = 0;
  52.         bool flag = true;
  53.         for (int j = p - 1; j < n; j++) {
  54.             if (p == cnt) {
  55.                 flag = !flag;
  56.                 cnt = 0;
  57.             }
  58.             if (!flag) {
  59.                 cnt++;
  60.             } else {
  61.                 if (cnt != 0)
  62.                     x ^= code[j];
  63.                 cnt++;
  64.             }
  65.         }
  66.         if (x != code[p - 1]) {
  67.             error += p;
  68.         }
  69.     }
  70.     return error;
  71. }
  72.  
  73. void correct_error(vector<int> &code, int error) {
  74.     if (error == 0) return;
  75.     code[error - 1] ^= 1;
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. int main() {
  85.     char ch = 'y';
  86.     do {
  87.         cout << "1 - Encode\n";
  88.         cout << "2 - Check and Correct Error\n";
  89.  
  90.         cout << "Enter your choice: (1/2) : ";
  91.         int choice;
  92.         cin >> choice;
  93.  
  94.         if (choice == 1) {
  95.             cout << "Enter the number of data bits: ";
  96.             int k;
  97.             cin >> k;
  98.  
  99.             cout << "Enter the data bits: ";
  100.  
  101.             vector<int> data(k);
  102.             string s;
  103.             cin >> s;
  104.  
  105.             for (int i = 0; i < k; i++) {
  106.                 data[i] = s[i] - '0';
  107.             }
  108.  
  109.             int r = parity(k);
  110.             int n = k + r;
  111.             vector<int> code(n);
  112.  
  113.             for (int i = 0; i < r; i++) {
  114.                 code[(1 << i) - 1] = -1;
  115.             }
  116.  
  117.  
  118.             int j = k - 1;
  119.             for (int i = 0; i < n; i++) {
  120.                 if (code[i] != -1) {
  121.                     code[i] = data[j--];
  122.                 }
  123.             }
  124.             encode(data, code);
  125.             cout << "The encoded data is: ";
  126.             for (int i = 0; i < n; i++) cout << code[i] << " ";
  127.             cout << endl;
  128.         } else {
  129.  
  130.             cout << "Enter the number of code bits: ";
  131.             int n;
  132.             cin >> n;
  133.             cout << "Enter the code bits: ";
  134.             string s;
  135.             cin >> s;
  136.             vector<int> code(n);
  137.             for (int i = 0; i < n; i++) {
  138.                 code[i] = s[i] - '0';
  139.             }
  140.  
  141.             int error = check_error(code);
  142.             if (error != 0) {
  143.                 cout << "Error at position: " << error << endl;
  144.                 correct_error(code, error);
  145.                 cout << "Corrected code is: ";
  146.                 for (int i = 0; i < n; i++) cout << code[i] << " ";
  147.                 cout << endl;
  148.             } else {
  149.                 cout << "No error detected\n";
  150.             }
  151.  
  152.         }
  153.         cout << "Do you want to continue? (y/n) : ";
  154.         cin >> ch;
  155.         ch = tolower(ch);
  156.     } while (ch == 'y');
  157.  
  158.     cout << '\n';
  159.     cout << "Thank you for using this program\n";
  160.  
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement