Advertisement
vencinachev

K2-Zad1

May 31st, 2021
945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. bool isInteger(string s);
  8. int toInteger(string s);
  9. void inputQueue(queue<int>& q);
  10. void outputQueue(queue<int> q);
  11.  
  12.  
  13. // Ex.1 A
  14. bool isSorted(queue<int> q);
  15.  
  16. // // Ex.1 B
  17. queue<int> sortedMerge(queue<int> q1, queue<int> q2);
  18.  
  19.  
  20.  
  21. int main()
  22. {
  23.     queue<int> a, b, c;
  24.     cout << "Enter first queue elements: ";
  25.     inputQueue(a);
  26.     bool isSortedA = isSorted(a);
  27.     cout << "Enter second queue elements: ";
  28.     inputQueue(b);
  29.     bool isSortedB = isSorted(b);
  30.     cout << boolalpha << "First queue sorted: " << isSortedA << endl;
  31.     cout << boolalpha << "Second queue sorted: " << isSortedB << endl;
  32.     if (isSortedA && isSortedB)
  33.     {
  34.         cout << "Sorted merge: ";
  35.         c = sortedMerge(a, b);
  36.         outputQueue(c);
  37.     }
  38.     return 0;
  39. }
  40.  
  41. bool isInteger(string s)
  42. {
  43.     bool state = true;
  44.     for (int i = 0; state && i < s.length(); i++)
  45.     {
  46.         if (i == 0 && s[i] == '-' &&  s.length() > 1)
  47.         {
  48.             continue;
  49.         }
  50.         if (!isdigit(s[i]))
  51.         {
  52.             state = false;
  53.         }
  54.     }
  55.  
  56.     return state;
  57. }
  58.  
  59. int toInteger(string s)
  60. {
  61.     int num = 0;
  62.     for (int i = 0; i < s.length(); i++)
  63.     {
  64.         if (i == 0 && s[i] == '-')
  65.         {
  66.             continue;
  67.         }
  68.         num = 10 * num + (s[i] - '0');
  69.     }
  70.     return (s[0] == '-') ? -num : num;
  71. }
  72.  
  73. void inputQueue(queue<int>& q)
  74. {
  75.     string input;
  76.     while(1)
  77.     {
  78.         cin >> input;
  79.  
  80.         if (isInteger(input))
  81.         {
  82.             q.push(toInteger(input));
  83.         }
  84.         else
  85.         {
  86.             return;
  87.         }
  88.     }
  89. }
  90.  
  91. void outputQueue(queue<int> q)
  92. {
  93.     if (q.empty())
  94.     {
  95.         cout << "Queue is empty!";
  96.         return;
  97.     }
  98.     while (!q.empty())
  99.     {
  100.         cout << q.front() << " ";
  101.         q.pop();
  102.     }
  103. }
  104.  
  105. bool isSorted(queue<int> q)
  106. {
  107.     if (q.empty())
  108.     {
  109.         return false;
  110.     }
  111.     int a = q.front();
  112.     q.pop();
  113.     while(!q.empty())
  114.     {
  115.         int b = q.front();
  116.         q.pop();
  117.         if (a > b)
  118.         {
  119.             return false;
  120.         }
  121.         a = b;
  122.     }
  123.     return true;
  124. }
  125.  
  126. queue<int> sortedMerge(queue<int> q1, queue<int> q2)
  127. {
  128.     queue<int> result;
  129.     while(!q1.empty() || !q2.empty())
  130.     {
  131.         if (!q1.empty() && !q2.empty())
  132.         {
  133.             if (q1.front() < q2.front())
  134.             {
  135.                 result.push(q1.front());
  136.                 q1.pop();
  137.             }
  138.             else
  139.             {
  140.                 result.push(q2.front());
  141.                 q2.pop();
  142.             }
  143.         }
  144.         else if (!q1.empty())
  145.         {
  146.             result.push(q1.front());
  147.             q1.pop();
  148.         }
  149.         else if (!q2.empty())
  150.         {
  151.             result.push(q2.front());
  152.             q2.pop();
  153.         }
  154.     }
  155.     return result;
  156. }
  157.  
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement