Advertisement
Aaaaa988

22

Sep 25th, 2021
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.36 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.     int summ = 0;
  118.     int i = 0;
  119.     ifstream fin;
  120.     fin.open(fileName);
  121.     if(!fin.is_open()){
  122.         cout << "Error open file";
  123.     }else{
  124.         while(!fin.eof()){
  125.             byte = fin.get();
  126.             cout<<(char)byte <<" = ";
  127.             cout<<findIndexOfArray(arrayAlphabet, sizeArray, byte)<<endl;
  128.             if(findIndexOfArray(arrayAlphabet, sizeArray, byte) != 0 && findIndexOfArray(arrayAlphabet, sizeArray, byte) != 1){
  129.                 summ += findIndexOfArray(arrayAlphabet, sizeArray, byte);
  130.                 i++;
  131.             }
  132.  
  133.             result += f1_code(findIndexOfArray(arrayAlphabet, sizeArray, byte));
  134.             shiftElementOfArray(arrayAlphabet, sizeArray, findIndexOfArray(arrayAlphabet, sizeArray, byte));
  135.         }
  136.     }
  137.     fin.close();
  138.  
  139.     cout<<"AVG item in stackBook == "<<(double)summ/i<<endl;
  140.     return result;
  141. }
  142.  
  143. string stackBookDecode(string bookCode, int* arrayAlphabet, int sizeArray){
  144.     string result = "";
  145.     string scaned = "";
  146.     for(int i; i < bookCode.size(); i++){
  147.         if(bookCode[i] == ' '){
  148.             result += arrayAlphabet[stoi(scaned)];
  149.             shiftElementOfArray(arrayAlphabet, sizeArray, stoi(scaned));
  150.             scaned = "";
  151.  
  152.         }else{
  153.             scaned += bookCode[i];
  154.         }
  155.     }
  156.     return result;
  157. }
  158.  
  159. int weightBitOfFile(string fileName){
  160.     int result = 0;
  161.     ifstream fin;
  162.     fin.open(fileName);
  163.     if(!fin.is_open()){
  164.         cout << "Error open file";
  165.     }else{
  166.         while(!fin.eof()){
  167.             if(fin.get() == '\n'){
  168.                 result+=2;
  169.             }else{
  170.                 result++;
  171.             }
  172.         }
  173.     }
  174.     fin.close();
  175.     return (result-1) * 8;
  176. }
  177.  
  178.  
  179. int main() {
  180.     int* arrayAlphabet;
  181.     string bookCode = "";
  182.     string filename = "../test.txt";
  183.     set<char> Alphabet = readAlphabet(filename);
  184.     int sizeArray = Alphabet.size();
  185.  
  186.     arrayAlphabet = new int[sizeArray];
  187.  
  188.     set<char>::iterator setIt = Alphabet.begin();
  189.     for(int i = 0; i < sizeArray; i++, setIt++){
  190.         arrayAlphabet[i] = *setIt;
  191.     }
  192.  
  193.     bookCode = stackBook(filename, arrayAlphabet, sizeArray);
  194.     cout<<endl;
  195.     cout<<"Text into F1Code : \n"<<bookCode<<endl<<endl;
  196.  
  197.     setIt = Alphabet.begin();
  198.     for(int i = 0; i < sizeArray; i++, setIt++){
  199.         arrayAlphabet[i] = *setIt;
  200.     }
  201.  
  202.     int bitCountCode = bookCode.size();
  203.     int bitCountFile = weightBitOfFile(filename);
  204.  
  205.     cout<<"Restored text : \n"<< stackBookDecode(f1_decode(bookCode), arrayAlphabet, sizeArray) <<endl;
  206.     cout<<endl;
  207.     cout<<"Size StackBook = "<<sizeArray<<endl;
  208.     cout<<"Bits in Code = "<< bitCountCode << ", Bits in File = " << bitCountFile <<endl;
  209.     cout<<"Compression ratio "<<(float)bitCountFile/bitCountCode<<endl;
  210.  
  211.     return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement