dorbird

LZW

Mar 11th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. #include <stdio.h>      /* printf */
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <math.h>
  6. #include "main.h"
  7. LZW::LZW(std::string fileName){
  8.     FILE * file;
  9.     file = fopen (fileName.c_str(),"r");
  10.     if (file==NULL) perror ("Error opening file");
  11.     else{
  12.         fseek(file, 0, SEEK_END); // seek to end of file
  13.         textLength = ftell(file); // get current file pointer
  14.         fseek(file, 0, SEEK_SET); //back to the beginning of the file
  15.         printf("Textlength: %d",textLength);//JAJ variable text längen!
  16.         printf("\n");
  17.         text=new char[textLength];
  18.         for(int i=0;i<(textLength);i++){           
  19.             text[i] = fgetc (file);//Char für char ausm text fischen  
  20.             printf("%c",text[i]);//Ausgabe der soeben eingelesenne Chars
  21.             //if((i%50)==0) printf("\n");
  22.         }
  23.         fclose (file);
  24.     }  
  25.     alphabetLength=256;
  26.     alphabet = new char[alphabetLength];
  27.     vorkommen = new int[alphabetLength];
  28.     wahrscheinlichkeit = new float[alphabetLength];
  29.     entscheidungsgehalt = new float[alphabetLength];
  30.     printf("\nVorhandenes Alphabet:\n");
  31.     for (int i=0; i<alphabetLength; i++){
  32.         char temp = i;
  33.         alphabet[i] = temp;
  34.         printf("%c",alphabet[i]);
  35.     }
  36.     codiertInt = new int[textLength];
  37.     for(int i=0;i<textLength;i++){
  38.         codiertInt[i]=0;
  39.     }
  40. }
  41. LZW::~LZW(){
  42.     delete text;
  43.     delete alphabet;
  44.     delete vorkommen;
  45.     delete wahrscheinlichkeit;
  46.     delete entscheidungsgehalt;
  47.     printf("Off");
  48. }
  49.    
  50. void LZW::Entropie(){
  51. //  array sauber machn warum auch immer es ohne nicht ging^^
  52.     for(int i=0;i<alphabetLength;i++){
  53.         vorkommen[i]=0;
  54.         wahrscheinlichkeit[i]=0;
  55.         entscheidungsgehalt[i]=0;
  56.     }
  57.     for (int i=0;i<textLength;i++){
  58.         for(int j=0;j<alphabetLength;j++){
  59.             if (text[i]==alphabet[j]){
  60.                 vorkommen[j]++;
  61.             }
  62.         }
  63.     }
  64.     for (int i=0;i<alphabetLength;i++){
  65.         wahrscheinlichkeit[i]=(float)vorkommen[i]/(float)textLength;
  66.     }
  67.     for (int i=0;i<alphabetLength;i++){
  68.         entscheidungsgehalt[i]=log2(1/(float)wahrscheinlichkeit[i]);
  69.     }
  70.     entropie=0;
  71.     for (int i=0;i<alphabetLength;i++){
  72.         entropie=entropie+(entscheidungsgehalt[i]*wahrscheinlichkeit[i]);
  73.     }
  74. //  Ausgabe von Alphabet un dden dazugehörigen berechneten werten 
  75.     for (int i=0;i<alphabetLength;i++){
  76.         if(vorkommen[i]!=0){//Filtern der nicht vorkommenden zeichen
  77.             printf("\n%d",i);
  78.             printf(" - %c",alphabet[i]);
  79.             printf(" - %u",vorkommen[i]);
  80.             printf(" - %f",wahrscheinlichkeit[i]);
  81.         //  printf(" - %f",entscheidungsgehalt[i]);
  82.         }                  
  83.     }
  84. //  printf("\nEntropie: %f",entropie,"\n");
  85. }
  86. bool LZW::musterVorhanden(std::string zuTestendesMuster){
  87. //  printf("Suche nach muster");
  88.     for(int i=0;i<musterCounter-1;i++){
  89.         if(zuTestendesMuster.compare(muster[i])==0){
  90.         //  printf(" %d",i);
  91.             lastTestedMuster=i;
  92.             return true;//Muster vorhanden
  93.         }
  94.     //  printf("F%d",i);
  95.     }
  96.     return false;// Muster nicht vorhanden
  97. }
  98. void LZW::codieren(){
  99.     printf("\nEs wird versucht zu codieren\n");
  100.     std::string temp1="";
  101.     std::string temp2="";
  102.     int counter=0;
  103.     int musterBuffer=1024;
  104.     codierteTextLength=0;
  105.     muster = new std::string[musterBuffer];
  106.     for (int i=0;i<alphabetLength;i++){
  107.         muster[i]=alphabet[i];
  108.     }
  109.     lastTestedMuster=0;
  110.     musterCounter=alphabetLength;//ASCII Alphabet wird als muster betrachetet
  111.     while(counter<=textLength){
  112.         temp1=temp2+text[counter];
  113.         if(musterVorhanden(temp1)){
  114.             counter++;
  115.             temp2=temp1;
  116.         }else if(!musterVorhanden(temp1)){
  117.             muster[musterCounter]=temp1;
  118.         //  printf("   Neues muster angelegt: %s",temp1.c_str());
  119.         //  printf(" %u",musterCounter);
  120.             musterCounter++;
  121.             printf(" %u",lastTestedMuster);
  122.             codiertInt[codierteTextLength]=lastTestedMuster;
  123.             codierteTextLength++;
  124.             if(musterCounter==musterBuffer)musterCounter=alphabetLength;
  125.             temp2="";
  126.         }
  127.     }  
  128. //  printf("\nVorhande Muster: \n");
  129.     for (int i=0;i<musterCounter;i++){
  130. //      printf("    %d",i);
  131. //      printf(" - %s",muster[i].c_str());
  132.     }
  133.     printf("\nCodierte Zeichen: \n");
  134.     printf("%d",codierteTextLength);
  135. }
  136. bool LZW::decodeMusterVorhanden(std::string zuTestendesMuster){
  137. //  printf("Suche nach muster");
  138.     for(int i=0;i<decodeMusterCounter-1;i++){
  139.         if(zuTestendesMuster.compare(decodeMuster[i])==0){
  140.         //  printf(" %d",i);
  141.             lastTestedMuster=i;
  142.             return true;//Muster vorhanden
  143.         }
  144.     //  printf("F%d",i);
  145.     }
  146.     return false;// Muster nicht vorhanden
  147. }
  148. void LZW::decodieren(){
  149.     printf("\nEs wird versucht zu decodieren\n");
  150.     std::string temp1="";
  151.     std::string temp2="";
  152.     std::string decodiert="";
  153.     int counter=0;
  154.     int counter2=0;
  155.     int decodeMusterBuffer=1024;
  156.     decodeMusterCounter=alphabetLength;
  157.     decodeMuster = new std::string[decodeMusterBuffer];
  158.     for (int i=0;i<alphabetLength;i++){
  159.         decodeMuster[i]=alphabet[i];
  160.     }  
  161.     while(counter<=textLength){    
  162.     //  printf(" ->%d",codiertInt[counter2]);
  163.         if(codiertInt[counter2]<=decodeMusterCounter-1&&counter2<codierteTextLength-1){
  164.             decodiert=decodiert+decodeMuster[codiertInt[counter2]];
  165.         //  printf("\nMuster bekannt decodiere:%s ",decodeMuster[codiertInt[counter2]].c_str());
  166.             counter2++;
  167.         }
  168.         temp1=temp2+decodiert[counter];
  169.         if(decodeMusterVorhanden(temp1)){
  170.             counter++;
  171.             temp2=temp1;
  172.         }else if(!decodeMusterVorhanden(temp1)){
  173.             decodeMuster[decodeMusterCounter]=temp1;
  174.         //  printf("   Neues muster angelegt: %s",temp1.c_str());
  175.         //  printf(" %u",decodeMusterCounter);
  176.             decodeMusterCounter++;
  177.             if(decodeMusterCounter==decodeMusterBuffer)decodeMusterCounter=alphabetLength;
  178.             temp2="";
  179.         }
  180.     }
  181.     printf("\ndecodierter text: \n");
  182.     printf("%s",decodiert.c_str());
  183.    
  184. //  printf("\nVorhande Muster: \n");
  185.     for (int i=0;i<decodeMusterCounter;i++){
  186. //      printf("    %d",i);
  187. //      printf(" - %s",decodeMuster[i].c_str());
  188.     }
  189. }
  190. int main(){
  191.     LZW lzw("Textbeispiel2.txt");
  192.     lzw.Entropie();
  193.     lzw.codieren();
  194.     lzw.decodieren();
  195.     char c;
  196.     scanf("%c", &c); // Ende   
  197. }
Advertisement
Add Comment
Please, Sign In to add comment