Advertisement
Aaaaa988

Untitled

Nov 19th, 2021
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include "IntEncoding.h"
  6. #include "Sbooks.h"
  7. #include "Bwt.h"
  8.  
  9. using namespace std;
  10.  
  11. bool read_from_file(const char *filename, std::string &text) {
  12.     std::ifstream file(filename);
  13.     if (file.is_open()) {
  14.         std::string line;
  15.         while (std::getline(file, line))
  16.             text += line;
  17.         file.close();
  18.         return true;
  19.     }
  20.     return false;
  21. }
  22.  
  23. bool write_to_file(const char *filename, const std::string &text) {
  24.     std::ofstream file(filename);
  25.     if (file.is_open()) {
  26.         file << text;
  27.         file.close();
  28.         return true;
  29.     }
  30.     return false;
  31. }
  32.  
  33. int main() {
  34.     int choice;
  35.     while (true) {
  36.         system("cls");
  37.         cout << "\n======= Menu Lab6 =======\n";
  38.         cout << "\n1. Next symbol probability\n";
  39.         cout << "\n2. Possible and impossible continuations\n";
  40.         cout << "\n=========================\n";
  41.         cout << "Enter option >  ";
  42.         cin >> choice;
  43.         switch (choice) {
  44.             case 1: {
  45.                 string filename;
  46.                 cout << "enter filename: ";
  47.                 cin >> filename;
  48.                 string text;
  49.                 if (!read_from_file(filename.c_str(), text)) {
  50.                     cout << "\tError IO\n";
  51.                     break;
  52.                 }
  53.                 string directory;
  54.                 cout << "enter Path to result: ";
  55.                 cin >> directory;
  56.                 Bwt bwt;
  57.                 pair<string, int> pr = bwt.bwt(text);
  58.                 Sbooks bk;
  59.                 bk.set_symbols(pr.first);
  60.                 filename = directory + "/orig.txt";
  61.                 string result =
  62.                         bk.move_to_front(&IntEncoding::fi2);
  63.                 if (bk.write_to_file(filename.c_str(), result))
  64.                     cout << "\n\t>>original file " << filename
  65.                               << " was created\n";
  66.                 else
  67.                     cout << "\n\t>>original file " << filename
  68.                               << " wasn't created\n";
  69.                 int orig_length = result.length();
  70.                 vector<std::pair<char, int> > results;
  71.                 results.reserve(26);
  72.                 for (char ch = 'a'; ch <= 'z'; ++ch) {
  73.                     pr = bwt.bwt(string(text + ch));
  74.                     bk.init_positions();
  75.                     bk.set_symbols(pr.first);
  76.                     filename = directory + "/" + ch + ".txt";
  77.                     result = bk.move_to_front(&IntEncoding::fi2);
  78.                     if (bk.write_to_file(filename.c_str(), result))
  79.                         cout << "\t>>file " << filename << " was created\n";
  80.                     else
  81.                         cout << "\t>>file " << filename << " wasn't created\n";
  82.                     results.push_back(std::make_pair(ch,
  83.                                                      result.length()));
  84.                 }
  85.                 cout << "\ntext:\t\tsize:\n";
  86.                 cout << "orig-txt\t" << orig_length << "\n";
  87.                 pair<char, int> probability;
  88.                 probability.second = INT_MAX;
  89.                 probability.first = '#';
  90.                 for (int i = 0; i < results.size(); ++i) {
  91.                     int prob = results[i].second - orig_length;
  92.                     if (prob < probability.second) {
  93.                         probability.second = prob;
  94.                         probability.first = results[i].first;
  95.                     }
  96.                     cout << "" << "text + " <<
  97.                               results[i].first << " symbol\t" << results[i].second << " bit\n";
  98.                 }
  99.                 cout << "\nThe most probable symbol is: " <<
  100.                           probability.first << "\n";
  101.                 system("pause");
  102.                 break;
  103.             }
  104.             case 2: {
  105.                 string filename;
  106.                 cout << "enter filename: ";
  107.                 cin >> filename;
  108.                 string text;
  109.                 if (!read_from_file(filename.c_str(), text)) {
  110.                     std::cout << "\tCould not open file!\n";
  111.                     break;
  112.                 }
  113.                 string word;
  114.                 cout << "enter the word you want to add: ";
  115.                 cin >> word;
  116.                 string dir = filename.substr(0,filename.find_last_of(".")) + ".txt";
  117.                 if (!write_to_file(dir.c_str(), text + word)) {
  118.                     cout << "Could not save file!\n";
  119.                     break;
  120.                 }
  121.                 string directory;
  122.                 cout << "enter the name of the directory where you want to save: ";
  123.                 cin >> directory;
  124.                 cout << "\n--Add to archive original-file: ";
  125.                 string tmp;
  126.                 tmp = "tar -zcvf " + directory + "/original-" +
  127.                       filename.substr(filename.find_last_of("/") + 1) + ".tar.gz " + filename;
  128.                 cout << "\n--Add to archive original-plus-wordfile: ";
  129.                 tmp = "tar -zcvf " + directory + "/original-plus-" +
  130.                       word + ".tar.gz " + dir;
  131.  
  132.                 tmp = "(for /R " + directory + " %F in (*) do @echo %F %~zF) > filelist.txt";
  133.                 system(tmp.c_str());
  134.                 ifstream file("filelist.txt", std::ios::app);
  135.                 if (file.is_open()) {
  136.                     cout << "\nfile:\t\tsize:\n";
  137.                     string tmpf, tmps;
  138.                     while (file >> tmpf >> tmps)
  139.                         cout << "" <<
  140.                                   tmpf.substr(tmpf.find_last_of("\\") + 1) << "\t" << tmps << "\n";
  141.                     file.close();
  142.                 } else
  143.                     cout << "\tError read filelist\n";
  144.                 system("pause");
  145.                 break;
  146.             }
  147.             case 3:
  148.                 exit(0);
  149.         }
  150.     }
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement