Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <fstream>
- #include "IntEncoding.h"
- #include "Sbooks.h"
- #include "Bwt.h"
- using namespace std;
- bool read_from_file(const char *filename, std::string &text) {
- std::ifstream file(filename);
- if (file.is_open()) {
- std::string line;
- while (std::getline(file, line))
- text += line;
- file.close();
- return true;
- }
- return false;
- }
- bool write_to_file(const char *filename, const std::string &text) {
- std::ofstream file(filename);
- if (file.is_open()) {
- file << text;
- file.close();
- return true;
- }
- return false;
- }
- int main() {
- int choice;
- while (true) {
- system("cls");
- cout << "\n======= Menu Lab6 =======\n";
- cout << "\n1. Next symbol probability\n";
- cout << "\n2. Possible and impossible continuations\n";
- cout << "\n=========================\n";
- cout << "Enter option > ";
- cin >> choice;
- switch (choice) {
- case 1: {
- string filename;
- cout << "enter filename: ";
- cin >> filename;
- string text;
- if (!read_from_file(filename.c_str(), text)) {
- cout << "\tError IO\n";
- break;
- }
- string directory;
- cout << "enter Path to result: ";
- cin >> directory;
- Bwt bwt;
- pair<string, int> pr = bwt.bwt(text);
- Sbooks bk;
- bk.set_symbols(pr.first);
- filename = directory + "/orig.txt";
- string result =
- bk.move_to_front(&IntEncoding::fi2);
- if (bk.write_to_file(filename.c_str(), result))
- cout << "\n\t>>original file " << filename
- << " was created\n";
- else
- cout << "\n\t>>original file " << filename
- << " wasn't created\n";
- int orig_length = result.length();
- vector<std::pair<char, int> > results;
- results.reserve(26);
- for (char ch = 'a'; ch <= 'z'; ++ch) {
- pr = bwt.bwt(string(text + ch));
- bk.init_positions();
- bk.set_symbols(pr.first);
- filename = directory + "/" + ch + ".txt";
- result = bk.move_to_front(&IntEncoding::fi2);
- if (bk.write_to_file(filename.c_str(), result))
- cout << "\t>>file " << filename << " was created\n";
- else
- cout << "\t>>file " << filename << " wasn't created\n";
- results.push_back(std::make_pair(ch,
- result.length()));
- }
- cout << "\ntext:\t\tsize:\n";
- cout << "orig-txt\t" << orig_length << "\n";
- pair<char, int> probability;
- probability.second = INT_MAX;
- probability.first = '#';
- for (int i = 0; i < results.size(); ++i) {
- int prob = results[i].second - orig_length;
- if (prob < probability.second) {
- probability.second = prob;
- probability.first = results[i].first;
- }
- cout << "" << "text + " <<
- results[i].first << " symbol\t" << results[i].second << " bit\n";
- }
- cout << "\nThe most probable symbol is: " <<
- probability.first << "\n";
- system("pause");
- break;
- }
- case 2: {
- string filename;
- cout << "enter filename: ";
- cin >> filename;
- string text;
- if (!read_from_file(filename.c_str(), text)) {
- std::cout << "\tCould not open file!\n";
- break;
- }
- string word;
- cout << "enter the word you want to add: ";
- cin >> word;
- string dir = filename.substr(0,filename.find_last_of(".")) + ".txt";
- if (!write_to_file(dir.c_str(), text + word)) {
- cout << "Could not save file!\n";
- break;
- }
- string directory;
- cout << "enter the name of the directory where you want to save: ";
- cin >> directory;
- cout << "\n--Add to archive original-file: ";
- string tmp;
- tmp = "tar -zcvf " + directory + "/original-" +
- filename.substr(filename.find_last_of("/") + 1) + ".tar.gz " + filename;
- cout << "\n--Add to archive original-plus-wordfile: ";
- tmp = "tar -zcvf " + directory + "/original-plus-" +
- word + ".tar.gz " + dir;
- tmp = "(for /R " + directory + " %F in (*) do @echo %F %~zF) > filelist.txt";
- system(tmp.c_str());
- ifstream file("filelist.txt", std::ios::app);
- if (file.is_open()) {
- cout << "\nfile:\t\tsize:\n";
- string tmpf, tmps;
- while (file >> tmpf >> tmps)
- cout << "" <<
- tmpf.substr(tmpf.find_last_of("\\") + 1) << "\t" << tmps << "\n";
- file.close();
- } else
- cout << "\tError read filelist\n";
- system("pause");
- break;
- }
- case 3:
- exit(0);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement