Advertisement
ludaludaed

Untitled

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