Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PlacementTestPlaygrounds.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //Timo Nys
- //16-11-2019
- #include "pch.h"
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <chrono>
- std::string RunLengthEncoding(const std::string& word);
- std::string RunLengthDecoding(const std::string& word);
- float CalculateCompressionRatio(int originalCount, int newCount);
- int main()
- {
- #pragma region Encoding
- //-----1.Encoding/Compressing-----
- std::ifstream fileToEncode;
- std::string fileName;
- //Prompt the user for a VALID filename to compress
- do
- {
- std::cout << "Give the name of file to be encoded. \n";
- std::cin >> fileName;
- fileToEncode.open(fileName + ".txt");
- } while (!fileToEncode.is_open());
- //Prompt the user how to name the file to compress to
- std::cout << "Give the name of file to write the compressed result to. \n";
- std::cin >> fileName;
- std::ofstream fileToCompressTo{ fileName + ".txt" };
- //Compress every word in the file
- std::string wordToEncode;
- //If file exists and can be opened start running RLE on the file
- if (fileToEncode.is_open())
- {
- //Start timing duration of encoding
- auto startTime = std::chrono::high_resolution_clock::now();
- int originalLetterCount{ 0 };
- int newLetterCount{ 0 };
- while (fileToEncode >> wordToEncode)
- {
- originalLetterCount += wordToEncode.length();
- std::string compressedResult = RunLengthEncoding(wordToEncode);
- fileToCompressTo << compressedResult << ' '; //Parse the compressed result to the compressed file and add a space between individual words
- newLetterCount += compressedResult.length() ;
- }
- //Print the compression rate(uncompressed size / compressed size)
- float compressionRatio = CalculateCompressionRatio(originalLetterCount, newLetterCount);
- std::cout << "Compression ratio: " << std::setprecision(4) << compressionRatio << "\n";
- // Record end time of encoding
- auto endTime = std::chrono::high_resolution_clock::now();
- std::cout << "Time in milliseconds to encode: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "\n";
- }
- fileToCompressTo.close(); //Close the file so it can be used by another stream later
- #pragma endregion Encoding
- #pragma region Decoding
- //-----2.Decode/Decompress-----
- std::ifstream fileToDecode;
- //Prompt the user for a VALID filename to compress
- do
- {
- std::cout << "Give the name of file to be Decoded. \n";
- std::cin >> fileName;
- fileToDecode.open(fileName + ".txt");
- } while (!fileToDecode.is_open());
- //Prompt the user how to name the file to decompress to
- std::cout << "Give the name of file to write the decompressed result to. \n";
- std::cin >> fileName;
- std::ofstream fileToDecompressTo{ fileName + ".txt" };
- //Deompress every word in the file
- std::string wordToDecode;
- //If file exists and can be opened start running RLE on the file
- if (fileToDecode.is_open())
- {
- //Start timing duration of decoding
- auto startTime = std::chrono::high_resolution_clock::now();
- while (fileToDecode >> wordToDecode)
- {
- std::string decompressedResult = RunLengthDecoding(wordToDecode);
- fileToDecompressTo << decompressedResult << ' '; //Parse the compressed result to the decompressed file and add a space between individual words
- }
- // Record end time of decoding
- auto endTime = std::chrono::high_resolution_clock::now();
- std::cout << "Time in milliseconds to decode: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "\n";
- }
- #pragma endregion Decoding
- return 0;
- }
- std::string RunLengthEncoding(const std::string& word)
- {
- std::string encoded{ "" };
- std::string toEncode{ word };//cache word in local memory
- for (size_t i = 0; i < toEncode.length(); ++i)
- {
- int amount{ 1 };//init at 1 cause the character/number will always appear at least once
- while (i < toEncode.length() - 1 && toEncode[i] == toEncode[i + 1])
- {
- ++amount;
- ++i;
- }
- if (amount > 1) encoded += std::to_string(amount);
- //If working with numbers add a symbol to make decompressing later easier
- if (isdigit(toEncode[i]))
- {
- char digit = toEncode[i] - '0';
- encoded += "<";
- encoded += std::to_string(digit);
- encoded += ">";
- }
- else encoded += toEncode[i];
- }
- return encoded;
- }
- std::string RunLengthDecoding(const std::string& word)
- {
- int amount = 0;
- std::string decoded{ "" };
- std::string toDecode{ word }; //cache the word
- for (size_t i = 0; i < toDecode.length(); ++i)
- {
- if (isdigit(toDecode[i]))
- {
- amount = amount * 10 + (toDecode[i] - '0');
- }
- else
- {
- if (toDecode[i] == '<')
- {
- if (amount == 0)
- {
- decoded += toDecode[i+1];
- }
- while (amount > 0)
- {
- decoded += toDecode[i+1];
- --amount;
- }
- i+=2;
- }
- else
- {
- if (amount == 0)
- {
- decoded += toDecode[i];
- }
- while (amount > 0)
- {
- decoded += toDecode[i];
- --amount;
- }
- }
- }
- }
- return decoded;
- }
- float CalculateCompressionRatio(int originalCount, int newCount)
- {
- return float(originalCount) / float(newCount);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement