Advertisement
Aaaaa988

22

Sep 22nd, 2021
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <fstream>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. string intTobin(int code){
  9.     if(code == 0) return "0";
  10.     string ch = "";
  11.     int temp = code;
  12.     while(temp > 0){
  13.         ch = temp%2?"1"+ch:"0"+ch;
  14.         temp = temp/2;
  15.     }
  16.     return ch;
  17. }
  18.  
  19. int binToInt(string code){
  20.     int result = 0;
  21.     for(int i = 0; i < code.size(); i++){
  22.         if(code[i] == '1'){
  23.             result+= pow(2,code.size()-1-i);
  24.         }
  25.     }
  26.     return result;
  27. }
  28.  
  29. string intTobint(int code){
  30.     if(code == 0) return "";
  31.     if(code == 1) return "";
  32.     string ch = "";
  33.     int temp = code;
  34.     while(temp > 0){
  35.         ch = temp%2?"1"+ch:"0"+ch;
  36.         temp = temp/2;
  37.     }
  38.     ch.erase(0,1);
  39.     return ch;
  40. }
  41.  
  42. string f0_code(int x){
  43.     string result = "";
  44.     for (int i = 0; i < x; ++i) {
  45.         result += "0";
  46.     }
  47.     return result+="1";
  48. }
  49.  
  50. string f1_code(int x){
  51.     string result = "";
  52.     if(x == 0) return "1";
  53.     result = f0_code(intTobin(x).size()) + intTobint(x);
  54.     return result;
  55. }
  56.  
  57. string f1_decode(string code){
  58.     string decodeResult = "";
  59.     int skip = 0;
  60.     int nullCount = 0;
  61.     for(int i = 0; i < code.size(); i++){
  62.         if (code[i] == '1'){
  63.             if(nullCount == 0) {
  64.                 decodeResult += "0";
  65.                 if(i < code.size() - 1) decodeResult += " ";
  66.                 continue;
  67.             }
  68.             decodeResult += to_string(binToInt(code.substr(i,nullCount)));
  69.             skip = nullCount-1;
  70.             nullCount = 0;
  71.             i+=skip;
  72.             if(i < code.size() - 1) decodeResult += " ";
  73.         }else{
  74.             nullCount++;
  75.         }
  76.     }
  77.     return decodeResult;
  78. }
  79.  
  80. set<char> readAlphabet(string fileName){
  81.     set<char> s;
  82.     ifstream fin;
  83.     fin.open(fileName);
  84.     if(!fin.is_open()){
  85.         cout << "Error open file";
  86.     }else{
  87.         while(!fin.eof())
  88.             s.insert(fin.get());
  89.     }
  90.     fin.close();
  91.     return s;
  92. }
  93.  
  94. int findIndexOfArray(int* arrayAlphabet, int sizeArray ,int charByte){
  95.     int result = -100;
  96.     for(int i = 0; i < sizeArray; i++){
  97.         if(charByte == arrayAlphabet[i]){
  98.             result = i;
  99.             break;
  100.         }
  101.     }
  102.     return result;
  103. }
  104.  
  105.  
  106. void shiftElementOfArray(int* arrayAlphabet, int sizeArray , int index){
  107.     int temp = arrayAlphabet[index];
  108.     for(int i = index; i > 0 ; --i){
  109.         arrayAlphabet[i] = arrayAlphabet[i-1];
  110.     }
  111.     arrayAlphabet[0] = temp;
  112. }
  113.  
  114. string stackBook(string fileName, int* arrayAlphabet, int sizeArray){
  115.     int byte;
  116.     string result = "";
  117.     ifstream fin;
  118.     fin.open(fileName);
  119.     if(!fin.is_open()){
  120.         cout << "Error open file";
  121.     }else{
  122.         while(!fin.eof()){
  123.             byte = fin.get();
  124.             cout<<byte <<" = ";
  125.             cout<<findIndexOfArray(arrayAlphabet, sizeArray, byte)<<endl;
  126.             result += f1_code(findIndexOfArray(arrayAlphabet, sizeArray, byte));
  127.             shiftElementOfArray(arrayAlphabet, sizeArray, findIndexOfArray(arrayAlphabet, sizeArray, byte));
  128.         }
  129.     }
  130.     fin.close();
  131.     return result;
  132. }
  133.  
  134. string stackBookDecode(string bookCode, int* arrayAlphabet, int sizeArray){
  135.     string result = "";
  136.     string scaned = "";
  137.     for(int i; i < bookCode.size(); i++){
  138.         if(bookCode[i] == ' '){
  139.             result += arrayAlphabet[stoi(scaned)];
  140.             shiftElementOfArray(arrayAlphabet, sizeArray, stoi(scaned));
  141.             scaned = "";
  142.  
  143.         }else{
  144.             scaned += bookCode[i];
  145.         }
  146.     }
  147.     return result;
  148. }
  149.  
  150. int weightBitOfFile(string fileName){
  151.     int result = 0;
  152.     ifstream fin;
  153.     fin.open(fileName);
  154.     if(!fin.is_open()){
  155.         cout << "Error open file";
  156.     }else{
  157.         while(!fin.eof()){
  158.             if(fin.get() == '\n'){
  159.                 result+=2;
  160.             }else{
  161.                 result++;
  162.             }
  163.         }
  164.     }
  165.     fin.close();
  166.     return (result-1) * 8;
  167. }
  168.  
  169.  
  170. int main() {
  171.     int* arrayAlphabet;
  172.     string bookCode = "";
  173.     string filename = "../test.txt";
  174.     set<char> Alphabet = readAlphabet(filename);
  175.     int sizeArray = Alphabet.size();
  176.  
  177.     arrayAlphabet = new int[sizeArray];
  178.  
  179.     set<char>::iterator setIt = Alphabet.begin();
  180.     for(int i = 0; i < sizeArray; i++, setIt++){
  181.         arrayAlphabet[i] = *setIt;
  182.     }
  183.  
  184.     bookCode = stackBook(filename, arrayAlphabet, sizeArray);
  185.     cout<<endl;
  186.     cout<<"Text into F1Code : \n"<<bookCode<<endl<<endl;
  187.  
  188.     setIt = Alphabet.begin();
  189.     for(int i = 0; i < sizeArray; i++, setIt++){
  190.         arrayAlphabet[i] = *setIt;
  191.     }
  192.  
  193.     int bitCountCode = bookCode.size();
  194.     int bitCountFile = weightBitOfFile(filename);
  195.    
  196.     cout<<"Restored text : \n"<< stackBookDecode(f1_decode(bookCode), arrayAlphabet, sizeArray) <<endl;
  197.     cout<<endl;
  198.     cout<<"Size StackBook = "<<sizeArray<<endl;
  199.     cout<<"Bits in Code = "<< bitCountCode << ", Bits in File = " << bitCountFile <<endl;
  200.     cout<<"Compression ratio "<<(float)bitCountFile/bitCountCode<<endl;
  201.    
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement