Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #define SIZE_OF_STRING 64
- using namespace std;
- class WordNote {
- public:
- friend std::istream &operator>>(std::istream &stream, WordNote &data) {
- return stream >> data.word_ >> data.number_of_occurrences_;
- }
- friend std::ostream &operator<<(std::ostream &stream, const WordNote &data) {
- return stream << data.word_ << " " << data.number_of_occurrences_;
- }
- private:
- char word_[SIZE_OF_STRING];
- uint64_t number_of_occurrences_{0};
- public:
- WordNote() = default;
- WordNote(const WordNote &other) = default;
- WordNote(WordNote &&other) noexcept = default;
- WordNote &operator=(const WordNote &other) = default;
- WordNote &operator=(WordNote &&other) noexcept = default;
- WordNote(const std::string &word, uint64_t number_of_occurrences)
- : number_of_occurrences_(number_of_occurrences), word_() {
- std::memcpy(word_, word.data(), SIZE_OF_STRING - 1);
- word_[SIZE_OF_STRING - 1] = '\0';
- }
- void setWord(const std::string &word) {
- std::memcpy(word_, word.data(), SIZE_OF_STRING - 1);
- word_[SIZE_OF_STRING - 1] = '\0';
- }
- std::string getWord() const {
- return std::string(word_);
- }
- uint64_t getNumberOfOccurrences() const {
- return number_of_occurrences_;
- }
- void setNumberOfOccurrences(uint64_t numberOfOccurrences) {
- number_of_occurrences_ = numberOfOccurrences;
- }
- static WordNote read(std::istream &);
- static void write(std::ostream &, const WordNote &);
- };
- WordNote WordNote::read(std::istream &stream) {
- WordNote word{};
- stream.read(reinterpret_cast<char *>(&word), sizeof(word));
- return word;
- }
- void WordNote::write(std::ostream &stream, const WordNote &data) {
- stream.write(reinterpret_cast<const char *>(&data), sizeof(data));
- }
- class FrequencyDictionary {
- std::string path_;
- std::fstream data_;
- public:
- void update(const std::string &word) {
- if (!data_.is_open()) {
- throw std::exception("Fail of file");
- }
- data_.seekg(0, std::ios_base::beg);
- auto start = data_.tellg();
- data_.seekg(0, std::ios_base::end);
- auto end = data_.tellg();
- data_.seekg(0, std::ios_base::beg);
- for (; start != end; start += sizeof(WordNote)) {
- data_.seekg(start, std::ios_base::beg);
- WordNote data = WordNote::read(data_);
- if (data.getWord() == word) {
- data.setNumberOfOccurrences(data.getNumberOfOccurrences() + 1);
- data_.seekg(start, std::ios_base::beg);
- WordNote::write(data_, data);
- data_.seekg(0, std::ios_base::beg);
- return;
- }
- }
- WordNote word_note(word, 1);
- WordNote::write(data_, word_note);
- data_.seekg(0, std::ios_base::beg);
- }
- void erase(const std::string &word) {
- if (!data_.is_open()) {
- throw std::exception("Fail of file");
- }
- data_.seekg(0, std::ios_base::beg);
- auto start = data_.tellg();
- data_.seekg(0, std::ios_base::end);
- auto end = data_.tellg();
- data_.seekg(0, std::ios_base::beg);
- std::vector<WordNote> new_notes;
- for (; start != end; start += sizeof(WordNote)) {
- data_.seekg(start, std::ios_base::beg);
- WordNote data = WordNote::read(data_);
- if (data.getWord() != word) {
- new_notes.push_back(data);
- }
- }
- data_.close();
- bind(path_);
- if (!data_.is_open()) {
- throw std::exception("Bad open!");
- }
- for (auto it = new_notes.begin(); it != new_notes.end(); ++it) {
- WordNote::write(data_, *it);
- }
- }
- void bind(const std::string &file_name) {
- data_.open(file_name, std::ios_base::binary | std::ios_base::out | std::ios_base::in);
- if (!data_.is_open()) {
- throw std::exception("Bad open!");
- }
- path_ = file_name;
- }
- void clear() {
- data_.close();
- data_.open(path_, std::ios_base::out);
- data_.close();
- data_.open(path_, std::ios_base::binary | std::ios_base::out | std::ios_base::in);
- if (!data_.is_open()) {
- throw std::exception("Bad open!");
- }
- }
- void fill(std::istream &stream) {
- if (!data_.is_open()) {
- throw std::exception("Bad open!");
- }
- clear();
- std::string word;
- while (stream >> word) {
- update(word);
- }
- }
- std::string maxWord() {
- if (!data_.is_open()) {
- throw std::exception("Bad open!");
- }
- WordNote max_word_note{};
- data_.seekg(0, std::ios_base::beg);
- auto start = data_.tellg();
- data_.seekg(0, std::ios_base::end);
- auto end = data_.tellg();
- data_.seekg(0, std::ios_base::beg);
- for (; start != end; start += sizeof(WordNote)) {
- data_.seekg(start, std::ios_base::beg);
- WordNote data = WordNote::read(data_);
- if (data.getNumberOfOccurrences() > max_word_note.getNumberOfOccurrences()) {
- max_word_note = data;
- }
- }
- return max_word_note.getWord();
- }
- void out(std::ostream &stream, char separator = '\n') {
- if (!data_.is_open()) {
- throw std::exception("Bad open!");
- }
- data_.seekg(0, std::ios_base::beg);
- auto start = data_.tellg();
- data_.seekg(0, std::ios_base::end);
- auto end = data_.tellg();
- data_.seekg(0, std::ios_base::beg);
- for (; start != end; start += sizeof(WordNote)) {
- data_.seekg(start, std::ios_base::beg);
- WordNote data = WordNote::read(data_);
- stream << data << separator;
- }
- }
- };
- void secondTaskRun() {
- FrequencyDictionary frequency_dictionary;
- while (true) {
- try {
- std::cout << "1) Open file" << std::endl;
- std::cout << "2) Update item" << std::endl;
- std::cout << "3) Erase item" << std::endl;
- std::cout << "4) Most frequently occurring word" << std::endl;
- std::cout << "5) Out in console" << std::endl;
- std::cout << "6) Out in file" << std::endl;
- std::cout << "7) Fill file" << std::endl;
- std::cout << "0) Exit" << std::endl;
- int num_of_operation;
- std::cin >> num_of_operation;
- if (num_of_operation == 1) {
- std::cout << "Enter the path:" << std::endl;
- std::string path;
- std::cin >> path;
- frequency_dictionary.bind(path);
- } else if (num_of_operation == 2) {
- std::cout << "Enter the word:" << std::endl;
- std::string word;
- std::cin >> word;
- frequency_dictionary.update(word);
- } else if (num_of_operation == 3) {
- std::cout << "Enter the word:" << std::endl;
- std::string word;
- std::cin >> word;
- frequency_dictionary.erase(word);
- } else if (num_of_operation == 4) {
- std::cout << frequency_dictionary.maxWord() << std::endl;
- } else if (num_of_operation == 5) {
- frequency_dictionary.out(std::cout);
- } else if (num_of_operation == 6) {
- std::cout << "Enter the path:" << std::endl;
- std::string path;
- std::cin >> path;
- std::ofstream out(path);
- if (!out) {
- throw std::exception("Bad open!");
- }
- frequency_dictionary.out(out);
- } else if (num_of_operation == 7) {
- std::cout << "Enter the path:" << std::endl;
- std::string path;
- std::cin >> path;
- std::ifstream in(path);
- if (!in) {
- throw std::exception("Bad open!");
- }
- frequency_dictionary.fill(in);
- } else {
- return;
- }
- } catch (std::exception &exception) {
- std::cout << exception.what() << std::endl;
- }
- }
- }
- int main() {
- secondTaskRun();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement