Mikhail-Podbolotov

Untitled

May 1st, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. #include "library.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. List::List()
  8. {
  9.     Start = nullptr;
  10.     End = nullptr;
  11. }
  12.  
  13. List::~List()
  14. {
  15.     Reserv* prom;
  16.     Reserv* t = Start;
  17.     while (t != 0) {
  18.         prom = t;
  19.         t = t->next;
  20.         delete prom;
  21.     }
  22. }
  23.  
  24. void List::Print(std::ofstream& file2)
  25. {
  26.     Reserv* t = Start;
  27.     while (t != 0) {
  28.         file2 << t->word << " Count: " << t->count << endl;
  29.         t = t->next;
  30.     }
  31. }
  32.  
  33.  
  34.  
  35. void List::Add(string cur)
  36. {
  37.     Reserv* Temp = new Reserv;
  38.     Temp->word = cur;
  39.     Temp->next = nullptr;
  40.     if (Start == nullptr) {
  41.         Start = End = Temp;
  42.     }
  43.     else {
  44.         End->next = Temp;
  45.         End = Temp;
  46.     }
  47. }
  48.  
  49. void List::Read(std::ifstream& file1)
  50. {
  51.     string cur;
  52.     while (!file1.eof()) {
  53.         getline(file1, cur);
  54.         Reserv* Temp = find(cur);
  55.         if (Temp == nullptr) this->Add(cur);
  56.     }
  57. }
  58.  
  59. Reserv* List::find(string cur)
  60. {
  61.     Reserv* t = Start;
  62.     while (t != 0) {
  63.         if (t->word == cur) {
  64.             return t;
  65.         }
  66.         t = t->next;
  67.     }
  68.     return nullptr;
  69. }
  70.  
  71. void List::Count(string* Array, int n)
  72. {
  73.     Reserv* t = Start;
  74.     while (t != 0) {
  75.  
  76.         for (int i = 0; i < n; ++i) {
  77.             //cout << Array[i] << " " << t->word << endl;
  78.             if (t->word == Array[i]) {
  79.                 t->count++;
  80.             }
  81.         }
  82.         t = t->next;
  83.     }
  84. }
  85.  
  86. List List::InsertSort()
  87. {
  88.     List res;
  89.     Reserv* t = Start;
  90.     while (t != nullptr) {
  91.         Reserv* NewElement = new Reserv;
  92.         NewElement->count = t->count;
  93.         NewElement->word = t->word;
  94.  
  95.         if (res.Start == nullptr) {
  96.             res.Start = res.End = NewElement;
  97.         }
  98.         else {
  99.             if (res.Start->count > NewElement->count) {
  100.                 NewElement->next = res.Start;
  101.                 res.Start = NewElement;
  102.             }
  103.             else {
  104.                 Reserv* i = res.Start->next;
  105.                 Reserv* prev = nullptr; // i - 1 index
  106.                 while (i != nullptr) {
  107.                     if (i->count > NewElement->count) {
  108.                         NewElement->next = i;
  109.                         if (prev != nullptr) {
  110.                             prev->next = NewElement;
  111.                         }
  112.                         break;
  113.                     }
  114.                     prev = i;
  115.                     i = i->next;
  116.                 }
  117.                 if (i == nullptr) {
  118.                     res.End->next = NewElement;
  119.                     res.End = NewElement;
  120.                 }
  121.             }
  122.         }
  123.         t = t->next;
  124.     }
  125.     return res;
  126. }
  127.  
  128. void List::BubleSort()
  129. {
  130.     Reserv* cur = Start;
  131.     while (cur != nullptr) {
  132.         Reserv* min = cur;
  133.         Reserv* temp = cur->next;
  134.         while (temp != nullptr) {
  135.             if (temp->count < min->count) {
  136.                 min = temp;
  137.             }
  138.             temp = temp->next;
  139.         }
  140.         swap(cur->word, min->word);
  141.         swap(cur->count, min->count);
  142.         cur = cur->next;
  143.     }
  144. }
  145.  
  146. string* TextDivision(string input, int wordCount)
  147. {
  148.     string* words = new string[wordCount];
  149.     int wordIndex = 0;
  150.     int charIndex = 0;
  151.     while (charIndex < input.length()) {
  152.         while (isspace(input[charIndex]) or input[charIndex]==',' or input[charIndex] == ':' or input[charIndex] == ';') {
  153.             charIndex++;
  154.         }
  155.         //cout << charIndex << " " << endl;
  156.         string cur = "";
  157.         while (charIndex < input.length() and !isspace(input[charIndex]) and input[charIndex] != ',' and input[charIndex] != ':' and input[charIndex] != ';') {
  158.             cur += input[charIndex];
  159.             charIndex++;
  160.         }
  161.         //cout << cur << endl;
  162.         words[wordIndex] = cur;
  163.         wordIndex++;
  164.     }
  165.     return words;
  166. }
  167.  
  168. string ReadText(std::ifstream& file3)
  169. {
  170.     string res= "";
  171.     string cur;
  172.     while (!file3.eof()) {
  173.         getline(file3, cur);
  174.         res += " " + cur;
  175.     }
  176.     return res;
  177. }
  178.  
  179. int CountWord(string input)
  180. {
  181.     int wordCount = 0;
  182.     for (int i = 0; i < input.length(); i++) {
  183.         if (isspace(input[i]) or input[i] == ',' or input[i] == ':' or input[i] == ';') {
  184.             wordCount++;
  185.             while (isspace(input[i]) or input[i] == ',' or input[i] == ':' or input[i] == ';') {
  186.                 i++;
  187.             }
  188.             //cout << i << " " << input[i] << endl;
  189.         }
  190.         //cout << i << " " << input[i] << endl;
  191.     }
  192.     return wordCount;
  193. }
Add Comment
Please, Sign In to add comment