Advertisement
VictoriaLodochkina

sacod lab 2_2

Feb 12th, 2021
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <string>
  4.  
  5.  
  6. template <typename T>
  7. class Queue {
  8. private:
  9.     class Node {
  10.     private:
  11.         T value;
  12.         Node* next;
  13.     public:
  14.         Node(const T& val) : value(val), next(0) {}
  15.         T getValue() const {
  16.             return value;
  17.         }
  18.         Node* getNext() const {
  19.             return next;
  20.         }
  21.         void setNext(Node* node) {
  22.             next = node;
  23.         }
  24.     };
  25.  
  26.     Node* pHead;
  27.     Node* pTail;
  28.  
  29.     Queue(const Queue& another) {}
  30.     Queue& operator = (const Queue& another) {}
  31.  
  32. public:
  33.     Queue() : pHead(0), pTail(0) {}
  34.  
  35.     ~Queue() {
  36.         while (pHead) {
  37.             pTail = pHead->getNext();
  38.             delete pHead;
  39.             pHead = pTail;
  40.         }
  41.     }
  42.  
  43.     bool empty() const {
  44.         return !pHead;
  45.     }
  46.  
  47.     void push(const T& value) {
  48.         Node* pNode = new Node(value);//value=value, next=0
  49.  
  50.         if (!pHead)//указ первый элемент 0
  51.             pHead = pNode;
  52.         else
  53.             pTail->setNext(pNode);
  54.         pTail = pNode;
  55.     }
  56.  
  57.     void pop() { //удаляем первый зашедший
  58.         if (pHead && pHead->next)
  59.         {
  60.             Node* pNext = pHead->getNext();
  61.  
  62.             delete pHead;
  63.             pHead = pNext;
  64.         }
  65.     }
  66.  
  67.     /*Node* front(){ //first elem
  68.         return pHead;
  69.     }*/
  70.  
  71.     T shift() {
  72.         if (empty())
  73.             throw std::runtime_error("Empty queue!");
  74.  
  75.         T ret = pHead->getValue();
  76.         Node* pNext = pHead->getNext();
  77.  
  78.         delete pHead;
  79.         pHead = pNext;
  80.  
  81.         return ret;
  82.     }
  83.  
  84.     void print() {
  85.         Node* n = pHead;
  86.         while (n)
  87.         {
  88.             std::cout << n->getValue() << "  ";
  89.             n = n->getNext();
  90.         }
  91.         std::cout << std::endl;
  92.     }
  93.  
  94.     bool perfect(int num) {
  95.         int sum_del = 0;
  96.         for (int j = 1; j <= num / 2; j++) {
  97.             if (num % j == 0)
  98.                 sum_del += j;
  99.         }
  100.  
  101.         if (sum_del == num)
  102.         {
  103.             return true;
  104.         }
  105.         return false;
  106.     }
  107.     int brute_force() {
  108.         Node* n = pHead;
  109.         int sum = 0;
  110.         while (n)
  111.         {
  112.             int number=n->getValue();
  113.             if (perfect(number))
  114.             {
  115.                 sum++;
  116.             }
  117.             n = n->getNext();
  118.         }
  119.         return sum;
  120.     }
  121.  
  122.     void add(int k) {
  123.         int count = 0;
  124.         Node* n = pHead;
  125.         while (n)
  126.         {
  127.             count++;
  128.             n = n->getNext();
  129.         }
  130.         if ((count + 1 <= k) && (pHead))
  131.         {
  132.             Node* pNode = new Node(pHead->getValue());//value=value, next=0
  133.             pTail->setNext(pNode);
  134.             pTail = pNode;
  135.         }
  136.         else
  137.         {
  138.             std::cout << "Queue overflow or empty" << std::endl;
  139.         }
  140.     }
  141.  
  142.     bool check(int A) {
  143.         Node* n = pHead;
  144.         int num;
  145.         while (n)
  146.         {
  147.             num=n->getValue();
  148.             if (num == A)
  149.             {
  150.                 return true;
  151.             }
  152.             n = n->getNext();
  153.         }
  154.         return false;
  155.     }
  156. };
  157.  
  158. int main() {
  159.     /*Queue<std::string> queue;
  160.     queue.push("Any");
  161.     queue.push("many");
  162.     queue.push("money");
  163.     queue.push("more...");
  164.  
  165.     while (!queue.empty())
  166.         std::cout << queue.shift() << " ";
  167.     std::cout << std::endl;*/
  168.     setlocale(LC_ALL, "Russian");
  169.     long n;
  170.     char ex = 'n';
  171.     do {
  172.         std::cout << "Введите номер задания: " << std::endl;
  173.         char task;
  174.         /*cin.ignore(100, '\n');*/
  175.         std::cin >> task;
  176.         switch (task)
  177.         {
  178.         case '1': {
  179.             Queue<int> Q;
  180.             float num;
  181.             std::cout << "Enter numbers. If you want to stop it enter 0.5.";
  182.             std::cin >> num;
  183.             while (num!=0.5) {
  184.                 Q.push(num);
  185.                 std::cin >> num;
  186.             }
  187.             Queue<int> P;
  188.             while (!Q.empty()) {
  189.                 int new_num = Q.shift();
  190.                 if (new_num % 3 == 0)
  191.                 {
  192.                     P.push(new_num);
  193.                 }
  194.             }
  195.             std::cout << "P result: ";
  196.             P.print();
  197.             break;
  198.         }
  199.         case '2': {
  200.             Queue<int> Q;
  201.             float num;
  202.             std::cout << "Enter numbers. If you want to stop it enter 0.5.";
  203.             std::cin >> num;
  204.             while (num != 0.5) {
  205.                 Q.push(num);
  206.                 std::cin >> num;
  207.             }
  208.             std::cout << "The sum of perfect numbers is " << Q.brute_force() << std::endl;
  209.             break;
  210.         }
  211.         case '3': {
  212.             Queue<int> Q;
  213.             float num;
  214.             std::cout << "Enter numbers. If you want to stop it enter 0.5.";
  215.             std::cin >> num;
  216.             while (num != 0.5) {
  217.                 Q.push(num);
  218.                 std::cin >> num;
  219.             }
  220.             Q.add(10);
  221.             std::cout << "Your result: ";
  222.             Q.print();
  223.             break;
  224.         }
  225.         case '4': {
  226.             Queue<int> Q;
  227.             float num;
  228.             std::cout << "Enter numbers. If you want to stop it enter 0.5.";
  229.             std::cin >> num;
  230.             while (num != 0.5) {
  231.                 Q.push(num);
  232.                 std::cin >> num;
  233.             }
  234.             bool flag = 0;
  235.             flag = Q.check(15);
  236.             if (flag)
  237.             {
  238.                 std::cout << "Содержит." << std::endl;
  239.             }
  240.             else
  241.             {
  242.                 std::cout << "Не содержит." << std::endl;
  243.             }
  244.             break;
  245.         }
  246.         default: {std::cout << "Нет такой задачи.\n"; } break;
  247.         }
  248.         std::cout << "Если вы хотите выйти, нажмите \'y\', в противном случае-любую другую клавишу" << std::endl;
  249.         std::cin.ignore(100, '\n');
  250.         std::cin >> ex;
  251.     } while (ex != 'y');
  252.     return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement