我有一个LZW压缩机/减压器用C写的。
初始表由ASCII字符组成,然后每个要保存到表中的字符串都由一个前缀和一个字符组成,它们都保存在一个列表中作为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;
}你能认出它吗?我会很感激的。
发布于 2009-12-02 15:21:39
函数返回字符串中的第一个字符。您需要这个字符才能将它添加到字典中,但是不应该将previousCode设置为它。因此,您的代码应该如下所示:
...
firstChar = decode(currentCode);
dictionaryAdd(previousCode, firstChar, nextCode++);
previousCode = currentCode;
...https://stackoverflow.com/questions/1833437
复制相似问题