Skip to content
Snippets Groups Projects
Commit a2a89b46 authored by Eduardo Machado's avatar Eduardo Machado
Browse files

quase lá

parent dd009b50
Branches
No related tags found
No related merge requests found
......@@ -6,18 +6,14 @@ LIB=./lib
INCLUDE=./include
SRC=./src
OBJ=./obj
FLAGS = -Wall
FLAGS = -Wall -lm
main: connectionRawSocket messages
$(CC) $(SRC)/main.cpp $(OBJ)/connectionRawSocket.o $(OBJ)/messages.o $(FLAGS) -I$(INCLUDE) -L$(LIB) -o connection
main: lzw
$(CC) $(SRC)/main.cpp $(OBJ)/lzw.o $(FLAGS) -I$(INCLUDE) -L$(LIB) -o lzw
connectionRawSocket:
$(CC) -c $(SRC)/connectionRawSocket.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/connectionRawSocket.o
ar -cru $(LIB)/connectionRawSocket.a $(OBJ)/connectionRawSocket.o
messages:
$(CC) -c $(SRC)/messages.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/messages.o
ar -cru $(LIB)/messages.a $(OBJ)/messages.o
lzw:
$(CC) -c $(SRC)/lzw.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/lzw.o
ar -cru $(LIB)/lzw.a $(OBJ)/lzw.o
clean:
rm connection $(SRC)/*~ $(OBJ)/*o $(LIB)/*a
rm lzw $(SRC)/*~ $(OBJ)/*o $(LIB)/*a
// Implementado por Eduardo Machado e Victor Perszel
// 2015
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <cstdlib>
#include <cmath>
using namespace std;
class LZW{
public:
int* pack(string &input);
string unpack(int input[]);
};
\ No newline at end of file
void pack(string input, int* intOutput);
string unpack(int* input);
\ No newline at end of file
// Implementado por Eduardo Machado e Victor Perszel
// 2015
#include "lzw.h"
using namespace std;
int* LZW::pack(string &input){
void pack(string input, int* intOutput){
map<string, int> dictionary;
string output = "";
string nextChar, currentChar = "";
int i, temp, n, lastI, cont=0, dictionarySize = 256;
int *intOutput;
intOutput = new int[dictionarySize-1];
int i, j, temp, n = 0, size, lastI, cont=0, dictionarySize = 256;
//inicializa o dicionário
for(i = 0; i < 256; i++){
dictionary[string(1, i)] = i;
}
for(i = 0; i < input.size(); i++){
size = input.size();
for(i = 0; i < size; i++){
nextChar = input[i];
if(dictionary[currentChar + nextChar] != 0){
currentChar = currentChar + nextChar;
}else{
output += dictionary[currentChar] + " ";
cout << dictionary[currentChar] << endl;
cout << output << endl;
dictionary[currentChar+nextChar] = dictionarySize;
currentChar = nextChar;
dictionarySize++;
cont++;
}
//cout << output << endl;
}
output += dictionary[currentChar] + " ";
cont++;
// --------------------- CONVERSAO --------------------------
intOutput = new int[dictionarySize-1];
do{
if(output[i] != ' ' && output[i] != '\0'){
temp++;
......@@ -49,15 +53,15 @@ int* LZW::pack(string &input){
i++;
}while (output[i-1] != '\0');
//-----------------------------------------------------------
cout << intOutput << endl;
return &intOutput;
//-----------------------------------------------------------
}
string LZW::unpack(int input[]){
string unpack(int* input){
map<int, string> dictionary;
string currentChar, nextChar, output = "";
int i, dictionarySize = 256;
int i, size, dictionarySize = 256;
int currentWord, nextWord;
//inicializa o dicionário
......@@ -67,7 +71,8 @@ string LZW::unpack(int input[]){
nextWord = input[0];
output += dictionary[nextWord];
for(i = 0; i < sizeof(input)/sizeof(int); i++){
size = sizeof(input)/sizeof(int);
for(i = 0; i < size; i++){
currentWord = nextWord;
nextWord = input[i+1];
if(dictionary[nextWord] != ""){
......
// Implementado por Eduardo Machado e Victor Perszel
// 2015
#include "lzw.h"
using namespace std;
int main(int argc, char *argv[])
{
ifstream fileIn;
ofstream fileOut;
string parameter, temp, nameFileIn, nameFileOut, inputPack="", outputUnpack;
int *outputPack=NULL, *inputUnpack, length;
// Leitura de parâmetros.
if(argc != 4){
cout << "Arquivo não encontrado!" << endl;
return 0;
}
nameFileIn=argv[1];
parameter=argv[2];
nameFileOut=argv[3];
if(parameter == "pack"){
// Abertura do arquivo de entrada.
fileIn.open(nameFileIn.c_str(), ios::in);
fileIn.seekg (0);
// Leitura do arquivo.
while(getline(fileIn, temp)) {
inputPack += temp + "\n";
}
// Compactação.
pack(inputPack, outputPack);
// Escrita no arquivo de saída.
fileOut.open(nameFileOut.c_str(), ios::binary);
fileOut << outputPack;
}else if(parameter == "unpack"){
// Abertura do arquivo de entrada.
fileIn.open(nameFileIn.c_str(), ios::binary);
// Contagem do tamanho do arquivo.
fileIn.seekg (0, ios::end);
length = fileIn.tellg();
fileIn.seekg (0, ios::beg);
// Alocação do vetor para a leitura do arquivo.
inputUnpack = new int[length/4];
// Leitura.
fileIn.read((char*)inputUnpack, sizeof(inputUnpack));
// Descompactação.
outputUnpack = unpack(inputUnpack);
// Escrita no arquivo de saída.
fileOut.open(nameFileOut.c_str(), ios::out);
fileOut << outputUnpack;
}else{
cout << "Parâmetro inválido!" << endl;
}
// Fecha os arquivos
fileIn.close();
fileOut.close();
return 0;
}
\ No newline at end of file
apenas uma frase
ou duas
apenas para testar.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment