Advertisement
ludaludaed

hw3_first_part

Dec 12th, 2022
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include "hash_table.h"
  6.  
  7. #define SIZE_OF_STRING 64
  8.  
  9. using namespace std;
  10.  
  11. class WordNote {
  12. public:
  13.     friend istream &operator>>(istream &stream, WordNote &data) {
  14.         return stream >> data.word_ >> data.number_of_occurrences_;
  15.     }
  16.  
  17.     friend ostream &operator<<(ostream &stream, const WordNote &data) {
  18.         return stream << data.word_ << " " << data.number_of_occurrences_;
  19.     }
  20.  
  21. private:
  22.     char word_[SIZE_OF_STRING];
  23.     uint64_t number_of_occurrences_{0};
  24.  
  25. public:
  26.     WordNote() = default;
  27.  
  28.     WordNote(const WordNote &other) = default;
  29.  
  30.     WordNote(WordNote &&other) noexcept = default;
  31.  
  32.     WordNote &operator=(const WordNote &other) = default;
  33.  
  34.     WordNote &operator=(WordNote &&other) noexcept = default;
  35.  
  36.     WordNote(const string &word, uint64_t number_of_occurrences)
  37.             : number_of_occurrences_(number_of_occurrences), word_() {
  38.         memcpy(word_, word.data(), SIZE_OF_STRING - 1);
  39.         word_[SIZE_OF_STRING - 1] = '\0';
  40.     }
  41.  
  42.     void setWord(const string &word) {
  43.         memcpy(word_, word.data(), SIZE_OF_STRING - 1);
  44.         word_[SIZE_OF_STRING - 1] = '\0';
  45.     }
  46.  
  47.     string getWord() const {
  48.         return string(word_);
  49.     }
  50.  
  51.     uint64_t getNumberOfOccurrences() const {
  52.         return number_of_occurrences_;
  53.     }
  54.  
  55.     void setNumberOfOccurrences(uint64_t numberOfOccurrences) {
  56.         number_of_occurrences_ = numberOfOccurrences;
  57.     }
  58.  
  59.     static WordNote read(istream &);
  60.  
  61.     static void write(ostream &, const WordNote &);
  62. };
  63.  
  64. WordNote WordNote::read(istream &stream) {
  65.     WordNote word{};
  66.     stream.read(reinterpret_cast<char *>(&word), sizeof(word));
  67.     return word;
  68. }
  69.  
  70. void WordNote::write(ostream &stream, const WordNote &data) {
  71.     stream.write(reinterpret_cast<const char *>(&data), sizeof(data));
  72. }
  73.  
  74.  
  75. class FrequencyDictionary {
  76.     string path_;
  77.     fstream data_;
  78.     hash_table<std::string, size_t> map;
  79.  
  80. public:
  81.     void update(const string &word) {
  82.         if (!data_.is_open()) {
  83.             throw exception("Fail of file");
  84.         }
  85.         data_.seekg(0, ios_base::beg);
  86.         auto result = map.find(word);
  87.         if (result) {
  88.             data_.seekg(result.value(), ios_base::beg);
  89.             WordNote data = WordNote::read(data_);
  90.             if (data.getWord() == word) {
  91.                 data.setNumberOfOccurrences(data.getNumberOfOccurrences() + 1);
  92.                 data_.seekg(result.value(), ios_base::beg);
  93.                 WordNote::write(data_, data);
  94.                 data_.seekg(0, ios_base::beg);
  95.             }
  96.         } else {
  97.             data_.seekg(0, ios_base::end);
  98.             auto end = data_.tellg();
  99.             WordNote word_note(word, 1);
  100.             WordNote::write(data_, word_note);
  101.             map.insert(word, static_cast<size_t>(end));
  102.             data_.seekg(0, ios_base::beg);
  103.         }
  104.     }
  105.  
  106.     void erase(const string &word) {
  107.         if (!data_.is_open()) {
  108.             throw exception("Fail of file");
  109.         }
  110.         data_.seekg(0, ios_base::beg);
  111.         auto start = data_.tellg();
  112.         data_.seekg(0, ios_base::end);
  113.         auto end = data_.tellg();
  114.  
  115.         data_.seekg(0, ios_base::beg);
  116.  
  117.         vector<WordNote> new_notes;
  118.  
  119.         for (; start != end; start += sizeof(WordNote)) {
  120.             data_.seekg(start, ios_base::beg);
  121.             WordNote data = WordNote::read(data_);
  122.             if (data.getWord() != word) {
  123.                 new_notes.push_back(data);
  124.             }
  125.         }
  126.         data_.close();
  127.         bind(path_);
  128.         if (!data_.is_open()) {
  129.             throw exception("Bad open!");
  130.         }
  131.         for (auto it = new_notes.begin(); it != new_notes.end(); ++it) {
  132.             WordNote::write(data_, *it);
  133.         }
  134.     }
  135.  
  136.     void bind(const string &file_name) {
  137.         data_.open(file_name, ios_base::binary | ios_base::out | ios_base::in);
  138.         if (!data_.is_open()) {
  139.             throw exception("Bad open!");
  140.         }
  141.         data_.seekg(0, ios_base::beg);
  142.         auto start = data_.tellg();
  143.         data_.seekg(0, ios_base::end);
  144.         auto end = data_.tellg();
  145.  
  146.         data_.seekg(0, ios_base::beg);
  147.  
  148.         for (; start != end; start += sizeof(WordNote)) {
  149.             data_.seekg(start, ios_base::beg);
  150.             auto current = data_.tellg();
  151.             WordNote data = WordNote::read(data_);
  152.             map.insert(data.getWord(), static_cast<size_t>(current));
  153.         }
  154.         path_ = file_name;
  155.     }
  156.  
  157.     void clear() {
  158.         data_.close();
  159.         data_.open(path_, ios_base::out);
  160.         data_.close();
  161.         data_.open(path_, ios_base::binary | ios_base::out | ios_base::in);
  162.         map.clear();
  163.         if (!data_.is_open()) {
  164.             throw exception("Bad open!");
  165.         }
  166.     }
  167.  
  168.     void fill(istream &stream) {
  169.         if (!data_.is_open()) {
  170.             throw exception("Bad open!");
  171.         }
  172.         clear();
  173.         string word;
  174.         while (stream >> word) {
  175.             update(word);
  176.         }
  177.     }
  178.  
  179.     string maxWord() {
  180.         if (!data_.is_open()) {
  181.             throw exception("Bad open!");
  182.         }
  183.         WordNote max_word_note{};
  184.  
  185.         data_.seekg(0, ios_base::beg);
  186.         auto start = data_.tellg();
  187.         data_.seekg(0, ios_base::end);
  188.         auto end = data_.tellg();
  189.  
  190.         data_.seekg(0, ios_base::beg);
  191.  
  192.         for (; start != end; start += sizeof(WordNote)) {
  193.             data_.seekg(start, ios_base::beg);
  194.             WordNote data = WordNote::read(data_);
  195.             if (data.getNumberOfOccurrences() > max_word_note.getNumberOfOccurrences()) {
  196.                 max_word_note = data;
  197.             }
  198.         }
  199.         return max_word_note.getWord();
  200.     }
  201.  
  202.     void out(ostream &stream, char separator = '\n') {
  203.         if (!data_.is_open()) {
  204.             throw exception("Bad open!");
  205.         }
  206.         data_.seekg(0, ios_base::beg);
  207.         auto start = data_.tellg();
  208.         data_.seekg(0, ios_base::end);
  209.         auto end = data_.tellg();
  210.  
  211.         data_.seekg(0, ios_base::beg);
  212.  
  213.         for (; start != end; start += sizeof(WordNote)) {
  214.             data_.seekg(start, ios_base::beg);
  215.             WordNote data = WordNote::read(data_);
  216.             stream << data << separator;
  217.         }
  218.     }
  219. };
  220.  
  221. void secondTaskRun() {
  222.     FrequencyDictionary frequency_dictionary;
  223.     while (true) {
  224.         try {
  225.             cout << "1) Open file" << endl;
  226.             cout << "2) Update item" << endl;
  227.             cout << "3) Erase item" << endl;
  228.             cout << "4) Most frequently occurring word" << endl;
  229.             cout << "5) Out in console" << endl;
  230.             cout << "6) Out in file" << endl;
  231.             cout << "7) Fill file" << endl;
  232.             cout << "0) Exit" << endl;
  233.  
  234.             int num_of_operation;
  235.             cin >> num_of_operation;
  236.             if (num_of_operation == 1) {
  237.                 cout << "Enter the path:" << endl;
  238.                 string path;
  239.                 cin >> path;
  240.                 frequency_dictionary.bind(path);
  241.             } else if (num_of_operation == 2) {
  242.                 cout << "Enter the word:" << endl;
  243.                 string word;
  244.                 cin >> word;
  245.                 frequency_dictionary.update(word);
  246.             } else if (num_of_operation == 3) {
  247.                 cout << "Enter the word:" << endl;
  248.                 string word;
  249.                 cin >> word;
  250.                 frequency_dictionary.erase(word);
  251.             } else if (num_of_operation == 4) {
  252.                 cout << frequency_dictionary.maxWord() << endl;
  253.             } else if (num_of_operation == 5) {
  254.                 frequency_dictionary.out(cout);
  255.             } else if (num_of_operation == 6) {
  256.                 cout << "Enter the path:" << endl;
  257.                 string path;
  258.                 cin >> path;
  259.                 ofstream out(path);
  260.                 if (!out) {
  261.                     throw exception("Bad open!");
  262.                 }
  263.                 frequency_dictionary.out(out);
  264.             } else if (num_of_operation == 7) {
  265.                 cout << "Enter the path:" << endl;
  266.                 string path;
  267.                 cin >> path;
  268.                 ifstream in(path);
  269.                 if (!in) {
  270.                     throw exception("Bad open!");
  271.                 }
  272.                 frequency_dictionary.fill(in);
  273.             } else {
  274.                 return;
  275.             }
  276.         } catch (exception &exception) {
  277.             cout << exception.what() << endl;
  278.         }
  279.     }
  280. }
  281.  
  282. int main() {
  283.     secondTaskRun();
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement