Advertisement
Mikhail-Podbolotov

Untitled

May 1st, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.87 KB | None | 0 0
  1. #include "library.h"
  2.  
  3. ListF::ListF()
  4. {
  5.     Start = nullptr;
  6.     End = nullptr;
  7. }
  8.  
  9. ListF::~ListF()
  10. {
  11.     Reserv* prom;
  12.     Reserv* t = Start;
  13.     while (t != 0) {
  14.         prom = t;
  15.         t = t->next;
  16.         delete prom;
  17.     }
  18. }
  19.  
  20. void ListF::ReadF(fstream& FD)
  21. {
  22.     int idx, count;
  23.     string cur;
  24.     Reserv* Temp = new Reserv;
  25.     FD >> idx;
  26.     FD >> cur;
  27.     FD >> count;
  28.     if (idx > 0) {
  29.         Temp->word = cur;
  30.         Temp->idx = idx;
  31.         Temp->count = count;
  32.         Temp->next = nullptr;
  33.         if (Start == nullptr) {
  34.             Start = End = Temp;
  35.         }
  36.         else {
  37.             End->next = Temp;
  38.             End = Temp;
  39.         }
  40.     }
  41. }
  42.  
  43. void ListF::ReadTextF(string text)
  44. {
  45.     string word;
  46.     bool inWord = false;
  47.     for (char c : text) {
  48.         if (isalpha(c)) {
  49.             word += c;
  50.             inWord = true;
  51.         }
  52.         else {
  53.             if (inWord) {
  54.                 bool found = false;
  55.                 Reserv* current = Start;
  56.                 while (current != nullptr) {
  57.                     if (current->word == word) {
  58.                         found = true;
  59.                         current->count++;
  60.                         word.clear();
  61.                         inWord = false;
  62.                         break;
  63.                     }
  64.                     current = current->next;
  65.                 }
  66.                 if (!found) {
  67.                     AddWord(word);
  68.                     word.clear();
  69.                     inWord = false;
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76. void ListF::AddWord(string word) {
  77.     Reserv* newReserv = new Reserv;
  78.     newReserv->word = word;
  79.     newReserv->count = 1;
  80.     newReserv->next = nullptr;
  81.     int idx = LastIdx();
  82.     newReserv->idx = ++idx;
  83.     if (Start == nullptr) {
  84.         Start = End = newReserv;
  85.     }
  86.     else {
  87.         End->next = newReserv;
  88.         End = newReserv;
  89.     }
  90. }
  91.  
  92. int ListF::LastIdx()
  93. {
  94.     int idx = 0;
  95.     Reserv* t = Start;
  96.     while (t != 0) {
  97.         idx = t->idx;
  98.         t = t->next;
  99.     }
  100.     return idx;
  101. }
  102.  
  103. Reserv* ListF::getStart()
  104. {
  105.     return Start;
  106. }
  107.  
  108. void ListF::PrintListF(fstream& FD)
  109. {
  110.     Reserv* t = Start;
  111.     while (t != 0) {
  112.         FD << t->idx << " " << t->word << " " << t->count << endl;
  113.         t = t->next;
  114.     }
  115. }
  116.  
  117. string ReadText(std::ifstream& input)
  118. {
  119.     string res = "";
  120.     string cur;
  121.     while (!input.eof()) {
  122.         getline(input, cur);
  123.         res += " " + cur;
  124.     }
  125.     res += " ";
  126.     return res;
  127. }
  128.  
  129. ListB::ListB()
  130. {
  131.     Start = nullptr;
  132.     End = nullptr;
  133. }
  134.  
  135. ListB::~ListB()
  136. {
  137.     BD* prom;
  138.     BD* t = Start;
  139.     while (t != 0) {
  140.         prom = t;
  141.         t = t->next;
  142.         delete prom;
  143.     }
  144. }
  145.  
  146. void ListB::ReadB(fstream& BDtext)
  147. {
  148.     int idx, count;
  149.     string cur;
  150.     string t;
  151.     BD* Temp = new BD;
  152.     BDtext >> idx;
  153.     getline(BDtext, cur);
  154.     getline(BDtext, t); //"\n"
  155.     if (idx > 0) {
  156.         Temp->str = cur;
  157.         Temp->idx = idx;
  158.         Temp->next = nullptr;
  159.         if (Start == nullptr) {
  160.             Start = End = Temp;
  161.         }
  162.         else {
  163.             End->next = Temp;
  164.             End = Temp;
  165.         }
  166.     }
  167. }
  168.  
  169. void ListB::ReadTextB(string text, ListF A)
  170. {
  171.     string word1;
  172.     string word2;
  173.     string cur1;
  174.     string cur2;
  175.     bool inword1 = false;
  176.     bool inword2 = false;
  177.     bool Flag = false;
  178.     for (char c : text) {
  179.         if (c == '!' || c == ',' or c == '.') {
  180.             word1 = "";
  181.             word2 = "";
  182.             inword1 = false;
  183.             inword2 = false;
  184.             Flag = false;
  185.         }
  186.         if (isalpha(c)) {
  187.             word1 += c;
  188.             inword1 = true;
  189.         }
  190.         if (c == ' ' && inword1) {
  191.             inword2 = true;
  192.         }
  193.         if (isalpha(c) && inword2) {
  194.             word2 += c;
  195.             Flag = true;
  196.         }
  197.         if (c == ' ' && Flag) {
  198.             Reserv* currentFD1 = A.getStart();
  199.             while (currentFD1 != nullptr) {
  200.                 if (currentFD1->word == word1) {
  201.                     Reserv* currentFD2 = currentFD1->next;
  202.                     while (currentFD2 != nullptr) {
  203.                         if (currentFD2->word == word2) {
  204.                             int idx0 = currentFD2->idx;
  205.                             BD* currentBD = Start;
  206.                             while (currentBD != nullptr) {
  207.                                 if (currentBD->str != "") { // нашли два слова и строка не пустая
  208.                                     int length = currentBD->str.length();
  209.                                     string cur = currentBD->str;
  210.                                     string res = "";
  211.                                     string residx = "";
  212.                                     string rescount = "";
  213.                                     int count0 = 0;
  214.                                     bool Residx = false;
  215.                                     bool Result = false;
  216.                                     int RememberIdx = 0;
  217.                                     for (int i = 0; i < length - 1; i++) {
  218.                                         if (isdigit(cur[i]) && !isdigit(cur[i + 1])) {
  219.                                             residx += cur[i];
  220.                                             Residx = true;
  221.                                             RememberIdx = i;
  222.                                         }
  223.                                         if (stoi(residx) == idx0) {
  224.                                             if (Residx and isdigit(cur[i]) && !isdigit(cur[i + 1])) {
  225.                                                 rescount += cur[i];
  226.                                                 Result = true;
  227.  
  228.                                             }
  229.                                             if (Result) {
  230.                                                 count0 = stoi(rescount) + 1;
  231.                                                 rescount = to_string(count0);
  232.                                                 res = cur.substr(0, RememberIdx)+residx + " " + rescount + cur.substr(i, length - i);
  233.                                                 break;
  234.                                             }
  235.                                         }
  236.                                     }
  237.                                 }
  238.                                 else { // нашли два слова и строка пустая
  239.                                     BD* currentBD2 = Start;
  240.                                     while (currentBD2 != nullptr) {
  241.                                         if (currentBD2->idx == stoi(word1)) currentBD2->str = to_string(currentBD2->idx) + " 1";
  242.                                         currentBD2 = currentBD2->next;
  243.                                     }
  244.                                 }
  245.                                 currentBD = currentBD->next;
  246.                             }
  247.                         }
  248.                         currentFD2 = currentFD2->next;
  249.                     }
  250.                 }
  251.                 else {
  252.                     int Last = LastIdx();
  253.                     BD* Temp0 = new BD;
  254.                     BD* Temp1 = new BD;
  255.                     Temp0->idx = 1;
  256.                     Temp0->str = "1 2 1";
  257.                     Temp0->next = Temp1;
  258.                     Temp1->idx = 2;
  259.                     Temp1->str = "";
  260.                     Temp1->next = nullptr;
  261.                     Start = Temp0;
  262.                 }
  263.                 currentFD1 = currentFD1->next;
  264.             }
  265.         }
  266.     }
  267. }
  268. int ListB::LastIdx()
  269. {
  270.     int Last = 0;
  271.     BD* t = Start;
  272.     while (t != 0) {
  273.         Last = t->idx;
  274.         t = t->next;
  275.     }
  276.     return Last;
  277. }
  278. void ListB::PrintListB(fstream& BDtext)
  279. {
  280.     BD* t = Start;
  281.     while (t != 0) {
  282.         BDtext << t->idx << " " << t->str << endl;
  283.         t = t->next;
  284.     }
  285. }
  286.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement