首页
学习
活动
专区
圈层
工具
发布

LZW减压
EN

Stack Overflow用户
提问于 2009-12-02 14:59:33
回答 1查看 3.2K关注 0票数 2

我有一个LZW压缩机/减压器用C写的。

初始表由ASCII字符组成,然后每个要保存到表中的字符串都由一个前缀和一个字符组成,它们都保存在一个列表中作为int。

我的压缩工作,但我的解压没有一些字符。

投入:

代码语言:javascript
复制
<title>Agile</title><body><h1>Agile</h1></body></html>

我得到的输出(注意缺少的'e‘和'<'):

代码语言:javascript
复制
<title>Agile</title><body><h1>Agil</h1></body>/html>

这是我使用的代码(相关部分):

代码语言:javascript
复制
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;
}

你能认出它吗?我会很感激的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-12-02 15:21:39

函数返回字符串中的第一个字符。您需要这个字符才能将它添加到字典中,但是不应该将previousCode设置为它。因此,您的代码应该如下所示:

代码语言:javascript
复制
...
firstChar = decode(currentCode);
dictionaryAdd(previousCode, firstChar, nextCode++);
previousCode = currentCode;
...
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1833437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档