Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <ctime>
- using namespace std;
- string intTobin(int code){
- if(code == 0) return "0";
- string ch = "";
- int temp = code;
- while(temp > 0){
- ch = temp%2?"1"+ch:"0"+ch;
- temp = temp/2;
- }
- return ch;
- }
- int binToInt(string code){
- int result = 0;
- for(int i = 0; i < code.size(); i++){
- if(code[i] == '1'){
- result+= pow(2,code.size()-1-i);
- }
- }
- return result;
- }
- string intTobint(int code){
- if(code == 0) return "";
- if(code == 1) return "";
- string ch = "";
- int temp = code;
- while(temp > 0){
- ch = temp%2?"1"+ch:"0"+ch;
- temp = temp/2;
- }
- ch.erase(0,1);
- return ch;
- }
- string f0_code(int x){
- string result = "";
- for (int i = 0; i < x; ++i) {
- result += "0";
- }
- return result+="1";
- }
- string f1_code(int x){
- string result = "";
- if(x == 0) return "1";
- result = f0_code(intTobin(x).size()) + intTobint(x);
- return result;
- }
- string f2_code(int x){
- string result = "";
- if(x == 0) return "1";
- result = f1_code(intTobin(x).size()) + intTobint(x);
- return result;
- }
- string f0_decode(string code){
- string decodeResult = "";
- int nullCount = 0;
- for(int i = 0; i < code.size(); i++){
- if (code[i] == '1'){
- decodeResult += to_string(nullCount);
- if(i < code.size() - 1) decodeResult += ", ";
- nullCount = 0;
- }else{
- nullCount++;
- }
- }
- return decodeResult;
- }
- string f1_decode(string code){
- string decodeResult = "";
- int skip = 0;
- int nullCount = 0;
- for(int i = 0; i < code.size(); i++){
- if (code[i] == '1'){
- if(nullCount == 0) {
- decodeResult += "0";
- if(i < code.size() - 1) decodeResult += ", ";
- continue;
- }
- decodeResult += to_string(binToInt(code.substr(i,nullCount)));
- skip = nullCount-1;
- nullCount = 0;
- i+=skip;
- if(i < code.size() - 1) decodeResult += ", ";
- }else{
- nullCount++;
- }
- }
- return decodeResult;
- }
- string f2_decode(string code){
- string decodeResult = "";
- int skip = 0;
- int nullCount = 0;
- int countInfo = 0;
- for(int i = 0; i < code.size(); i++){
- if (code[i] == '1'){
- if(nullCount == 0) {
- decodeResult += "0";
- if(i < code.size() - 1) decodeResult += ", ";
- continue;
- }
- countInfo = binToInt(code.substr(i,nullCount)) - 1;
- decodeResult += to_string(binToInt("1"+code.substr(i+nullCount,countInfo)));
- skip = nullCount - 1 + countInfo;
- nullCount = 0;
- i+=skip;
- if(i < code.size() - 1) decodeResult += ", ";
- countInfo = 0;
- }else{
- nullCount++;
- }
- }
- return decodeResult;
- }
- string arrayToString(int* array, int N){
- string result = "";
- for(int i = 0; i < N; i++){
- result += to_string(array[i]);
- if (i < N - 1) result += ", ";
- }
- return result;
- }
- bool equalsStr(string a, string b){
- if (a.size() != b.size()) return false;
- bool check = false;
- for(int i = 0; i < a.size(); i++){
- if(a[i] != b[i]){
- check = true;
- break;
- }
- }
- return !check;
- }
- int main() {
- int array[10] = {4, 32, 1, 53, 2, 5, 8, 123, 456, 345};
- string code = "";
- cout<< "\n///////////////////////////// F0 /////////////////////\n";
- for(int i = 0; i < 10; i++){
- cout << array[i] << " === " << f0_code(array[i])<<endl;
- code += f0_code(array[i]);
- }
- cout << "\nCode F0 = " << code <<endl;
- cout << "Input sequence " << arrayToString(array, 10) << endl;
- cout << "Decode sequence " << f0_decode(code) << endl;
- if (equalsStr(arrayToString(array, 10), f0_decode(code))){
- cout << "Input sequence restored " << endl;
- }else{
- cout << "Input sequence not restored " << endl;
- }
- code = "";
- cout<< "\n///////////////////////////// F1 /////////////////////\n";
- for(int i = 0; i < 10; i++){
- cout << array[i] << " === " << f1_code(array[i])<<endl;
- code += f1_code(array[i]);
- }
- cout << "\nCode F1 = " << code <<endl;
- cout << "Input sequence " << arrayToString(array, 10) << endl;
- cout << "Decode sequence " << f1_decode(code) << endl;
- if (equalsStr(arrayToString(array, 10), f1_decode(code))){
- cout << "Input sequence restored " << endl;
- }else{
- cout << "Input sequence not restored " << endl;
- }
- code = "";
- cout<< "\n///////////////////////////// F2 /////////////////////\n";
- for(int i = 0; i < 10; i++){
- cout << array[i] << " === " << f2_code(array[i])<<endl;
- code += f2_code(array[i]);
- }
- cout << "\nCode F2 = " << code <<endl;
- cout << "Input sequence " << arrayToString(array, 10) << endl;
- cout << "Decode sequence " << f2_decode(code) << endl;
- if (equalsStr(arrayToString(array, 10), f2_decode(code))){
- cout << "Input sequence restored " << endl;
- }else{
- cout << "Input sequence not restored " << endl;
- }
- int summLenghtF0 = 0;
- int summLenghtF1 = 0;
- int summLenghtF2 = 0;
- double sumRatioF2F1 = 0;
- double sumRatioF2F0 = 0;
- int arrayRand[1000];
- srand(time(NULL));
- for(int i = 0; i < 20; i++){
- summLenghtF0 = 0;
- summLenghtF1 = 0;
- summLenghtF2 = 0;
- for(int j = 0; j < 1000; j++){
- arrayRand[i] = rand()%100000;
- summLenghtF0 += f0_code(arrayRand[i]).size();
- summLenghtF1 += f1_code(arrayRand[i]).size();
- summLenghtF2 += f2_code(arrayRand[i]).size();
- }
- sumRatioF2F1 += (double)summLenghtF1/summLenghtF2;
- sumRatioF2F0 += (double)summLenghtF0/summLenghtF2;
- }
- cout<<"\nin averege F2 short than F1 in "<< sumRatioF2F1 / 20 << " times" << endl;
- cout<<"in averege F2 short than F0 in "<< sumRatioF2F0 / 20 << " times" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement