Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- #include <string>
- template <typename T>
- class Queue {
- private:
- class Node {
- private:
- T value;
- Node* next;
- public:
- Node(const T& val) : value(val), next(0) {}
- T getValue() const {
- return value;
- }
- Node* getNext() const {
- return next;
- }
- void setNext(Node* node) {
- next = node;
- }
- };
- Node* pHead;
- Node* pTail;
- Queue(const Queue& another) {}
- Queue& operator = (const Queue& another) {}
- public:
- Queue() : pHead(0), pTail(0) {}
- ~Queue() {
- while (pHead) {
- pTail = pHead->getNext();
- delete pHead;
- pHead = pTail;
- }
- }
- bool empty() const {
- return !pHead;
- }
- void push(const T& value) {
- Node* pNode = new Node(value);//value=value, next=0
- if (!pHead)//указ первый элемент 0
- pHead = pNode;
- else
- pTail->setNext(pNode);
- pTail = pNode;
- }
- void pop() { //удаляем первый зашедший
- if (pHead && pHead->next)
- {
- Node* pNext = pHead->getNext();
- delete pHead;
- pHead = pNext;
- }
- }
- /*Node* front(){ //first elem
- return pHead;
- }*/
- T shift() {
- if (empty())
- throw std::runtime_error("Empty queue!");
- T ret = pHead->getValue();
- Node* pNext = pHead->getNext();
- delete pHead;
- pHead = pNext;
- return ret;
- }
- void print() {
- Node* n = pHead;
- while (n)
- {
- std::cout << n->getValue() << " ";
- n = n->getNext();
- }
- std::cout << std::endl;
- }
- bool perfect(int num) {
- int sum_del = 0;
- for (int j = 1; j <= num / 2; j++) {
- if (num % j == 0)
- sum_del += j;
- }
- if (sum_del == num)
- {
- return true;
- }
- return false;
- }
- int brute_force() {
- Node* n = pHead;
- int sum = 0;
- while (n)
- {
- int number=n->getValue();
- if (perfect(number))
- {
- sum++;
- }
- n = n->getNext();
- }
- return sum;
- }
- void add(int k) {
- int count = 0;
- Node* n = pHead;
- while (n)
- {
- count++;
- n = n->getNext();
- }
- if ((count + 1 <= k) && (pHead))
- {
- Node* pNode = new Node(pHead->getValue());//value=value, next=0
- pTail->setNext(pNode);
- pTail = pNode;
- }
- else
- {
- std::cout << "Queue overflow or empty" << std::endl;
- }
- }
- bool check(int A) {
- Node* n = pHead;
- int num;
- while (n)
- {
- num=n->getValue();
- if (num == A)
- {
- return true;
- }
- n = n->getNext();
- }
- return false;
- }
- };
- int main() {
- /*Queue<std::string> queue;
- queue.push("Any");
- queue.push("many");
- queue.push("money");
- queue.push("more...");
- while (!queue.empty())
- std::cout << queue.shift() << " ";
- std::cout << std::endl;*/
- setlocale(LC_ALL, "Russian");
- long n;
- char ex = 'n';
- do {
- std::cout << "Введите номер задания: " << std::endl;
- char task;
- /*cin.ignore(100, '\n');*/
- std::cin >> task;
- switch (task)
- {
- case '1': {
- Queue<int> Q;
- float num;
- std::cout << "Enter numbers. If you want to stop it enter 0.5.";
- std::cin >> num;
- while (num!=0.5) {
- Q.push(num);
- std::cin >> num;
- }
- Queue<int> P;
- while (!Q.empty()) {
- int new_num = Q.shift();
- if (new_num % 3 == 0)
- {
- P.push(new_num);
- }
- }
- std::cout << "P result: ";
- P.print();
- break;
- }
- case '2': {
- Queue<int> Q;
- float num;
- std::cout << "Enter numbers. If you want to stop it enter 0.5.";
- std::cin >> num;
- while (num != 0.5) {
- Q.push(num);
- std::cin >> num;
- }
- std::cout << "The sum of perfect numbers is " << Q.brute_force() << std::endl;
- break;
- }
- case '3': {
- Queue<int> Q;
- float num;
- std::cout << "Enter numbers. If you want to stop it enter 0.5.";
- std::cin >> num;
- while (num != 0.5) {
- Q.push(num);
- std::cin >> num;
- }
- Q.add(10);
- std::cout << "Your result: ";
- Q.print();
- break;
- }
- case '4': {
- Queue<int> Q;
- float num;
- std::cout << "Enter numbers. If you want to stop it enter 0.5.";
- std::cin >> num;
- while (num != 0.5) {
- Q.push(num);
- std::cin >> num;
- }
- bool flag = 0;
- flag = Q.check(15);
- if (flag)
- {
- std::cout << "Содержит." << std::endl;
- }
- else
- {
- std::cout << "Не содержит." << std::endl;
- }
- break;
- }
- default: {std::cout << "Нет такой задачи.\n"; } break;
- }
- std::cout << "Если вы хотите выйти, нажмите \'y\', в противном случае-любую другую клавишу" << std::endl;
- std::cin.ignore(100, '\n');
- std::cin >> ex;
- } while (ex != 'y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement