Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. // PlacementTestPlaygrounds.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //Timo Nys
  3. //16-11-2019
  4.  
  5. #include "pch.h"
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <fstream>
  9. #include <string>
  10. #include <chrono>
  11.  
  12. std::string RunLengthEncoding(const std::string& word);
  13. std::string RunLengthDecoding(const std::string& word);
  14. float CalculateCompressionRatio(int originalCount, int newCount);
  15.  
  16. int main()
  17. {
  18.  
  19. #pragma region Encoding
  20. //-----1.Encoding/Compressing-----
  21. std::ifstream fileToEncode;
  22. std::string fileName;
  23. //Prompt the user for a VALID filename to compress
  24. do
  25. {
  26. std::cout << "Give the name of file to be encoded. \n";
  27. std::cin >> fileName;
  28.  
  29. fileToEncode.open(fileName + ".txt");
  30.  
  31. } while (!fileToEncode.is_open());
  32.  
  33. //Prompt the user how to name the file to compress to
  34. std::cout << "Give the name of file to write the compressed result to. \n";
  35. std::cin >> fileName;
  36. std::ofstream fileToCompressTo{ fileName + ".txt" };
  37.  
  38. //Compress every word in the file
  39. std::string wordToEncode;
  40.  
  41. //If file exists and can be opened start running RLE on the file
  42. if (fileToEncode.is_open())
  43. {
  44. //Start timing duration of encoding
  45. auto startTime = std::chrono::high_resolution_clock::now();
  46.  
  47. int originalLetterCount{ 0 };
  48. int newLetterCount{ 0 };
  49. while (fileToEncode >> wordToEncode)
  50. {
  51. originalLetterCount += wordToEncode.length();
  52. std::string compressedResult = RunLengthEncoding(wordToEncode);
  53. fileToCompressTo << compressedResult << ' '; //Parse the compressed result to the compressed file and add a space between individual words
  54. newLetterCount += compressedResult.length() ;
  55.  
  56. }
  57. //Print the compression rate(uncompressed size / compressed size)
  58. float compressionRatio = CalculateCompressionRatio(originalLetterCount, newLetterCount);
  59. std::cout << "Compression ratio: " << std::setprecision(4) << compressionRatio << "\n";
  60.  
  61. // Record end time of encoding
  62. auto endTime = std::chrono::high_resolution_clock::now();
  63.  
  64. std::cout << "Time in milliseconds to encode: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "\n";
  65. }
  66.  
  67. fileToCompressTo.close(); //Close the file so it can be used by another stream later
  68. #pragma endregion Encoding
  69.  
  70. #pragma region Decoding
  71. //-----2.Decode/Decompress-----
  72. std::ifstream fileToDecode;
  73. //Prompt the user for a VALID filename to compress
  74. do
  75. {
  76. std::cout << "Give the name of file to be Decoded. \n";
  77. std::cin >> fileName;
  78.  
  79. fileToDecode.open(fileName + ".txt");
  80.  
  81. } while (!fileToDecode.is_open());
  82.  
  83. //Prompt the user how to name the file to decompress to
  84. std::cout << "Give the name of file to write the decompressed result to. \n";
  85. std::cin >> fileName;
  86. std::ofstream fileToDecompressTo{ fileName + ".txt" };
  87.  
  88. //Deompress every word in the file
  89. std::string wordToDecode;
  90.  
  91. //If file exists and can be opened start running RLE on the file
  92. if (fileToDecode.is_open())
  93. {
  94. //Start timing duration of decoding
  95. auto startTime = std::chrono::high_resolution_clock::now();
  96. while (fileToDecode >> wordToDecode)
  97. {
  98.  
  99. std::string decompressedResult = RunLengthDecoding(wordToDecode);
  100. fileToDecompressTo << decompressedResult << ' '; //Parse the compressed result to the decompressed file and add a space between individual words
  101.  
  102. }
  103. // Record end time of decoding
  104. auto endTime = std::chrono::high_resolution_clock::now();
  105.  
  106. std::cout << "Time in milliseconds to decode: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "\n";
  107. }
  108.  
  109. #pragma endregion Decoding
  110. return 0;
  111. }
  112.  
  113. std::string RunLengthEncoding(const std::string& word)
  114. {
  115. std::string encoded{ "" };
  116. std::string toEncode{ word };//cache word in local memory
  117. for (size_t i = 0; i < toEncode.length(); ++i)
  118. {
  119. int amount{ 1 };//init at 1 cause the character/number will always appear at least once
  120.  
  121. while (i < toEncode.length() - 1 && toEncode[i] == toEncode[i + 1])
  122. {
  123. ++amount;
  124. ++i;
  125. }
  126. if (amount > 1) encoded += std::to_string(amount);
  127.  
  128. //If working with numbers add a symbol to make decompressing later easier
  129. if (isdigit(toEncode[i]))
  130. {
  131. char digit = toEncode[i] - '0';
  132.  
  133. encoded += "<";
  134. encoded += std::to_string(digit);
  135. encoded += ">";
  136. }
  137. else encoded += toEncode[i];
  138. }
  139. return encoded;
  140. }
  141.  
  142. std::string RunLengthDecoding(const std::string& word)
  143. {
  144. int amount = 0;
  145. std::string decoded{ "" };
  146. std::string toDecode{ word }; //cache the word
  147. for (size_t i = 0; i < toDecode.length(); ++i)
  148. {
  149. if (isdigit(toDecode[i]))
  150. {
  151. amount = amount * 10 + (toDecode[i] - '0');
  152. }
  153. else
  154. {
  155. if (toDecode[i] == '<')
  156. {
  157. if (amount == 0)
  158. {
  159. decoded += toDecode[i+1];
  160.  
  161. }
  162. while (amount > 0)
  163. {
  164. decoded += toDecode[i+1];
  165. --amount;
  166. }
  167. i+=2;
  168. }
  169. else
  170. {
  171. if (amount == 0)
  172. {
  173. decoded += toDecode[i];
  174. }
  175. while (amount > 0)
  176. {
  177. decoded += toDecode[i];
  178. --amount;
  179. }
  180. }
  181. }
  182. }
  183.  
  184. return decoded;
  185. }
  186.  
  187.  
  188.  
  189. float CalculateCompressionRatio(int originalCount, int newCount)
  190. {
  191. return float(originalCount) / float(newCount);
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement