2009-12-02 69 views
1

我有一個LZW壓縮器/解壓縮器中C.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; 
} 

你能發現它嗎?我會很感激。

+1

請注意,你應該儘量避免依賴你的壓縮,直到你可以解壓縮它。換句話說,如果你的陳述「我的壓縮工作」實際上意味着「它減少了你的大小」,就是這樣,你不應該排除該代碼中的一個錯誤。 – 2009-12-02 15:03:09

+3

我的壓縮在我的輸入作品中使用別人的解壓縮。 – Radek 2009-12-02 15:04:27

+1

第8行 - > previousCode = input [0];似乎對我很可疑。你在decode()中調用appendCharacter()來輸出,但是這個第一個代碼永遠不會呈現給appendCharacter()來輸出。另外,如果inputSize爲零,則輸入[0]可能是一個糟糕的解引用。 – meklarian 2009-12-02 15:19:05

回答

4

你的解碼函數返回字符串中的第一個字符。你需要這個角色,以便將它添加到字典中,但你應該設置previousCode它。所以你的代碼應該看起來像:

... 
firstChar = decode(currentCode); 
dictionaryAdd(previousCode, firstChar, nextCode++); 
previousCode = currentCode; 
... 
+0

完美interjay,我現在明白爲什麼我做到了!對你好的業力:) – Radek 2009-12-02 15:26:48