生成海明码

  • Post author:
  • Post category:其他

功能:输入信息位,生成海明码

编译环境:VS2022

样例:

  • 输入:
1010
  • 输出:
H1=P1=H3⊕H5⊕H7=011=0
H2=P2=H3⊕H6⊕H7=001=1
H4=P3=H5⊕H6⊕H7=101=0
1010010

代码如下:

#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<cmath> 
#include <windows.h>
#include<algorithm>
using namespace std;
#define JHM(value)  ((value==1||value==49)?1:(value==0||value==48)?0:0)
void colorxy(int x, int y)
{
    HANDLE hOut;
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hOut, x | y);
}
class HMCode
{
public:
	HMCode(string str, int str_len,int K) :code(str), len(str_len),K(K)
	{
		code.insert(0," ");
		ve.push_back({});
		ve.push_back({ 3,5,7,9,11 });
		ve.push_back({ 3,6,7,10,11 });
		ve.push_back({ 5,6,7,12 });
		ve.push_back({ 9,10,11,12 });
		print();
	}
	void print()
	{
		for (int i = 1; i <= K; i++)
		{
			cout << "H" << P_list[i] << "=";
			cout << "P" << i << "=";
			for (int j = 0; j < ve[i].size(); j++)
			{
				if (ve[i][j] > len)
				{
					continue;
				}
				else
				{
					if (j != 0) 
					{
						cout << "⊕";
					}
					cout << "H" << ve[i][j];
				}
			}
			cout << "=";

			for (int j = 0; j < ve[i].size(); j++)
			{
				if (ve[i][j] > len)
				{
					continue;
				}
				else
				{
					if (j != 0)
					{
						cout << "⊕";
					}
					cout << code[len-ve[i][j]+1];
				}
			}

			cout << "=" << code[len-P_list[i] + 1] << endl;
		}
		for (int i = 1; i <= len; i++)
		{
			if (i == 1 || i == 2 || i == 4 || i == 8)
			{
				colorxy(0x0c, 0x0c);
				cout << code[i];
				colorxy(0x07, 0x07);
			}
			else
			{
				cout << code[i];
			}
		}
	}
	vector<vector<int> >ve;
	int P_list [5] = {0,1,2,4,8};
	string code;
	int len;
	int K;
};
// 支持序列长度为:3~11位
char* TotalHMCodec(char* hm)
{
	if (!hm) { return nullptr; }

	// 计算k k^2≥k+n+1
	unsigned short hmLen = strlen(hm);
	if (hmLen <= 1) { return nullptr; }
	unsigned short K = 0;
	if (hmLen <= 4) { K = 3; }
	else if (hmLen <= 11) { K = 4; }
	else if (hmLen <= 26) { K = 5; }
	else if (hmLen <= 26) { K = 5; }

	// 创建处理新字符串
	int* HM = new int[hmLen * sizeof(int)], * pHM = HM;
	memset(HM, 0, hmLen * sizeof(int));
	for (short index = hmLen - 1; index >= 0; *pHM++ = hm[index], --index);
	pHM = nullptr;

	// 构建表格
	const unsigned short tableLen = K + hmLen;
	char* tableBuf = new char[tableLen + 1];
	memset(tableBuf, 0, tableLen + 1);
	std::vector<unsigned short> nTmp;
	for (unsigned short index = 0; index < K; nTmp.push_back(pow(2, index) - 1), ++index);

	// 表格填充
	pHM = HM;
	for (unsigned short index = 0; index < tableLen; ++index)
	{
		if (std::find(nTmp.begin(), nTmp.end(), index) == nTmp.end()) { tableBuf[index] = *pHM++; }
		else { tableBuf[index] = '-'; }
	}


	if (HM) { delete[]HM; }
	HM = pHM = nullptr;

	// 异或运算
	int* kBuf = new int[K * sizeof(int)];
	memset(kBuf, 0, K * sizeof(int));
	for (unsigned short index = 0; index < K; ++index)
	{
		if (index + 1 == 1)
		{
			if (tableLen == 5 || tableLen == 6) { kBuf[index] = JHM((int)(tableBuf[2] ^ tableBuf[4])); }									// 6
			else if (tableLen == 7 || tableLen == 8) { kBuf[index] = JHM((int)(tableBuf[2] ^ tableBuf[4] ^ tableBuf[6])); }						// 7
			else if (tableLen == 9 || tableLen == 10) { kBuf[index] = JHM((int)(tableBuf[2] ^ tableBuf[4] ^ tableBuf[6] ^ tableBuf[8])); }		// 9
			else if (tableLen == 11 || tableLen == 12) { kBuf[index] = JHM((int)(tableBuf[2] ^ tableBuf[4] ^ tableBuf[6] ^ tableBuf[8] ^ tableBuf[10])); }
		}
		else if (index + 1 == 2)
		{
			if (tableLen == 5) { kBuf[index] = JHM((int)(tableBuf[2])); }
			else if (tableLen == 6) { kBuf[index] = JHM((int)(tableBuf[5])); }													// 6
			else if (tableLen >= 7 && tableLen <= 9) { kBuf[index] = JHM((int)(tableBuf[5] ^ tableBuf[6])); }									// 7 9
			else if (tableLen == 10) { kBuf[index] = JHM((int)(tableBuf[2] ^ tableBuf[5] ^ tableBuf[6] ^ tableBuf[9])); }
			else if (tableLen == 11 || tableLen == 12) { kBuf[index] = JHM((int)(tableBuf[2] ^ tableBuf[5] ^ tableBuf[6] ^ tableBuf[9] ^ tableBuf[10])); }
		}
		else if (index + 1 == 3)
		{
			if (tableLen == 5) { kBuf[index] = JHM((int)(tableBuf[4])); }
			else if (tableLen == 6) { kBuf[index] = JHM((int)(tableBuf[4] ^ tableBuf[5])); }									// 6
			else if (tableLen >= 7 && tableLen <= 11) { kBuf[index] = JHM((int)(tableBuf[4] ^ tableBuf[5] ^ tableBuf[6])); }						// 7 9
			else if (tableLen == 12) { kBuf[index] = JHM((int)(tableBuf[4] ^ tableBuf[5] ^ tableBuf[6] ^ tableBuf[11])); }
		}
		else if (index + 1 == 4)
		{
			if (tableLen == 9) { kBuf[index] = JHM((int)(tableBuf[8])); }													// 9
			else if (tableLen == 10) { kBuf[index] = JHM((int)(tableBuf[8] ^ tableBuf[9])); }
			else if (tableLen == 11) { kBuf[index] = JHM((int)(tableBuf[8] ^ tableBuf[9] ^ tableBuf[10])); }
			else if (tableLen == 12) { kBuf[index] = JHM((int)(tableBuf[8] ^ tableBuf[9] ^ tableBuf[11] ^ tableBuf[11])); }
		}
	}

	// 填充完善
	if (K == 3 || K == 4)
	{
		tableBuf[0] = (char)(kBuf[0] + 48);
		tableBuf[1] = (char)(kBuf[1] + 48);
		tableBuf[3] = (char)(kBuf[2] + 48);
		if (K == 4) { tableBuf[7] = (char)(kBuf[3] + 48); }
	}
	if (kBuf) { delete[]kBuf; }kBuf = nullptr;

	// 翻转
	for (unsigned short index = 0; index < tableLen / 2; ++index)
	{
		char tmp = tableBuf[index];
		tableBuf[index] = tableBuf[tableLen - 1 - index];
		tableBuf[tableLen - 1 - index] = tmp;
	}

	string str = "";
	for (int i = 0; i < tableLen; i++)
	{
		str.push_back(tableBuf[i]);
	}
	HMCode MyCode(str, tableLen,K);

	// 调用者需要释放
	return tableBuf;
}
int main()
{
	while(1){
		char* s=new char [10000];
		scanf("%s", s);
		TotalHMCodec(s);
	}
}

参考
计算机基础 —— 海明码 (C++实现)-信必诺


版权声明:本文为weixin_50616227原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。