View Single Post
Old 04-17-2005, 10:29 AM   #1 (permalink)
napsak
Registered User
 
Join Date: Apr 2005
Posts: 7
napsak is on a distinguished road
Question Issue implementing algorithm of Lempel Ziv

I have been struggling with this program for many hours. I've also looked through most all of the 114 posts listed here for c++ relevant to my issue. The problem is with my array that holds code word not seen yet, it gets replaced everytime, letme explain.
I have my dictionary of code words setup as a struct with two
variables "code" and "key". I have no problem creating a dictionary
and I can even add to it. What happens is I do this:
STRING = get input character
WHILE there are still input characters
CHARACTER = get input character
IF STRING+CHARACTER is in the string table then
STRING = STRING+character
ELSE
output the code for STRING
add STRING+CHARACTER to the string table
STRING = CHARACTER
END of IF
END of WHILE

Everything works great the first time through the loop. Then what
happens is not good. The buffer I use to hold the STRING+CHARACTER
writes itself into all the following new structs I create that hold
more code words for the dictionary. Unfortunately my code is not very
clear, but I seem to know it inside and out. Could anyone please help direct me to what I can do to
possibly get my issue resolved? Thank you for your time and
consideration. attachments enclosed that can be opened in notepad.

Bit of code that is a member function of the user defined struct follows:

Code:
void token::newTable(token oldTable[])

{

ifstream input;

  input.open("text.txt", ios::in);



  if (!input)

    {

    cerr<<"Error opening file:" <<endl;

    return;

    }

  token newTable[5];

  int codeCntr=3;

  for(int j=0;j<=codeCntr;j++){

cout <<oldTable[j].key <<endl;

    newTable[j]=oldTable[j];

cout <<newTable[j].code <<endl;}

  int found=0;

  int k=0, h=0, first_time=1;//, loopCntr=1;

first_time=0;

//char current, next;

  char c[20]="";

  char tempString[20]="";

  char buffer[20]="";

// char *emptyArray[20]="     ";

ofstream outdata;

outdata.open("outdata.txt");

//buffer[30]=emptyArray[30];

//cout<<buffer<<"<---intial buffer"<<endl;

input.get(*tempString);

strcat(buffer,tempString);

  while(!input.eof())

    {

cout<< newTable[codeCntr].key<<endl<<endl;

//for(int m=0;m<1;m++)

//{

//buffer=emptyArray;

cout<< newTable[codeCntr].key<<endl;

//}

//for(int m=0;m<5;m++)

//buffer[m]=0;

input.get(*c);

//      strcat(buffer,tempString);

      strcat(buffer,c);



cout <<c <<"<---c value" <<endl;

cout <<buffer<<"<--buffer value"<<endl;



cout <<tempString <<"<---tempString"<<endl;

found=0;

for(k=0;k<=codeCntr;k++)

{

usleep(1600);

cout <<newTable[k].key <<"<---newTable character"<<endl;;

cout <<buffer<<" b4 if statement" <<endl;



  if(char(newTable[k].key)==char(buffer)&&found==0)

  {

        tempString=buffer;

        cout<<"found it**************" <<endl<<k<<endl;

for(int m=0;m<1;m++)

buffer[m]=0;

found=1;

//for(int l=0;l<10;l++)

//  buffer[30]=emptyArray[30];

//  strcpy(tempString,"    ");

  }

}

 if(found==0) 

{

h=0;

codeCntr++;

//char bufferHolder[20]=" ";



//bufferHolder=buffer;

      newTable[codeCntr].code=codeCntr;

cout<<newTable[codeCntr].code<<" newTable code"<<endl;

cout<<   (newTable[codeCntr].key=buffer)<<endl;

cout<< newTable[codeCntr].key<<endl;

strcpy(buffer,"\0");

 tempString=c;

cout<<tempString<<"<--tempString in no found" <<endl;

cout<<c<<"<--c value" <<endl;

cout<<codeCntr<<" = newTable.code"<<endl;

//for(int m=0;m<5;m++)

//{

//buffer=emptyArray;

cout<< newTable[codeCntr].key<<endl;

cout<<buffer<<"buffer after delete"<<endl;

cout<< newTable[codeCntr].key<<endl;

  

cout<<"in !found statement" <<endl;

}



    }//end first while loop



//for(int h=0;h<3;h++)

//cout<<  newTable[h].key;

input.close();

outdata.close();

return; 

}

here is all other code including above:

Code:
Script started on Sun Apr 17 05:51:07 2005
$ cat zipa.cpp

#include <iostream>

#include <cstring>

#include <fstream.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>





using std::itoa;

using std::cout;

using std::endl;

using std::ifstream;

class token

{

  public:

    token(int=0, char* ="        ");

    token setTable(int);

    void newTable(token[]);

    ~token();

//  private:

    int code;

    char * key;

};

//*************************************************************

token::token(int newCode, char *newKey)

{

  code=newCode>0 ? newCode:1;

  key=new char [strlen(newKey)+1];

  strcpy(key,newKey);

}

//*************************************************************

token::~token()

{

}

//*************************************************************

token token::setTable(int q)

{

token table[30];

  char *AlphaArray[]={"A","B","C","D"};

  table[q].key=AlphaArray[q];



  table[q].code=q;

//cout<< "in setTable member function"<<endl;

//cout<< AlphaArray[q] <<endl;

//cout<< table[q].key <<endl;

return table[q];

}

//*************************************************************

void token::newTable(token oldTable[])

{

ifstream input;

  input.open("text.txt", ios::in);



  if (!input)

    {

    cerr<<"Error opening file:" <<endl;

    return;

    }

  token newTable[5];

  int codeCntr=3;

  for(int j=0;j<=codeCntr;j++){

cout <<oldTable[j].key <<endl;

    newTable[j]=oldTable[j];

cout <<newTable[j].code <<endl;}

  int found=0;

  int k=0, h=0, first_time=1;//, loopCntr=1;

first_time=0;

//char current, next;

  char c[20]="";

  char tempString[20]="";

  char buffer[20]="";

// char *emptyArray[20]="     ";

ofstream outdata;

outdata.open("outdata.txt");

//buffer[30]=emptyArray[30];

//cout<<buffer<<"<---intial buffer"<<endl;

input.get(*tempString);

strcat(buffer,tempString);

  while(!input.eof())

    {

cout<< newTable[codeCntr].key<<endl<<endl;

//for(int m=0;m<1;m++)

//{

//buffer=emptyArray;

cout<< newTable[codeCntr].key<<endl;

//}

//for(int m=0;m<5;m++)

//buffer[m]=0;

input.get(*c);

//      strcat(buffer,tempString);

      strcat(buffer,c);



cout <<c <<"<---c value" <<endl;

cout <<buffer<<"<--buffer value"<<endl;



cout <<tempString <<"<---tempString"<<endl;

found=0;

for(k=0;k<=codeCntr;k++)

{

usleep(1600);

cout <<newTable[k].key <<"<---newTable character"<<endl;;

cout <<buffer<<" b4 if statement" <<endl;



  if(char(newTable[k].key)==char(buffer)&&found==0)

  {

        tempString=buffer;

        cout<<"found it**************" <<endl<<k<<endl;

for(int m=0;m<1;m++)

buffer[m]=0;

found=1;

//for(int l=0;l<10;l++)

//  buffer[30]=emptyArray[30];

//  strcpy(tempString,"    ");

  }

}

 if(found==0) 

{

h=0;

codeCntr++;

//char bufferHolder[20]=" ";



//bufferHolder=buffer;

      newTable[codeCntr].code=codeCntr;

cout<<newTable[codeCntr].code<<" newTable code"<<endl;

cout<<   (newTable[codeCntr].key=buffer)<<endl;

cout<< newTable[codeCntr].key<<endl;

strcpy(buffer,"\0");

	tempString=c;

cout<<tempString<<"<--tempString in no found" <<endl;

cout<<c<<"<--c value" <<endl;

cout<<codeCntr<<" = newTable.code"<<endl;

//for(int m=0;m<5;m++)

//{

//buffer=emptyArray;

cout<< newTable[codeCntr].key<<endl;

cout<<buffer<<"buffer after delete"<<endl;

cout<< newTable[codeCntr].key<<endl;

  

cout<<"in !found statement" <<endl;

}



    }//end first while loop



//for(int h=0;h<3;h++)

//cout<<  newTable[h].key;

input.close();

outdata.close();

return; 

}

//*************************************************************

int main()

{  

  token table [10];



//  char wordArray[50];

//  char tempLetter;

/*  char charValue;

  int intValue;

  int matched;

  int codeCntr=4;

  int wordCntr=0;

*/

  int codeCntr=3;//This is how many letters are in dictionary

  int i; 

/*  ifstream indata;

  indata.open("text.txt", ios::nocreate);



  if (!indata)

    {

    cerr<<"Error opening file:" <<endl;

    return 0;

    }*/

  for(i=0;i<=codeCntr;i++){

//indata >>tempLetter;

    table[i]=table[i].setTable(i);

//cout<<table[i].key <<"from main" <<endl;

}



  table[0].newTable(table);

/* 

  while (!indata.eof())

    {

    matched=0;

    while (!matched)

    {

      codefound=0;

      while(!codefound)

      {

        if( 

      codeCntr++;

        

  

  nextCode=getToken(indata);

cout<<value <<endl;

*/

return 0;

}



$ g++ zipa.cpp

$ a.out

A

0

B

1

C

2

D

3

D



D

B<---c value

AB<--buffer value

A<---tempString

A<---newTable character

AB b4 if statement

B<---newTable character

AB b4 if statement

C<---newTable character

AB b4 if statement

D<---newTable character

AB b4 if statement

4 newTable code

AB

AB

B<--tempString in no found

B<--c value

4 = newTable.code



buffer after delete



in !found statement







a<---c value

a<--buffer value

B<---tempString

A<---newTable character

a b4 if statement

B<---newTable character

a b4 if statement

C<---newTable character

a b4 if statement

D<---newTable character

a b4 if statement

a<---newTable character

a b4 if statement

found it**************

4







B<---c value

B<--buffer value

a<---tempString

A<---newTable character

B b4 if statement

B<---newTable character

B b4 if statement

C<---newTable character

B b4 if statement

D<---newTable character

B b4 if statement

B<---newTable character

B b4 if statement

found it**************

4







A<---c value

A<--buffer value

B<---tempString

A<---newTable character

A b4 if statement

B<---newTable character

A b4 if statement

C<---newTable character

A b4 if statement

D<---newTable character

A b4 if statement

A<---newTable character

A b4 if statement

found it**************

4







B<---c value

B<--buffer value

A<---tempString

A<---newTable character

B b4 if statement

B<---newTable character

B b4 if statement

C<---newTable character

B b4 if statement

D<---newTable character

B b4 if statement

B<---newTable character

B b4 if statement

found it**************

4







C<---c value

C<--buffer value

B<---tempString

A<---newTable character

C b4 if statement

B<---newTable character

C b4 if statement

C<---newTable character

C b4 if statement

D<---newTable character

C b4 if statement

C<---newTable character

C b4 if statement

found it**************

4







B<---c value

B<--buffer value

C<---tempString

A<---newTable character

B b4 if statement

B<---newTable character

B b4 if statement

C<---newTable character

B b4 if statement

D<---newTable character

B b4 if statement

B<---newTable character

B b4 if statement

found it**************

4









<---c value



<--buffer value

B<---tempString

A<---newTable character



 b4 if statement

B<---newTable character



 b4 if statement

C<---newTable character



 b4 if statement

D<---newTable character



 b4 if statement



<---newTable character



 b4 if statement

found it**************

4









<---c value



<--buffer value



<---tempString

A<---newTable character



 b4 if statement

B<---newTable character



 b4 if statement

C<---newTable character



 b4 if statement

D<---newTable character



 b4 if statement



<---newTable character



 b4 if statement

found it**************

4

$ ^Z

$ ^C

$ ^D


script done on Sun Apr 17 05:51:44 2005
napsak is offline   Reply With Quote