Pr0va1der

121

Mar 23rd, 2023
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. class Index {
  8. private:
  9. struct Entry {
  10. std::string word;
  11. std::vector<int> pages;
  12.  
  13. Entry(std::string w, std::vector<int> p) : word(w), pages(p) {}
  14. };
  15.  
  16. std::vector<Entry> entries;
  17.  
  18. public:
  19. // Добавление записи в указатель
  20. void addEntry(std::string word, std::vector<int> pages) {
  21. Entry e(word, pages);
  22. entries.push_back(e);
  23. }
  24.  
  25. // Вывод указателя на экран
  26. void printIndex() {
  27. for (auto e : entries) {
  28. std::cout << e.word << ": ";
  29. for (auto p : e.pages) {
  30. std::cout << p << " ";
  31. }
  32. std::cout << std::endl;
  33. }
  34. }
  35.  
  36. // Поиск номеров страниц для заданного слова
  37. void searchWord(std::string word) {
  38. auto e = std::find_if(entries.begin(), entries.end(), [=](const Entry& entry) {
  39. return entry.word == word;
  40. });
  41.  
  42. if (e != entries.end()) {
  43. std::cout << word << ": ";
  44. for (auto p : e->pages) {
  45. std::cout << p << " ";
  46. }
  47. std::cout << std::endl;
  48. } else {
  49. std::cout << "Слово не найдено в указателе" << std::endl;
  50. }
  51. }
  52.  
  53. // Удаление записи из указателя
  54. void removeEntry(std::string word) {
  55. entries.erase(std::remove_if(entries.begin(), entries.end(), [=](const Entry& entry) {
  56. return entry.word == word;
  57. }), entries.end());
  58. }
  59.  
  60. // Загрузка указателя из файла
  61. void loadIndexFromFile(std::string filename) {
  62. std::ifstream file(filename);
  63. if (file.is_open()) {
  64. std::string line;
  65. while (getline(file, line)) {
  66. std::string word;
  67. std::vector<int> pages;
  68. std::istringstream iss(line);
  69. iss >> word;
  70. int page;
  71. while (iss >> page) {
  72. pages.push_back(page);
  73. }
  74. addEntry(word, pages);
  75. }
  76. file.close();
  77. } else {
  78. std::cerr << "Не удалось открыть файл " << filename << std::endl;
  79. }
  80. }
  81.  
  82. // Сохранение указателя в файл
  83. void saveIndexToFile(std::string filename) {
  84. std::ofstream file(filename);
  85. if (file.is_open()) {
  86. for (auto e : entries) {
  87. file << e.word << " ";
  88. for (auto p : e.pages) {
  89. file << p << " ";
  90. }
  91. file << std::endl;
  92. }
  93. file.close();
  94. } else {
  95. std::cerr << "Не удалось создать файл " << filename << std::endl;
  96. }
  97. }
  98. };
  99.  
  100. int main() {
  101. Index index;
  102.  
  103. // Пример добавления записей в указатель
  104. index.addEntry("apple", {1, 3, 5});
  105. index.addEntry("banana", {2, 4, 6});
  106. index.addEntry("cherry", {3, 6, 9});
  107.  
  108. // Вывод указателя на экран
  109. index.printIndex();
  110.  
  111. // Поиск номеров страниц для заданного слова
  112. index.searchWord("apple");
  113.  
  114. // Удаление записи из указателя
  115. index.removeEntry("banana");
  116.  
  117. // Вывод указателя на экран после удаления записи
  118. index.printIndex();
  119.  
  120. // Загрузка указателя из файла
  121. index.loadIndexFromFile("index.txt");
  122.  
  123. // Вывод указателя на экран после загрузки из файла
  124. index.printIndex();
  125.  
  126. // Сохранение указателя в файл
  127. index.saveIndexToFile("index_new.txt");
  128.  
  129. return 0;
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment