2

Cで書かれたLZWコンプレッサー/デコンプレッサーがあります。

最初のテーブルは 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;
}

あなたはそれを見つけることができますか?ありがたく思います。

4

1 に答える 1

4

デコード関数は、文字列の最初の文字を返します。辞書に追加するにはこの文字が必要ですが、設定しないpreviousCodeでください。したがって、コードは次のようになります。

...
firstChar = decode(currentCode);
dictionaryAdd(previousCode, firstChar, nextCode++);
previousCode = currentCode;
...
于 2009-12-02T15:21:39.153 に答える