Advertisement
Aaaaa988

11

Sep 22nd, 2021
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. string intTobin(int code){
  8.     if(code == 0) return "0";
  9.     string ch = "";
  10.     int temp = code;
  11.     while(temp > 0){
  12.         ch = temp%2?"1"+ch:"0"+ch;
  13.         temp = temp/2;
  14.     }
  15.     return ch;
  16. }
  17.  
  18. int binToInt(string code){
  19.     int result = 0;
  20.     for(int i = 0; i < code.size(); i++){
  21.         if(code[i] == '1'){
  22.             result+= pow(2,code.size()-1-i);
  23.         }
  24.     }
  25.     return result;
  26. }
  27.  
  28. string intTobint(int code){
  29.     if(code == 0) return "";
  30.     if(code == 1) return "";
  31.     string ch = "";
  32.     int temp = code;
  33.     while(temp > 0){
  34.         ch = temp%2?"1"+ch:"0"+ch;
  35.         temp = temp/2;
  36.     }
  37.     ch.erase(0,1);
  38.     return ch;
  39. }
  40.  
  41. string f0_code(int x){
  42.     string result = "";
  43.     for (int i = 0; i < x; ++i) {
  44.         result += "0";
  45.     }
  46.     return result+="1";
  47. }
  48.  
  49. string f1_code(int x){
  50.     string result = "";
  51.     if(x == 0) return "1";
  52.     result = f0_code(intTobin(x).size()) + intTobint(x);
  53.     return result;
  54. }
  55.  
  56. string f2_code(int x){
  57.     string result = "";
  58.     if(x == 0) return "1";
  59.     result = f1_code(intTobin(x).size()) + intTobint(x);
  60.     return result;
  61. }
  62.  
  63. string f0_decode(string code){
  64.     string decodeResult = "";
  65.     int nullCount = 0;
  66.     for(int i = 0; i < code.size(); i++){
  67.         if (code[i] == '1'){
  68.             decodeResult += to_string(nullCount);
  69.             if(i < code.size() - 1) decodeResult += ", ";
  70.             nullCount = 0;
  71.         }else{
  72.             nullCount++;
  73.         }
  74.     }
  75.     return decodeResult;
  76. }
  77.  
  78. string f1_decode(string code){
  79.     string decodeResult = "";
  80.     int skip = 0;
  81.     int nullCount = 0;
  82.     for(int i = 0; i < code.size(); i++){
  83.         if (code[i] == '1'){
  84.             if(nullCount == 0) {
  85.                 decodeResult += "0";
  86.                 if(i < code.size() - 1) decodeResult += ", ";
  87.                 continue;
  88.             }
  89.             decodeResult += to_string(binToInt(code.substr(i,nullCount)));
  90.             skip = nullCount-1;
  91.             nullCount = 0;
  92.             i+=skip;
  93.             if(i < code.size() - 1) decodeResult += ", ";
  94.         }else{
  95.             nullCount++;
  96.         }
  97.     }
  98.     return decodeResult;
  99. }
  100.  
  101. string f2_decode(string code){
  102.     string decodeResult = "";
  103.     int skip = 0;
  104.     int nullCount = 0;
  105.     int countInfo = 0;
  106.     for(int i = 0; i < code.size(); i++){
  107.         if (code[i] == '1'){
  108.             if(nullCount == 0) {
  109.                 decodeResult += "0";
  110.                 if(i < code.size() - 1) decodeResult += ", ";
  111.                 continue;
  112.             }
  113.             countInfo = binToInt(code.substr(i,nullCount)) - 1;
  114.             decodeResult += to_string(binToInt("1"+code.substr(i+nullCount,countInfo)));
  115.             skip = nullCount - 1 + countInfo;
  116.             nullCount = 0;
  117.             i+=skip;
  118.             if(i < code.size() - 1) decodeResult += ", ";
  119.             countInfo = 0;
  120.         }else{
  121.             nullCount++;
  122.         }
  123.     }
  124.     return decodeResult;
  125. }
  126.  
  127. string arrayToString(int* array, int N){
  128.     string result = "";
  129.     for(int i = 0; i < N; i++){
  130.         result += to_string(array[i]);
  131.         if (i < N - 1) result += ", ";
  132.     }
  133.     return result;
  134.  
  135. }
  136.  
  137. bool equalsStr(string a, string b){
  138.     if (a.size() != b.size()) return false;
  139.     bool check = false;
  140.     for(int i = 0; i < a.size(); i++){
  141.         if(a[i] != b[i]){
  142.             check = true;
  143.             break;
  144.         }
  145.     }
  146.     return !check;
  147. }
  148.  
  149. int main() {
  150.     int array[10] = {4, 32, 1, 53, 2, 5, 8, 123, 456, 345};
  151.     string code = "";
  152.  
  153.     cout<< "\n///////////////////////////// F0 /////////////////////\n";
  154.  
  155.     for(int i = 0; i < 10; i++){
  156.         cout << array[i] << " === " << f0_code(array[i])<<endl;
  157.         code += f0_code(array[i]);
  158.     }
  159.     cout << "\nCode F0 = " << code <<endl;
  160.     cout << "Input  sequence " << arrayToString(array, 10) << endl;
  161.     cout << "Decode sequence " << f0_decode(code) << endl;
  162.     if (equalsStr(arrayToString(array, 10), f0_decode(code))){
  163.         cout << "Input sequence restored " << endl;
  164.     }else{
  165.         cout << "Input sequence not restored " << endl;
  166.     }
  167.  
  168.     code = "";
  169.     cout<< "\n///////////////////////////// F1 /////////////////////\n";
  170.  
  171.     for(int i = 0; i < 10; i++){
  172.         cout << array[i] << " === " << f1_code(array[i])<<endl;
  173.         code += f1_code(array[i]);
  174.     }
  175.     cout << "\nCode F1 = " << code <<endl;
  176.     cout << "Input  sequence " << arrayToString(array, 10) << endl;
  177.     cout << "Decode sequence " << f1_decode(code) << endl;
  178.     if (equalsStr(arrayToString(array, 10), f1_decode(code))){
  179.         cout << "Input sequence restored " << endl;
  180.     }else{
  181.         cout << "Input sequence not restored " << endl;
  182.     }
  183.  
  184.     code = "";
  185.     cout<< "\n///////////////////////////// F2 /////////////////////\n";
  186.  
  187.     for(int i = 0; i < 10; i++){
  188.         cout << array[i] << " === " << f2_code(array[i])<<endl;
  189.         code += f2_code(array[i]);
  190.     }
  191.     cout << "\nCode F2 = " << code <<endl;
  192.     cout << "Input  sequence " << arrayToString(array, 10) << endl;
  193.     cout << "Decode sequence " << f2_decode(code) << endl;
  194.     if (equalsStr(arrayToString(array, 10), f2_decode(code))){
  195.         cout << "Input sequence restored " << endl;
  196.     }else{
  197.         cout << "Input sequence not restored " << endl;
  198.     }
  199.  
  200.  
  201.     int summLenghtF0 = 0;
  202.     int summLenghtF1 = 0;
  203.     int summLenghtF2 = 0;
  204.     double sumRatioF2F1 = 0;
  205.     double sumRatioF2F0 = 0;
  206.     int arrayRand[1000];
  207.  
  208.     srand(time(NULL));
  209.     for(int i = 0; i < 20; i++){
  210.         summLenghtF0 = 0;
  211.         summLenghtF1 = 0;
  212.         summLenghtF2 = 0;
  213.  
  214.         for(int j = 0; j < 1000; j++){
  215.             arrayRand[i] = rand()%100000;
  216.             summLenghtF0 += f0_code(arrayRand[i]).size();
  217.             summLenghtF1 += f1_code(arrayRand[i]).size();
  218.             summLenghtF2 += f2_code(arrayRand[i]).size();
  219.  
  220.         }
  221.         sumRatioF2F1 += (double)summLenghtF1/summLenghtF2;
  222.         sumRatioF2F0 += (double)summLenghtF0/summLenghtF2;
  223.     }
  224.     cout<<"\nin averege F2 short than F1 in "<< sumRatioF2F1 / 20 << " times" << endl;
  225.     cout<<"in averege F2 short than F0 in "<< sumRatioF2F0 / 20 << " times" << endl;
  226.     return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement