Advertisement
tachia

Untitled

Jun 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. void encodeFile(istream& infile, Node* encodingTree, obstream& outfile) {
  2.     char ch;
  3.     while(infile.get(ch)){
  4.         string str="";
  5.         string code=codeMaker(ch,encodingTree,str);
  6.     //  if(code.length()==0){
  7.     //      error("Your character doesnt exist");
  8.     //  }
  9.         for(int i=0; i<code.length(); i++){
  10.             int a=code[i]-'0';
  11.             outfile.writeBit(a);
  12.         }
  13.     }
  14.     string str="";
  15.     string code=codeMaker(PSEUDO_EOF,encodingTree,str);
  16.     for(int i=0; i<code.length(); i++){
  17.             int a=code[i]-'0';
  18.             outfile.writeBit(a);
  19.         }
  20.  
  21. }
  22. string codeMaker(ext_char ch, Node * encodingTree,string str){
  23.     Node* first=encodingTree->one;
  24.     Node* second=encodingTree->zero;
  25.     if(first!=NULL && first->character==ch){
  26.         return str+'1';
  27.     }
  28.     if(second!=NULL && second->character==ch){
  29.         return str+'0';
  30.     }
  31.     if(first!=NULL && first->character==NOT_A_CHAR && charExists(ch,first)){
  32.         return codeMaker(ch,first,str+'1');
  33.     }
  34.     if(second!=NULL && second->character==NOT_A_CHAR && charExists(ch,second)){
  35.         return codeMaker(ch,second,str+'0');
  36.     }
  37. return "";
  38. }
  39.  
  40. bool charExists(char ch, Node * tree){
  41.     Node * first=tree->one;
  42.     Node * second=tree->zero;
  43.     if((first==NULL && second==NULL) || ((first!=NULL && first->character!=NOT_A_CHAR && first->character!=ch) && (second!=NULL &&
  44.         second->character!=NOT_A_CHAR && second->character!=ch))){
  45.         return false;
  46.     }
  47.     if(first!=NULL && second!=NULL && (first->character==ch || second->character==ch)){
  48.         return true;
  49.     }else{
  50.         if(first!=NULL && first->character==ch){
  51.             return true;
  52.         }
  53.         if(second !=NULL && second->character==ch){
  54.             return true;
  55.         }
  56.     }
  57. return charExists(ch,first) || charExists(ch,second);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement