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

📄 00.cpp

📁 实现简单的压缩解压缩功能!具体实现是使用霍夫曼编码原理
💻 CPP
字号:
#include"memory.h"
#include <stdio.h>
#include<string>
#include<iostream>
using namespace std;
//unsigned long string_to_long(const char* code)
unsigned long string_to_long(string str)
{
	const char*code=str.c_str();
	unsigned long ret = 0;
	int len = (int)strlen(code);
	for(int i = len - 1; i >= 0; i--)
		if (code[i] == '1')
			ret |= (1ul << (len - i - 1));
	
     	return ret;
}
string long_to_string(unsigned long code, int code_len)
{
	char* buf = new char[code_len + 1];
	if (buf == NULL)
		cout<<"no enough memory."<<endl;
	memset(buf, 0, code_len + 1);
	unsigned long bit = 1 << (code_len - 1);
	for(int i = 0; i < code_len; i++)
	{
		if (code & bit)
			buf[i] = '1';
		else
			buf[i] = '0';
		bit >>= 1;
	}
	string ret(buf); 
	delete buf;
	return ret;
}
void main()
{
	//char code[9]="10011010";
	string code="10011010";
	unsigned long key=string_to_long(code);
	cout<<key<<endl;
	char ch=char(key); 
	char store[8],temp;
for ( int i = 0 ; i < 8 ; i++ )
		{
			if ( ch & 128 ){ temp = '1'; }
			else { temp = '0'; }
			ch = ch << 1;
			store [ i ] = temp;
		}
string k=store;
		cout<<k<<endl;
  //cout<< long_to_string(key,9);
}
/*
void main()
{
	char code[11]="1001101010";
	unsigned long key=string_to_long(code);
	unsigned t=key;
	for(int i=0;i<10;i++)
	{	t=t>>1;
	    cout<<char(t)<<endl;
	    cout<< long_to_string(t,10)<<endl;
	}
}
*/

⌨️ 快捷键说明

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