Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <fstream>
- #include <cmath>
- 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 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;
- }
- set<char> readAlphabet(string fileName){
- set<char> s;
- ifstream fin;
- fin.open(fileName);
- if(!fin.is_open()){
- cout << "Error open file";
- }else{
- while(!fin.eof())
- s.insert(fin.get());
- }
- fin.close();
- return s;
- }
- int findIndexOfArray(int* arrayAlphabet, int sizeArray ,int charByte){
- int result = -100;
- for(int i = 0; i < sizeArray; i++){
- if(charByte == arrayAlphabet[i]){
- result = i;
- break;
- }
- }
- return result;
- }
- void shiftElementOfArray(int* arrayAlphabet, int sizeArray , int index){
- int temp = arrayAlphabet[index];
- for(int i = index; i > 0 ; --i){
- arrayAlphabet[i] = arrayAlphabet[i-1];
- }
- arrayAlphabet[0] = temp;
- }
- string stackBook(string fileName, int* arrayAlphabet, int sizeArray){
- int byte;
- string result = "";
- int summ = 0;
- int i = 0;
- ifstream fin;
- fin.open(fileName);
- if(!fin.is_open()){
- cout << "Error open file";
- }else{
- while(!fin.eof()){
- byte = fin.get();
- cout<<(char)byte <<" = ";
- cout<<findIndexOfArray(arrayAlphabet, sizeArray, byte)<<endl;
- if(findIndexOfArray(arrayAlphabet, sizeArray, byte) != 0 && findIndexOfArray(arrayAlphabet, sizeArray, byte) != 1){
- summ += findIndexOfArray(arrayAlphabet, sizeArray, byte);
- i++;
- }
- result += f1_code(findIndexOfArray(arrayAlphabet, sizeArray, byte));
- shiftElementOfArray(arrayAlphabet, sizeArray, findIndexOfArray(arrayAlphabet, sizeArray, byte));
- }
- }
- fin.close();
- cout<<"AVG item in stackBook == "<<(double)summ/i<<endl;
- return result;
- }
- string stackBookDecode(string bookCode, int* arrayAlphabet, int sizeArray){
- string result = "";
- string scaned = "";
- for(int i; i < bookCode.size(); i++){
- if(bookCode[i] == ' '){
- result += arrayAlphabet[stoi(scaned)];
- shiftElementOfArray(arrayAlphabet, sizeArray, stoi(scaned));
- scaned = "";
- }else{
- scaned += bookCode[i];
- }
- }
- return result;
- }
- int weightBitOfFile(string fileName){
- int result = 0;
- ifstream fin;
- fin.open(fileName);
- if(!fin.is_open()){
- cout << "Error open file";
- }else{
- while(!fin.eof()){
- if(fin.get() == '\n'){
- result+=2;
- }else{
- result++;
- }
- }
- }
- fin.close();
- return (result-1) * 8;
- }
- int main() {
- int* arrayAlphabet;
- string bookCode = "";
- string filename = "../test.txt";
- set<char> Alphabet = readAlphabet(filename);
- int sizeArray = Alphabet.size();
- arrayAlphabet = new int[sizeArray];
- set<char>::iterator setIt = Alphabet.begin();
- for(int i = 0; i < sizeArray; i++, setIt++){
- arrayAlphabet[i] = *setIt;
- }
- bookCode = stackBook(filename, arrayAlphabet, sizeArray);
- cout<<endl;
- cout<<"Text into F1Code : \n"<<bookCode<<endl<<endl;
- setIt = Alphabet.begin();
- for(int i = 0; i < sizeArray; i++, setIt++){
- arrayAlphabet[i] = *setIt;
- }
- int bitCountCode = bookCode.size();
- int bitCountFile = weightBitOfFile(filename);
- cout<<"Restored text : \n"<< stackBookDecode(f1_decode(bookCode), arrayAlphabet, sizeArray) <<endl;
- cout<<endl;
- cout<<"Size StackBook = "<<sizeArray<<endl;
- cout<<"Bits in Code = "<< bitCountCode << ", Bits in File = " << bitCountFile <<endl;
- cout<<"Compression ratio "<<(float)bitCountFile/bitCountCode<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement