⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 communication.cpp

📁 信息论与编码重要算法lz算法、汉明码实现码字 分组编码。
💻 CPP
字号:
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

const int char_l = 8;  //CII码表示范围,8位二元码

class CodeResult
{
private:
	int dict_l;
	string result;
public:
	CodeResult(string result, int dict_l)
	{
		this->dict_l = dict_l;
		this->result = result;
	}
	string getResult()
	{
		return result;
	}
	int getDict_l()
	{
		return dict_l;
	}
};

int getCodeLen(const int totalChars)  //totalChars个不同字符需要的二元码个数
{
	int temp = log(totalChars + 1) / log(2);
	int char_l = (int)temp;
	
	if (temp != char_l)
	{
		char_l += 1;
	}

	char_l += 1;

	return char_l;
}

int toDec(string c)
{
	int result = 0;
	int i;
	for (i = 0; i < c.length(); i++)
	{
		char temp = c.at(i);
		if (temp == '1')
		{
			result += pow(2, c.length() - i - 1);
		}
	}

	return result;
}

string toBinary(int num, int len)
{
	int i, rest;
	char a[1];
	vector<int> v;
	string result = "";
	string temp = "";
	
	if (num == 0)
	{
		v.push_back(0);
	}
	while (num != 0)
	{
		v.push_back(num % 2);
		num = num / 2;
	}
	for (i = v.size() - 1; i >= 0; i--)
	{
		result += temp.assign(itoa(v[i], a, 10));
	}

	temp.assign("");
	rest = len - result.length();
	if (rest > 0)
	{
		for (i = 0; i < rest; i++)
		{
			temp += "0";
		}
	}

	result = temp + result;
	return result;
}

CodeResult code(const string text)
{
	int i = 0, j = 0;
	int count = -1;
	string str;
	string result;
	string zero;
	vector<string> dict;  //字典
	vector<int> index;
	double temp = 0;
	int dict_l;  //字典需要dict_l位二位码元表示
	
	cout << "字典:" << endl;

	for (i = 0; i < text.length(); i++)
	{
		str = text.substr(i, 1);

		for (j = 0; j < dict.size(); j++)
		{
			if (i + 1 < text.length())
			{
				if (str.compare(dict[j]) == 0)
				{
					count = j;
					str += text.substr(i + 1, 1);
					i++;
					j = 0;
				}
			}
			else
				break;
		}

		if (count != -1) 
		{
			index.push_back(count);
		}
		dict.push_back(str);
		cout << str << endl;
		count = -1;
	}
	
	dict_l = getCodeLen(dict.size());
	zero = toBinary(0, dict_l);
	count = 0;
	for (i = 0; i < dict.size(); i++)
	{
		if (dict[i].length() == 1)
		{
			result += zero + toBinary(dict[i].at(0), char_l);
		}
		else
		{
			result += toBinary(index[count++]+1, dict_l) + 
				toBinary(dict[i].at(dict[i].length() - 1), char_l);
		}
	}

	return CodeResult(result, dict_l);
}

string decode(const string c, const int dict_l)
{
	int i, j;
	char a[2];
	string result;
	string temp;
	string dictGroup, charGroup;
	string zero = toBinary(0, dict_l);
	vector<string> dict;
	int groupLen = dict_l + char_l;
	int groupCount = c.length() / groupLen;
	
	a[1] = '\0';
	for (i = 0; i < groupCount; i++)
	{
		temp.assign("");
		dictGroup = c.substr(i * groupLen, dict_l);
		charGroup = c.substr(i * groupLen + dict_l, char_l);
		if (dictGroup.compare(zero) != 0)
		{
			temp += dict[toDec(dictGroup)-1];
		}

		a[0] = toDec(charGroup);
		
		temp += a;
		result += temp;
	//	cout << temp << endl;
		dict.push_back(temp);
	}
	
	return result;
}

int main()   
{

	char * text = "The sky is so clean I like it The sky is so clean I like it The sky is so clean I like it."; 
	CodeResult c = code(text);
	cout << "编码序列:" << endl;
	cout << c.getResult() << endl;
	string d = decode(c.getResult(), c.getDict_l());
	cout << "解码序列:" << endl;
	cout << d << endl;
	system("pause");
	return 0;
}   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -