dorbird icon

LZW

dorbird | PRO | 03/11/15 05:39:53 PM UTC | 0 ⭐ | 274 👁️ | Never ⏰ | []
C++ |

5.68 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>      /* printf */
#include <stdlib.h>
#include <string>
#include <iostream>
#include <math.h> 
#include "main.h"
LZW::LZW(std::string fileName){
    FILE * file;
    file = fopen (fileName.c_str(),"r");
    if (file==NULL) perror ("Error opening file");
    else{
        fseek(file, 0, SEEK_END); // seek to end of file
        textLength = ftell(file); // get current file pointer
        fseek(file, 0, SEEK_SET); //back to the beginning of the file
        printf("Textlength: %d",textLength);//JAJ variable text längen!
        printf("\n");
        text=new char[textLength];
        for(int i=0;i<(textLength);i++){            
            text[i] = fgetc (file);//Char für char ausm text fischen   
            printf("%c",text[i]);//Ausgabe der soeben eingelesenne Chars
            //if((i%50)==0) printf("\n");
        }
        fclose (file);
    }   
    alphabetLength=256;
    alphabet = new char[alphabetLength];
    vorkommen = new int[alphabetLength];
    wahrscheinlichkeit = new float[alphabetLength];
    entscheidungsgehalt = new float[alphabetLength];
    printf("\nVorhandenes Alphabet:\n");
    for (int i=0; i<alphabetLength; i++){
        char temp = i;
        alphabet[i] = temp;
        printf("%c",alphabet[i]);
    }
    codiertInt = new int[textLength];
    for(int i=0;i<textLength;i++){
        codiertInt[i]=0;
    }
}
LZW::~LZW(){
    delete text;
    delete alphabet;
    delete vorkommen;
    delete wahrscheinlichkeit;
    delete entscheidungsgehalt;
    printf("Off");
}
    
void LZW::Entropie(){
//  array sauber machn warum auch immer es ohne nicht ging^^
    for(int i=0;i<alphabetLength;i++){ 
        vorkommen[i]=0;
        wahrscheinlichkeit[i]=0;
        entscheidungsgehalt[i]=0;
    }
    for (int i=0;i<textLength;i++){
        for(int j=0;j<alphabetLength;j++){
            if (text[i]==alphabet[j]){
                vorkommen[j]++;
            }
        }
    }
    for (int i=0;i<alphabetLength;i++){
        wahrscheinlichkeit[i]=(float)vorkommen[i]/(float)textLength;
    }
    for (int i=0;i<alphabetLength;i++){
        entscheidungsgehalt[i]=log2(1/(float)wahrscheinlichkeit[i]);
    }
    entropie=0;
    for (int i=0;i<alphabetLength;i++){
        entropie=entropie+(entscheidungsgehalt[i]*wahrscheinlichkeit[i]);
    }
//  Ausgabe von Alphabet un dden dazugehörigen berechneten werten  
    for (int i=0;i<alphabetLength;i++){
        if(vorkommen[i]!=0){//Filtern der nicht vorkommenden zeichen
            printf("\n%d",i);
            printf(" - %c",alphabet[i]);
            printf(" - %u",vorkommen[i]);
            printf(" - %f",wahrscheinlichkeit[i]);
        //  printf(" - %f",entscheidungsgehalt[i]); 
        }                   
    }
//  printf("\nEntropie: %f",entropie,"\n");
}
bool LZW::musterVorhanden(std::string zuTestendesMuster){
//  printf("Suche nach muster");
    for(int i=0;i<musterCounter-1;i++){
        if(zuTestendesMuster.compare(muster[i])==0){
        //  printf(" %d",i);
            lastTestedMuster=i;
            return true;//Muster vorhanden
        }
    //  printf("F%d",i);
    }
    return false;// Muster nicht vorhanden
}
void LZW::codieren(){
    printf("\nEs wird versucht zu codieren\n");
    std::string temp1="";
    std::string temp2="";
    int counter=0;
    int musterBuffer=1024;
    codierteTextLength=0;
    muster = new std::string[musterBuffer];
    for (int i=0;i<alphabetLength;i++){
        muster[i]=alphabet[i];
    }
    lastTestedMuster=0;
    musterCounter=alphabetLength;//ASCII Alphabet wird als muster betrachetet
    while(counter<=textLength){
        temp1=temp2+text[counter];
        if(musterVorhanden(temp1)){
            counter++;
            temp2=temp1;
        }else if(!musterVorhanden(temp1)){
            muster[musterCounter]=temp1;
        //  printf("   Neues muster angelegt: %s",temp1.c_str());
        //  printf(" %u",musterCounter);
            musterCounter++;
            printf(" %u",lastTestedMuster);
            codiertInt[codierteTextLength]=lastTestedMuster;
            codierteTextLength++;
            if(musterCounter==musterBuffer)musterCounter=alphabetLength;
            temp2="";
        }
    }   
//  printf("\nVorhande Muster: \n");
    for (int i=0;i<musterCounter;i++){
//      printf("    %d",i);
//      printf(" - %s",muster[i].c_str());
    }
    printf("\nCodierte Zeichen: \n");
    printf("%d",codierteTextLength);
}
bool LZW::decodeMusterVorhanden(std::string zuTestendesMuster){
//  printf("Suche nach muster");
    for(int i=0;i<decodeMusterCounter-1;i++){
        if(zuTestendesMuster.compare(decodeMuster[i])==0){
        //  printf(" %d",i);
            lastTestedMuster=i;
            return true;//Muster vorhanden
        }
    //  printf("F%d",i);
    }
    return false;// Muster nicht vorhanden
}
void LZW::decodieren(){
    printf("\nEs wird versucht zu decodieren\n");
    std::string temp1="";
    std::string temp2="";
    std::string decodiert="";
    int counter=0;
    int counter2=0;
    int decodeMusterBuffer=1024;
    decodeMusterCounter=alphabetLength;
    decodeMuster = new std::string[decodeMusterBuffer];
    for (int i=0;i<alphabetLength;i++){
        decodeMuster[i]=alphabet[i];
    }   
    while(counter<=textLength){     
    //  printf(" ->%d",codiertInt[counter2]);
        if(codiertInt[counter2]<=decodeMusterCounter-1&&counter2<codierteTextLength-1){
            decodiert=decodiert+decodeMuster[codiertInt[counter2]];
        //  printf("\nMuster bekannt decodiere:%s ",decodeMuster[codiertInt[counter2]].c_str());
            counter2++;
        }
        temp1=temp2+decodiert[counter];
        if(decodeMusterVorhanden(temp1)){
            counter++;
            temp2=temp1;
        }else if(!decodeMusterVorhanden(temp1)){
            decodeMuster[decodeMusterCounter]=temp1;
        //  printf("   Neues muster angelegt: %s",temp1.c_str());
        //  printf(" %u",decodeMusterCounter);
            decodeMusterCounter++;
            if(decodeMusterCounter==decodeMusterBuffer)decodeMusterCounter=alphabetLength;
            temp2="";
        }
    }
    printf("\ndecodierter text: \n");
    printf("%s",decodiert.c_str());
    
//  printf("\nVorhande Muster: \n");
    for (int i=0;i<decodeMusterCounter;i++){
//      printf("    %d",i);
//      printf(" - %s",decodeMuster[i].c_str());
    }
}
int main(){
    LZW lzw("Textbeispiel2.txt");
    lzw.Entropie();
    lzw.codieren();
    lzw.decodieren();
    char c;
    scanf("%c", &c); // Ende    
}

Comments