-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathlzw.c
More file actions
178 lines (150 loc) · 4.56 KB
/
lzw.c
File metadata and controls
178 lines (150 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
*
* LZW压缩算法 - 字典压缩
*
* 问题:使用动态字典进行无损数据压缩
*
* 核心思想:
* - 动态构建编码字典
* - 查找最长匹配字符串
* - 输出字典索引
*
* 时间复杂度: O(n)
* 空间复杂度: O(n)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DICT_SIZE 4096
#define MAX_CODE_LEN 256
/*
*
* 字典条目
*/
typedef struct {
char string[MAX_CODE_LEN];
int code;
} DictionaryEntry;
/*
*
* LZW压缩
*/
void lzw_compress(const char* input, int* output, int* output_size) {
DictionaryEntry dictionary[MAX_DICT_SIZE];
int dict_size = 256;
// 初始化字典(单字符)
for (int i = 0; i < 256; i++) {
dictionary[i].string[0] = (char)i;
dictionary[i].string[1] = '\0';
dictionary[i].code = i;
}
char current[MAX_CODE_LEN] = "";
int output_idx = 0;
for (int i = 0; i < strlen(input); i++) {
char next_char = input[i];
char combined[MAX_CODE_LEN];
strcpy(combined, current);
combined[strlen(current)] = next_char;
combined[strlen(current) + 1] = '\0';
// 查找combined是否在字典中
int found = 0;
for (int j = 0; j < dict_size; j++) {
if (strcmp(dictionary[j].string, combined) == 0) {
strcpy(current, combined);
found = 1;
break;
}
}
if (!found) {
// 输出current的编码
for (int j = 0; j < dict_size; j++) {
if (strcmp(dictionary[j].string, current) == 0) {
output[output_idx++] = dictionary[j].code;
break;
}
}
// 将combined加入字典
if (dict_size < MAX_DICT_SIZE) {
strcpy(dictionary[dict_size].string, combined);
dictionary[dict_size].code = dict_size;
dict_size++;
}
// 重置current为next_char
current[0] = next_char;
current[1] = '\0';
}
}
// 输出最后一个编码
for (int j = 0; j < dict_size; j++) {
if (strcmp(dictionary[j].string, current) == 0) {
output[output_idx++] = dictionary[j].code;
break;
}
}
*output_size = output_idx;
}
/*
*
* LZW解压
*/
void lzw_decompress(const int* input, int input_size, char* output) {
DictionaryEntry dictionary[MAX_DICT_SIZE];
int dict_size = 256;
// 初始化字典(单字符)
for (int i = 0; i < 256; i++) {
dictionary[i].string[0] = (char)i;
dictionary[i].string[1] = '\0';
dictionary[i].code = i;
}
int old_code = input[0];
strcpy(output, dictionary[old_code].string);
char* output_ptr = output + strlen(dictionary[old_code].string);
for (int i = 1; i < input_size; i++) {
int new_code = input[i];
char string[MAX_CODE_LEN];
if (new_code >= dict_size) {
// 特殊情况:new_code不在字典中
strcpy(string, dictionary[old_code].string);
string[strlen(dictionary[old_code].string)] = dictionary[old_code].string[0];
string[strlen(dictionary[old_code].string) + 1] = '\0';
} else {
strcpy(string, dictionary[new_code].string);
}
// 输出string
strcpy(output_ptr, string);
output_ptr += strlen(string);
// 将old_string + string[0]加入字典
if (dict_size < MAX_DICT_SIZE) {
char new_entry[MAX_CODE_LEN];
strcpy(new_entry, dictionary[old_code].string);
new_entry[strlen(dictionary[old_code].string)] = string[0];
new_entry[strlen(dictionary[old_code].string) + 1] = '\0';
strcpy(dictionary[dict_size].string, new_entry);
dictionary[dict_size].code = dict_size;
dict_size++;
}
old_code = new_code;
}
}
/*
*
* 主函数
*/
int main() {
printf("=== LZW压缩算法 ===\n");
const char* input = "TOBEORNOTTOBEORTOBEORNOT";
printf("原始文本: %s\n", input);
int compressed[1000];
int compressed_size;
lzw_compress(input, compressed, &compressed_size);
printf("压缩后编码: ");
for (int i = 0; i < compressed_size; i++) {
printf("%d ", compressed[i]);
}
printf("\n");
char decompressed[1000];
lzw_decompress(compressed, compressed_size, decompressed);
printf("解压后文本: %s\n", decompressed);
printf("验证: %s\n", strcmp(input, decompressed) == 0 ? "成功" : "失败");
return 0;
}