Advertisement
Sanlover

Untitled

Oct 16th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. struct cell
  8. {
  9.     char symbol;
  10.     size_t amount;
  11.  
  12.     cell* next;
  13.  
  14.     cell(char symbol) : symbol(symbol), next(nullptr)
  15.     {
  16.         amount = 1;
  17.     }
  18.     cell(char symbol, size_t amount) : symbol(symbol), amount(amount),next(nullptr)
  19.     {
  20.     }
  21. };
  22.  
  23. class queue
  24. {
  25. private:
  26.     cell* first;
  27.     cell* last;
  28.     size_t size;
  29. public:
  30.     queue() : first(nullptr), last(nullptr), size(0) {};
  31.     bool isEmpty() { return size == 0; }
  32.    
  33.     void push(char symbol);
  34.     void push(cell put);
  35.     cell pop();
  36.     void sortByAmount();
  37.    
  38.     friend void print(const queue my);
  39.     friend void fillFromFile(queue& my, const char* fileName);
  40. };
  41.  
  42. void print(queue my)
  43. {
  44.     if(my.isEmpty())
  45.     {
  46.         cout << "Queue is empty" << endl;
  47.         return;
  48.     }
  49.  
  50.  
  51.     size_t counter = 1;
  52.     cell* it = my.first;
  53.     while(it!= nullptr)
  54.     {
  55.         cout << counter++ << ") Symbol = " << it->symbol << " | Amount = " << it->amount << endl;
  56.         it = it->next;
  57.     }
  58. };
  59.  
  60. void fillFromFile(queue& my, const char* fileName)
  61. {
  62.     ifstream input(fileName);
  63.     if (!input.is_open())
  64.     {
  65.         cout << "File is not open" << endl;
  66.         return;
  67.     }
  68.  
  69.     string text;
  70.     while (!input.eof())
  71.     {
  72.         string tmp;
  73.         getline(input, tmp);
  74.         text += tmp;
  75.     }
  76.  
  77.     for (size_t i = 0; i < text.size(); i++)
  78.         if(text[i] != ' ')
  79.             my.push(text[i]);
  80.    
  81.     input.close();
  82. }
  83.  
  84.  
  85. int main()
  86. {
  87.     queue a;
  88.     fillFromFile(a, "file.txt");
  89.     cout << "Before adding" << endl;
  90.     print(a);
  91.  
  92.     a.sortByAmount();
  93.     cout << "After adding" << endl;
  94.     print(a);
  95.  
  96.    
  97.     return 0;
  98. }
  99.  
  100.  
  101.  
  102. void
  103. queue::push(char symbol)
  104. {
  105.    
  106.     if(isEmpty())
  107.     {
  108.         cell* newCell = new cell(symbol);
  109.         first = last = newCell;
  110.         size++;
  111.         return;
  112.     }
  113.  
  114.     cell* it = first;
  115.     while (it != nullptr)
  116.     {
  117.         if (it->symbol == symbol)
  118.         {
  119.             it->amount++;
  120.             return;
  121.         }
  122.         it = it->next;
  123.     }
  124.    
  125.     cell* newCell = new cell(symbol);
  126.     last->next = newCell;
  127.     last = newCell;
  128.     size++;
  129.    
  130. }
  131.  
  132. void
  133. queue::push(cell put)
  134. {
  135.  
  136.     if (isEmpty())
  137.     {
  138.         cell* newCell = new cell(put.symbol,put.amount);
  139.         first = last = newCell;
  140.         size++;
  141.         return;
  142.     }
  143.  
  144.     cell* it = first;
  145.     while (it != nullptr)
  146.     {
  147.         if (it->symbol == put.symbol)
  148.         {
  149.             it->amount += put.amount;
  150.             return;
  151.         }
  152.         it = it->next;
  153.     }
  154.     cell* newCell = new cell(put.symbol,put.amount);
  155.     last->next = newCell;
  156.     last = newCell;
  157.     size++;
  158.  
  159. }
  160.  
  161. cell
  162. queue::pop()
  163. {
  164.     if(isEmpty())
  165.     {
  166.         cout << "Error: Queue is empty " << endl;
  167.         return '-';
  168.     }
  169.  
  170.     if(first != last)
  171.     {
  172.         cell toReturn = *first;
  173.         cell* toDelete = first;
  174.  
  175.         first = first->next;
  176.         delete toDelete;
  177.         size--;
  178.         return toReturn;
  179.     }
  180.     else
  181.     {
  182.         cell toReturn = *first;
  183.         delete first;
  184.         first = last = nullptr;
  185.         size--;
  186.         return toReturn;
  187.     }
  188. }
  189.  
  190. void queue::sortByAmount()
  191. {
  192.     queue temp;
  193.  
  194.     while (!isEmpty())
  195.     {
  196.         cell min = *first;
  197.        
  198.         for(size_t i = 1; i < size; i++)
  199.         {
  200.             cell toCheck = pop();
  201.             if (toCheck.amount < min.amount)
  202.                 min = toCheck;
  203.             push(toCheck);
  204.         }
  205.  
  206.         while(true)
  207.         {
  208.             cell toCheck = pop();
  209.             if(toCheck.amount == min.amount && toCheck.symbol == min.symbol)
  210.             {
  211.                 break;
  212.             }
  213.             push(toCheck);
  214.         }
  215.        
  216.         temp.push(min);
  217.     }
  218.  
  219.     while (!temp.isEmpty())
  220.         push(temp.pop());
  221. }
  222.  
  223.  
  224.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement