سؤال

لدي ضاغط 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;
}

يمكنك اكتشاف ذلك؟ سأكون ممتنا.

هل كانت مفيدة؟

المحلول

تقوم دالة فك التشفير بإرجاع الحرف الأول في السلسلة. تحتاج إلى هذه الشخصية من أجل إضافتها إلى القاموس، ولكن يجب عليك ليس تعيين previousCode لذلك. لذلك يجب أن تبدو الكود الخاص بك:

...
firstChar = decode(currentCode);
dictionaryAdd(previousCode, firstChar, nextCode++);
previousCode = currentCode;
...
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top