Advertisement
Derga

Untitled

May 27th, 2023
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4. #include <forward_list>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. void GenerateFile(const string& pattern, const string& file_name) {
  10.     string text;
  11.     int WORDS_COUNT = 5;
  12.     srand(time(0));
  13.     for (int i = 0; i < WORDS_COUNT; ++i) {
  14.         int word_size = 1 + rand() % 10;
  15.         string word;
  16.         for (int j = 0; j < word_size; ++j) {
  17.             word += (char)('a' + rand() % 25);
  18.         }
  19.  
  20.         int pattern_chance = rand() % 11;
  21.         if (pattern_chance == 0) {
  22.             text += pattern;
  23.             text += ' ';
  24.         }
  25.  
  26.         if (i == 1 || i == 3) {
  27.             text += pattern;
  28.             text += ' ';
  29.         }
  30.  
  31.         text += word;
  32.         text += ' ';
  33.     }
  34.  
  35.     ofstream fout(file_name, std::ios::binary);
  36.     fout << text;
  37.     fout.close();
  38.  
  39. }
  40.  
  41. void Print(const forward_list<char>& l) {
  42.     auto it = l.begin();
  43.     while (it != l.end()) {
  44.         cout << *it++;
  45.     }
  46. }
  47.  
  48. int main() {
  49.     setlocale(LC_ALL, "");
  50.     string pattern, file_name;
  51.     cin >> pattern >> file_name;
  52.     GenerateFile(pattern, file_name);
  53.     ifstream fin(file_name);
  54.     if (!fin.is_open()) {
  55.         cout << "Файл невозможно открыть " << file_name;
  56.         return -1;
  57.     }
  58.     forward_list<char> text = { 't', 'a', 'c', ' ', ' ', 't', 'a', 'c' };
  59.     char read_in;
  60.     /*while (fin.get(read_in)) {
  61.         text.push_front(read_in);
  62.     }*/
  63.     Print(text);
  64.  
  65.     string revers_patter;
  66.     for (int i = pattern.size() - 1; i >= 0; i--) {
  67.         revers_patter += pattern[i];
  68.     }
  69.  
  70.     auto it = text.begin();
  71.     string save;
  72.     bool is_last_word = false;
  73.     for (; it != text.end(); it++) {
  74.         while (*it != ' ') {
  75.             save += *it;
  76.             auto is_end = it;
  77.             is_end++;
  78.             if (is_end == text.end()) {
  79.                 is_last_word = true;
  80.                 break;  
  81.             }
  82.             it++;
  83.             continue;
  84.         }
  85.  
  86.         if (save.find(revers_patter) != string::npos) {
  87.             if (is_last_word) {
  88.                 it = text.insert_after(it, ' ');
  89.             }
  90.             it = text.insert_after(it, save.begin(), save.end());
  91.             if (is_last_word) {
  92.                 break;
  93.             }
  94.             it = text.insert_after(it, ' ');
  95.         }
  96.         save.erase();
  97.     }
  98.     cout << endl;
  99.     Print(text);
  100.  
  101.  
  102.     return 0;
  103. }
  104. //cat data.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement