Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.8 kB
3
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

class HammingCode {
private:
	string s;
	vector<int> msg;
	int r;
	int m;
public:
	void func() {

		m = s.size();
		r = 0;
		while (true) {
			if (pow(2, r) >= (m + r + 1))
				break;
			else
				r++;
		}
		//		cout << "r: " << r;
		vector<int> rs;
		int x = 0;
		for (int i = 0; i < r; i++) {
			rs.push_back(pow(2, x));
			x++;
		}
		int sz = rs.size() + m;
		vector<int> message(sz, -2);
		x = 1;
		for (int i = 0; i < r; i++) {
			message[sz - x] = -1;
			x = x * 2;
		}
		x = 0;
		sz = message.size();
		for (int i = 0; i < sz; i++) {
			if (message[i] == -1)
				continue;
			if (x < m)
				message[i] = (s[x++] - '0');
		}
		//		for(int i=0;i<sz;i++) cout<<message[i]<<" ";
		int XOR = 0;
		x = 1;
		int drop = 1;
		string error = "";
		for (int i = 0; i < r; i++) {
			int j = sz - x;
			x = x * 2;
			for (;; j -= drop) {
				if (j < 0) {
					j+=drop;
//					for(int l=j;l>=0;l--)
				}
				if (message[j] >= 0)
					XOR = XOR ^ (message[j]);
			}
			error += (XOR + '0');
			XOR = 0;
		}
		//		cout<<"error: "<<error;
		x = 0;
		for (int i = 0; i < sz; i++) {
			if (message[i] != -1)
				continue;
			message[i] = error[x++] - '0';
		}

		//		for (int i = 0; i < sz; i++)
		//			cout << message[i] << " ";
		error = "";
		XOR = 0;
		x = 1;
		drop = 1;
		for (int i = 0; i < r; i++) {
			int j = sz - x;
			x = x * 2;
			for (; j >= 0; j -= drop) {
				if (message[j] >= 0)
					XOR = XOR ^ (message[j]);
			}
			error += (XOR + '0');
			XOR = 0;
		}
		cout << "error: " << error;
		//		bool isError = 0;
		//		x = 1;
		//		for (int i = 0; i < error.size(); i++) {
		//			if (error[i] != '0') {
		//
		//			}
		//		}
	}
	HammingCode() {
		cout << "\nENTER MSG: ";
		cin >> this->s;
		func();
	}
};

int main() {
	cout << "s";
	HammingCode obj;
	return 0;
}