-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHamming.cpp
88 lines (73 loc) · 1.62 KB
/
Hamming.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>
#include <vector>
#include <map>
std::ostream& operator<<(std::ostream& stream, std::vector<int> vector)
{
for (int i = 0; i < vector.size(); i++)
{
stream << vector[i];
}
return stream;
}
int xor_func(std::vector<int> pos_array) {
int result = 0;
for (int k = 0; k < pos_array.size(); k++)
{
result ^= pos_array[k];
}
return result;
}
int not_op(int a){
std::map<int, int> negation;
negation[1] = 0;
negation[0] = 1;
return negation.at(a);
}
int main()
{
// taking input as string and converting it to integer array
std::string input;
std::cout << "Enter Data: ";
std::cin >> input;
std::map<char, int> bitmap;
bitmap['1'] = 1;
bitmap['0'] = 0;
std::vector<int> bits;
int res;
res = input.length();
bits.reserve(res);
for (char b : input)
{
int tb;
tb = bitmap.at(b);
bits.emplace_back(tb);
}
std::cout << "[INFO] Entered messege: " << bits << std::endl;
// extracting the positions with bit 1
std::vector<int> pos;
pos.reserve(res);
for (int j = 0; j <= res; j++)
{
if (bits[j] == 1)
{
pos.emplace_back(j);
}
}
// std::cout << pos << std::endl;
// xor all of the position with bit
int result = xor_func(pos);
if (result == 0)
{
std::cout << "[INFO] No errors detected..\n";
}
else if (result != 0)
{
int error_position = result;
std::cout << "[INFO] Error Detected at " << result << " position from left\n";
std::cout << "[INFO] Fixing the error..\n";
bits[result] = not_op(bits[result]);
std::cout << "[SUCCESS] Error Fixed..\n";
std::cout << "[SUCCESS] Final corrected message: " << bits << std::endl;
}
}