【发布时间】:2009-12-02 14:59:33
【问题描述】:
我有一个用 C 编写的 LZW 压缩器/解压缩器。
初始表由 ASCII 字符组成,然后每个现在要保存到表中的字符串由一个 prefix 和一个 character 组成,两者都作为 int 保存在一个列表中。
我的压缩有效,但我的解压缩留下了一些字符。
输入:
<title>Agile</title><body><h1>Agile</h1></body></html>
我得到的输出(注意缺少'e'和'
<title>Agile</title><body><h1>Agil</h1></body>/html>
这是我使用的代码(相关部分):
void expand(int * input, int inputSize) {
// int prevcode, currcode
int previousCode; int currentCode;
int nextCode = 256; // start with the same dictionary of 255 characters
dictionaryInit();
// prevcode = read in a code
previousCode = input[0];
int pointer = 1;
// while (there is still data to read)
while (pointer < inputSize) {
// currcode = read in a code
currentCode = input[pointer++];
if (currentCode >= nextCode) printf("!"); // XXX not yet implemented!
currentCode = decode(currentCode);
// add a new code to the string table
dictionaryAdd(previousCode, currentCode, nextCode++);
// prevcode = currcode
previousCode = currentCode;
}
}
int decode(int code) {
int character; int temp;
if (code > 255) { // decode
character = dictionaryCharacter(code);
temp = decode(dictionaryPrefix(code)); // recursion
} else {
character = code; // ASCII
temp = code;
}
appendCharacter(character); // save to output
return temp;
}
你能看出来吗?我将不胜感激。
【问题讨论】:
-
请注意,在可以解压缩之前,您应该尽量避免依赖压缩。换句话说,如果您所说的“我的压缩有效”实际上意味着“它会减小您的大小”,就是这样,您不应该排除该代码中的错误。
-
我的压缩工作就像在我的输入工作上使用别人的解压缩。
-
第 8 行 -> previousCode = input[0];对我来说似乎很可疑。您在 decode() 中调用 appendCharacter() 进行输出,但第一个代码将永远不会呈现给 appendCharacter() 进行输出。此外,如果 inputSize 为零,则 input[0] 可能是错误的取消引用。
-
您是否有理由无法使用调试器单步执行代码以查看这些字符被跳过的原因?
标签: c compression lzw localized