Advertisement
Guest User

LAB_5

a guest
Feb 16th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. ////////////////////////////////////////////////////////////////////
  9. /*MANIPULATOR*/
  10. ///////////////////////////////////////////////////////////////////
  11. class mm_t {};
  12. constexpr mm_t myManipulator;
  13.  
  14. struct toggled_ostream
  15. {
  16.     std::ostream& os;
  17. };
  18.  
  19. inline toggled_ostream operator << (std::ostream& os, mm_t)
  20. {
  21.     return { os };
  22. }
  23.  
  24. template <typename T>
  25. std::ostream& operator << (toggled_ostream tos, const T& v)
  26. {
  27.     return tos.os << v;
  28. }
  29.  
  30. std::ostream& operator << (toggled_ostream tos, char v)
  31. {
  32.     cout << "My_manipulator";
  33.     return tos.os << v;
  34. }
  35.  
  36. struct Program {
  37.     string name;
  38.     float version;
  39. };
  40.  
  41. struct dt {
  42.     Program str;
  43.     dt *sled;
  44. };
  45.  
  46.  
  47. class Queue {
  48. private:
  49.     Program queue[100];
  50.     int q_size = 100;
  51.     int start;
  52.     int end;
  53.     int elems;
  54. public:
  55.     Queue() {
  56.         start = end = 0;
  57.         elems = 0;
  58.     };
  59.  
  60.     ~Queue() {};
  61.  
  62.     void push(Program a) {
  63.         if (elems == 0) {
  64.             queue[start] = a;
  65.             end++;
  66.             if (end >= q_size) {
  67.                 end = 0;
  68.             }
  69.         } else {
  70.             if (start == end) {
  71.                 cout << "Too much pushed elems, clear first!";
  72.                 return;
  73.             }
  74.             queue[end] = a;
  75.             end++;
  76.             if (end >= q_size) {
  77.                 end = 0;
  78.             }
  79.         }
  80.         elems++;
  81.     }
  82.  
  83.     Program pop() {
  84.         Program val;
  85.         if (start - 1 >= 0) {
  86.             start++;
  87.             if (start >= q_size) {
  88.                 start = 0;
  89.             }
  90.             val = queue[start - 1];
  91.         } else {
  92.             val = queue[0];
  93.             start++;
  94.         }
  95.         elems--;
  96.         return val;
  97.     }
  98. };
  99.  
  100. class List {
  101. protected:
  102.     int listElems;
  103. public:
  104.     dt *Head = NULL;
  105.     dt *Ending = NULL;
  106.  
  107.     void add(Program c) {
  108.         dt *temp = new dt;
  109.         temp->str = c;
  110.         temp->sled = NULL;
  111.         if (Head != NULL) {
  112.             Ending->sled = temp;
  113.             Ending = temp;
  114.         } else {
  115.             Head = Ending = temp;
  116.         }
  117.         listElems++;
  118.     }
  119.  
  120.     virtual void show() {
  121.         cout << "Вывод содержимого очереди: " << endl;
  122.         dt *temp = Head;
  123.         while (temp != NULL) {
  124.             cout << temp->str.name << " " << temp->str.version << endl;
  125.             temp = temp->sled;
  126.         }
  127.     }
  128.  
  129.     void remove() {
  130.         if (Head != NULL) {
  131.             dt *temp = Head;
  132.             Head = Head->sled;
  133.             delete temp;
  134.         }
  135.         listElems--;
  136.     }
  137.  
  138.     void remove_all() {
  139.         if (Head != NULL) {
  140.             dt *temp = Head;
  141.             while (temp != NULL) {
  142.                 temp = Head->sled;
  143.                 delete Head;
  144.                 Head = temp;
  145.             }
  146.         }
  147.         listElems = 0;
  148.     }
  149.  
  150.     virtual void search(string sh) {
  151.         dt *temp = Head;
  152.         bool found = false;
  153.         while (temp != NULL) {
  154.             if (temp->str.name == sh) {
  155.                 cout << "Элемент <<" << temp->str.name << ">> версии " << temp->str.version << " найден в очереди."
  156.                      << endl;
  157.                 found = true;
  158.             }
  159.             temp = temp->sled;
  160.         }
  161.         if (!found)
  162.             cout << "Элемент <<" << sh << ">> не найден в очереди." << endl;
  163.     }
  164.  
  165.     virtual void obrabotka() {
  166.         Program r;
  167.         dt *temp1 = Head;
  168.         while (temp1 != NULL) {
  169.             if (temp1->str.version > 10.0) {
  170.                 cout << temp1->str.name << " " << temp1->str.version << endl;
  171.             }
  172.             temp1 = temp1->sled;
  173.         }
  174.         cout << "Обработка призведена!" << endl;
  175.     }
  176.  
  177.     void operator+(Program b) {
  178.         add(b);
  179.     }
  180.  
  181.     void operator-(int k) {
  182.         for (int i = 0; i < k; i++)
  183.             remove();
  184.     }
  185.  
  186.     void operator--(int) {
  187.         remove_all();
  188.     }
  189.  
  190.     friend std::ostream &operator<<(std::ostream &out, const List &superUpgradeList);
  191. };
  192.  
  193. std::ostream &operator<<(std::ostream &out, const List &List) {
  194.     out << "Перегрузка <<\n";
  195.     dt *temp = List.Head;
  196.     while (temp != NULL) {
  197.         out << temp->str.name << " " << temp->str.version << endl;
  198.         temp = temp->sled;
  199.     }
  200.     out << myManipulator << 'a';
  201.     return out;
  202. }
  203.  
  204.  
  205. class UpgradeShowList : public List {
  206. public:
  207.     UpgradeShowList() : List() {}
  208.  
  209.     void show() {
  210.         cout << "Всего элементов " << listElems << endl;
  211.         cout << "Вывод содержимого очереди: " << endl;
  212.         dt *temp = Head;
  213.         while (temp != NULL) {
  214.             cout << temp->str.name << " " << temp->str.version << endl;
  215.             temp = temp->sled;
  216.         }
  217.     }
  218. };
  219.  
  220. class MasterWelcome {
  221. public:
  222.     MasterWelcome() {};
  223.  
  224.     ~MasterWelcome() {};
  225.  
  226.     void printHello() {
  227.         cout << "HELLO!" << endl;
  228.     }
  229. };
  230.  
  231. class SuperUpgradeList : public UpgradeShowList, public MasterWelcome {
  232. public:
  233.     SuperUpgradeList() : UpgradeShowList(), MasterWelcome() {}
  234.  
  235.     void megaShow() {
  236.         this->printHello();
  237.         this->show();
  238.     }
  239.  
  240.     string megaFileShow() const {
  241.         string text;
  242.         text = "Всего элементов " + to_string(listElems) + "\n";
  243.         text = text + "Вывод содержимого очереди: " + "\n";
  244.         dt *temp = Head;
  245.         while (temp != NULL) {
  246.             text += temp->str.name + " " + to_string(temp->str.version) + "\n";
  247.             temp = temp->sled;
  248.         }
  249.         return text;
  250.     }
  251.  
  252.     string searchWithFile(string sh) {
  253.         string text = "";
  254.         dt *temp = Head;
  255.         bool found = false;
  256.         while (temp != NULL) {
  257.             if (temp->str.name == sh) {
  258.                 text += "Элемент <<" + temp->str.name + ">> версии " + to_string(temp->str.version) +
  259.                         " найден в очереди."
  260.                         + "\n";
  261.                 found = true;
  262.             }
  263.             temp = temp->sled;
  264.         }
  265.         if (!found)
  266.             text += "Элемент <<" + sh + ">> не найден в очереди." + "\n";
  267.         return text;
  268.     }
  269.  
  270.     int menu() {
  271.         float c;
  272.         string i, v;
  273.         menu:
  274.         cout << "1) Вывод содержимого очереди." << endl;
  275.         cout << "2) Добавить элемент в очередь." << endl;
  276.         cout << "3) Удалить элемент из очереди." << endl;
  277.         cout << "4) Поиск элемента." << endl;
  278.         cout << "5) Обработка." << endl;
  279.         cout << "6) Выход из программы." << endl;
  280.         cout << "Выбрать:";
  281.         cin >> i;
  282.         cout << endl;
  283.         if (i == "1") {
  284.             this->megaShow();
  285.             cout << endl;
  286.             goto menu;
  287.         } else if (i == "2") {
  288.             dobav:
  289.             cout << "Введите программу";
  290.             cin >> v;
  291.             cout << "Введите версию";
  292.             cin >> c;
  293.             if (v.length() <= 15) {
  294.                 this->operator+(Program{v, c});
  295.                 cout << endl;
  296.                 goto menu;
  297.             } else {
  298.                 cout << "Превышена максимальная длина строки!" << endl;
  299.                 goto dobav;
  300.             }
  301.         } else if (i == "3") {
  302.             this->operator-(1);
  303.             goto menu;
  304.         } else if (i == "4") {
  305.             dobav2:
  306.             cout << "Введите слово для поиска: ";
  307.             cin >> v;
  308.             if (v.length() <= 10) {
  309.                 this->search(v);
  310.                 cout << endl;
  311.                 goto menu;
  312.             } else {
  313.                 cout << "Превышена максимальная длина строки!" << endl;
  314.                 goto dobav2;
  315.             }
  316.         } else if (i == "5") {
  317.             this->obrabotka();
  318.             goto menu;
  319.         } else if (i == "6") {
  320.             this->remove_all();
  321.             return 0;
  322.         } else {
  323.             cout << "Некорректный ввод" << endl << endl;
  324.             goto menu;
  325.         }
  326.     }
  327.  
  328.     string obrabotkaWithFile() {
  329.         string text = "";
  330.         dt *temp1 = Head;
  331.         while (temp1 != NULL) {
  332.             if (temp1->str.version > 10.0) {
  333.                 text = text + temp1->str.name + " " + to_string(temp1->str.version) + "\n";
  334.             }
  335.             temp1 = temp1->sled;
  336.         }
  337.         text = text + "Обработка призведена!" + "\n";
  338.         return text;
  339.     }
  340.  
  341.     void menuWithFile(string path) {
  342.         ifstream in("D:\\Programming\\TheoryOfFormalLanguage\\Lexa\\input.txt");
  343.         fstream out("D:\\Programming\\TheoryOfFormalLanguage\\Lexa\\output.txt");
  344.         if (in.is_open() && out.is_open()) {
  345.             float c;
  346.             string i, v;
  347.             menu:
  348.             out << "1) Вывод содержимого очереди." << endl;
  349.             out << "2) Добавить элемент в очередь." << endl;
  350.             out << "3) Удалить элемент из очереди." << endl;
  351.             out << "4) Поиск элемента." << endl;
  352.             out << "5) Обработка." << endl;
  353.             out << "6) Выход из программы." << endl;
  354.             out << "Выбрать:";
  355.             in >> i;
  356.             out << myManipulator << '\n';
  357.             if (i == "1") {
  358.                 out << this->megaFileShow();
  359.                 out << endl;
  360.                 goto menu;
  361.             } else if (i == "2") {
  362.                 dobav:
  363.                 //cout << "Введите программу";
  364.                 in >> v;
  365.                 //cout << "Введите версию";
  366.                 in >> c;
  367.                 if (v.length() <= 15) {
  368.                     this->operator+(Program{v, c});
  369.                     cout << endl;
  370.                     goto menu;
  371.                 } else {
  372.                     out << "Превышена максимальная длина строки!" << endl;
  373.                     goto dobav;
  374.                 }
  375.             } else if (i == "3") {
  376.                 this->operator-(1);
  377.                 goto menu;
  378.             } else if (i == "4") {
  379.                 dobav2:
  380.                 //cout << "Введите слово для поиска: ";
  381.                 in >> v;
  382.                 if (v.length() <= 10) {
  383.                     out << this->searchWithFile(v);
  384.                     cout << endl;
  385.                     goto menu;
  386.                 } else {
  387.                     //cout << "Превышена максимальная длина строки!" << endl;
  388.                     goto dobav2;
  389.                 }
  390.             } else if (i == "5") {
  391.                 out << this->obrabotkaWithFile();
  392.                 goto menu;
  393.             } else if (i == "6") {
  394.                 in.close();
  395.                 out.close();
  396.                 this->remove_all();
  397.             } else {
  398.                 cout << "Некорректный ввод" << endl << endl;
  399.                 goto menu;
  400.             }
  401.         } else {
  402.             cout << "error";
  403.         }
  404.     }
  405.  
  406. };
  407.  
  408.  
  409.  
  410.  
  411. int main() {
  412.     setlocale(LC_ALL, "Rus");
  413.     system("color F0");
  414.     string v;
  415.     float c;
  416.     string i;
  417.     List e;
  418.     Queue q;
  419.     e.add(Program{"Steam", 2.23});
  420.     e.add(Program{"Discord", 3.22});
  421.     e.add(Program{"Thunder", 3});
  422.     e.add(Program{"Skype", 228});
  423.     e.add(Program{"Steam", 6.54});
  424.     e.add(Program{"Steam", 2.21});
  425.     menu:
  426.     cout << "1) Вывод содержимого очереди." << endl;
  427.     cout << "2) Добавить элемент в очередь." << endl;
  428.     cout << "3) Удалить элемент из очереди." << endl;
  429.     cout << "4) Поиск элемента." << endl;
  430.     cout << "5) Обработка." << endl;
  431.     cout << "6) Тестирование нового класса." << endl;
  432.     cout << "7) Тестирование нового класса с помощью файла." << endl;
  433.     cout << "8) Перегрузка оператора вывода." << endl;
  434.     cout << "9) Демонстрация манипулятора" << endl;
  435.     cout << "Выбрать:";
  436.     cin >> i;
  437.     cout << endl;
  438.     if (i == "1") {
  439.         e.show();
  440.         cout << endl;
  441.         goto menu;
  442.     } else if (i == "2") {
  443.         dobav:
  444.         cout << "Введите название программы";
  445.         cin >> v;
  446.         cout << "Введите версию";
  447.         cin >> c;
  448.         if (v.length() <= 15) {
  449.             e + Program{v, c};
  450.             cout << endl;
  451.             goto menu;
  452.         } else {
  453.             cout << "Превышена максимальная длина строки!" << endl;
  454.             goto dobav;
  455.         }
  456.     } else if (i == "3") {
  457.         e - 1;
  458.         goto menu;
  459.     } else if (i == "4") {
  460.         dobav2:
  461.         cout << "Введите слово для поиска: ";
  462.         cin >> v;
  463.         if (v.length() <= 10) {
  464.             e.search(v);
  465.             cout << endl;
  466.             goto menu;
  467.         } else {
  468.             cout << "Превышена максимальная длина строки!" << endl;
  469.             goto dobav2;
  470.         }
  471.     } else if (i == "5") {
  472.         e.obrabotka();
  473.         goto menu;
  474.     } else if (i == "6") {
  475.         SuperUpgradeList list;
  476.         list.menu();
  477.         return 0;
  478.     } else if (i == "7") {
  479.         SuperUpgradeList list;
  480.         string file = "D:\\Programming\\TheoryOfFormalLanguage\\Lexa\\";
  481.         //cin >> file;
  482.         list.menuWithFile(file);
  483.     } else if (i == "8") {
  484.         cout << e;
  485.     } else if (i == "9") {
  486.         cout << myManipulator << 'a';
  487.     } else {
  488.         cout << "Некорректный ввод" << endl << endl;
  489.         goto menu;
  490.     }
  491. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement