diff --git a/include/lzw.h b/include/lzw.h index d76e347fffa5565ba162b72e276b852e583e27b7..2373a757f12eac4114e9352502ab605fc27f61d7 100644 --- a/include/lzw.h +++ b/include/lzw.h @@ -12,4 +12,4 @@ using namespace std; int* pack(string input); -string unpack(int* input); \ No newline at end of file +string unpack(int* input, int size); \ No newline at end of file diff --git a/src/lzw.cpp b/src/lzw.cpp index 975f3946d5991ff33cd235174cea9ce5cf739bba..1abb35ff8a8ed6384b91230b10bd3de96963b205 100644 --- a/src/lzw.cpp +++ b/src/lzw.cpp @@ -40,10 +40,10 @@ int* pack(string input){ } -string unpack(int* input){ +string unpack(int* input, int size){ map<int, string> dictionary; string currentChar, nextChar, output = ""; - int i, size, dictionarySize = 256; + int i, dictionarySize = 256; int currentWord, nextWord; //inicializa o dicionário @@ -53,19 +53,18 @@ string unpack(int* input){ nextWord = input[0]; output += dictionary[nextWord]; - size = sizeof(input)/sizeof(int); - for(i = 0; i < size; i++){ + for(i = 0; i < size-1; i++){ currentWord = nextWord; nextWord = input[i+1]; if(dictionary[nextWord] != ""){ output += dictionary[nextWord]; currentChar = dictionary[currentWord]; - nextChar = dictionary[nextWord][1]; + nextChar = dictionary[nextWord][0]; dictionary[dictionarySize] = currentChar + nextChar; dictionarySize++; }else{ currentChar = dictionary[currentWord]; - nextChar = dictionary[currentWord][1]; + nextChar = dictionary[currentWord][0]; output += currentChar + nextChar; dictionary[dictionarySize] = currentChar + nextChar; dictionarySize++; diff --git a/src/main.cpp b/src/main.cpp index f3601c82ea25afe5ca4b98cca6e238bc8d8b4ac2..6bb21d6344954a0de28457f8dd2bf7b8f16ebee3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,8 +59,7 @@ int main(int argc, char *argv[]) } // Descompactação. - outputUnpack = unpack(inputUnpack); - cout << "outputUnpack = " << outputUnpack << endl; + outputUnpack = unpack(inputUnpack, length/4); // Escrita no arquivo de saída. fileOut.open(nameFileOut.c_str(), ios::out);