LZW压缩及解压C++实现

##LZW算法的压缩与解压
LZW压缩

#include <iostream>
#include <string>
#include <map>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {//对编码字符进行解码 
	int n;
	map<int, string> LzwMap;  //定义键值对存放<code,string> 
	cout<<"输入codes的个数"<<endl;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){//输入编码后的数字 
		cout<<"第"<<i+1<<"个数字(编码)";
		cin>>a[i];
	}
	LzwMap[1]="A",LzwMap[2]="B",LzwMap[3]="C";//初始化词典分别对A/B/C进行1/2/3编码
	string s="NIL";
	int count=4;//新字符的编码 
	int k;
	string str;
	for(int i=0;i<n;i++){//对每一个给定的k进行查找 
		k=a[i];
		map<int ,string>::iterator iter;
		bool flag=false;
		string value;
		for(iter=LzwMap.begin();iter!=LzwMap.end();iter++){//搜素字典中键为k的值 
			if (iter->first==k){
				flag=true;
				value=iter->second;
				break;	
			}	
		}
		if(flag==true){//若存在字典中,则输出值 
			cout<<value<<"--";
		}
		if(s!="NIL"){//把S对应的字符和字典k中对应的字符串的第一个字符相加并存储在字典中 
			const char *p=value.data();
			str=s+p[0];
			LzwMap[count++]=str;	
		}
		s=value;
	}
	return 0;
}

LZW解压

#include <iostream>
#include <string>
#include <map>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {//对编码字符进行解码
	int n;
	map<int, string> LzwMap;  //定义键值对存放<code,string>
	cout<<"输入codes的个数"<<endl;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){//输入编码后的数字
		cout<<"第"<<i+1<<"个数字(编码)"<<endl;
		cin>>a[i];
	}
	LzwMap[1]="A",LzwMap[2]="B",LzwMap[3]="C";//初始化词典分别对A/B/C进行1/2/3编码
	string s="NIL";
	int count=4;//新字符的编码
	int k;
	string str;
	for(int i=0;i<n;i++){//对每一个给定的k进行查找
		k=a[i];
		map<int ,string>::iterator iter;
		bool flag=false;
		string value;
		for(iter=LzwMap.begin();iter!=LzwMap.end();iter++){//搜素字典中键为k的值
			if (iter->first==k){
				flag=true;
				value=iter->second;
				break;
			}
		}
		if(flag==true){//若存在字典中,则输出值
			cout<<value<<"--";
		}
		if(s!="NIL"){//把S对应的字符和字典k中对应的字符串的第一个字符相加并存储在字典中
			const char *p=value.data();
			str=s+p[0];
			LzwMap[count++]=str;
		}
		s=value;
	}
	return 0;
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
LZW算法是一种无损压缩算法,可以将重复出现的字符串替换为较短的编码,并且可以实现压缩比。以下是在C语言中实现LZW算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define TABLE_SIZE 4096 // 编码表大小 typedef unsigned short code_t; // 编码类型 // 初始化编码表 void init_table(char **table) { for (int i = 0; i < 256; i++) { table[i] = (char *)malloc(2 * sizeof(char)); table[i][0] = (char)i; table[i][1] = '\0'; } for (int i = 256; i < TABLE_SIZE; i++) { table[i] = NULL; } } // 查找字符串在编码表中的位置 int find_string(char **table, char *str) { int i = 0; while (table[i] != NULL) { if (strcmp(table[i], str) == 0) { return i; } i++; } return -1; } // 将字符串插入编码表中 int insert_string(char **table, char *str) { int i = 0; while (table[i] != NULL) { i++; } if (i < TABLE_SIZE) { table[i] = (char *)malloc((strlen(str) + 1) * sizeof(char)); strcpy(table[i], str); } return i; } // LZW压缩函数 int lzw_compress(char *input, code_t *output) { char **table = (char **)malloc(TABLE_SIZE * sizeof(char *)); init_table(table); int input_len = strlen(input); char string[256]; int string_len = 0; int code = 0; int output_len = 0; for (int i = 0; i < input_len; i++) { string[string_len++] = input[i]; string[string_len] = '\0'; int index = find_string(table, string); if (index == -1) { output[output_len++] = code; code = insert_string(table, string); string_len = 1; string[0] = input[i]; string[1] = '\0'; } else { code = index; } } output[output_len++] = code; for (int i = 0; i < TABLE_SIZE; i++) { if (table[i] != NULL) { free(table[i]); } } free(table); return output_len; } // LZW函数 int lzw_decompress(code_t *input, int input_len, char *output) { char **table = (char **)malloc(TABLE_SIZE * sizeof(char *)); init_table(table); int output_len = 0; int code = input[0]; char *string = table[code]; int string_len = strlen(string); strcpy(output, string); output_len += string_len; for (int i = 1; i < input_len; i++) { code = input[i]; if (table[code] == NULL) { string = table[i]; string_len = strlen(string); strcat(output, string); output_len += string_len; table[code] = (char *)malloc((string_len + 1) * sizeof(char)); strcpy(table[code], string); } else { string = table[code]; string_len = strlen(string); strcat(output, string); output_len += string_len; char *prev_string = table[input[i - 1]]; int prev_string_len = strlen(prev_string); char new_string[256]; strcpy(new_string, prev_string); new_string[prev_string_len] = string[0]; new_string[prev_string_len + 1] = '\0'; table[TABLE_SIZE++] = (char *)malloc((strlen(new_string) + 1) * sizeof(char)); strcpy(table[TABLE_SIZE - 1], new_string); } } for (int i = 0; i < TABLE_SIZE; i++) { if (table[i] != NULL) { free(table[i]); } } free(table); return output_len; } int main() { char input[256] = "ababababab"; code_t output[256]; int output_len = lzw_compress(input, output); char decompressed[256]; int decompressed_len = lzw_decompress(output, output_len, decompressed); printf("Input: %s\n", input); printf("Output: "); for (int i = 0; i < output_len; i++) { printf("%d ", output[i]); } printf("\nDecompressed: %s\n", decompressed); return 0; } ``` 在此示例代码中,使用一个字符串数组作为编码表,并按字典序初始化前256个编码。在压缩过程中,先将第一个字符加入当前字符串,然后在编码表中查找是否有相同的字符串,若有则继续添加下一个字符,否则将当前字符串编码并加入编码序列中,然后将当前字符串重置为当前字符。在过程中,先将第一个编码对应的字符串加入输出中,然后按照编码序列逐个码,若当前编码在编码表中不存在,则将前一个编码对应的字符串加上当前字符串的第一个字符,并添加到编码表中,然后将当前编码对应的字符串加入输出中。若当前编码在编码表中存在,则将其对应的字符串加入输出中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值