Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- bool isInteger(string s);
- int toInteger(string s);
- void inputQueue(queue<int>& q);
- void outputQueue(queue<int> q);
- bool containsElement(queue<int> q, int element);
- bool isSubset(queue<int> a, queue<int> b); // A is subset of B
- queue<int> intersectQueues(queue<int> p, queue<int> q);
- queue<int> differencePQ(queue<int> p, queue<int> q);
- queue<int> complementPQ(queue<int> p, queue<int> q);
- int main()
- {
- queue<int> p, q, a, b, c;
- cout << "Enter P queue: ";
- inputQueue(p);
- cout << "Enter Q queue: ";
- inputQueue(q);
- a = intersectQueues(p, q);
- cout << "P ^ Q: ";
- outputQueue(a);
- cout << endl;
- b = differencePQ(p, q);
- cout << "P - Q: ";
- outputQueue(b);
- cout << endl;
- c = complementPQ(p, q);
- cout << "Compliment P, Q: ";
- outputQueue(c);
- cout << endl;
- return 0;
- }
- bool isInteger(string s)
- {
- bool state = true;
- for (int i = 0; state && i < s.length(); i++)
- {
- if (i == 0 && s[i] == '-' && s.length() > 1)
- {
- continue;
- }
- if (!isdigit(s[i]))
- {
- state = false;
- }
- }
- return state;
- }
- int toInteger(string s)
- {
- int num = 0;
- for (int i = 0; i < s.length(); i++)
- {
- if (i == 0 && s[i] == '-')
- {
- continue;
- }
- num = 10 * num + (s[i] - '0');
- }
- return (s[0] == '-') ? -num : num;
- }
- void inputQueue(queue<int>& q)
- {
- string input;
- while(1)
- {
- cin >> input;
- if (isInteger(input))
- {
- q.push(toInteger(input));
- }
- else
- {
- return;
- }
- }
- }
- void outputQueue(queue<int> q)
- {
- if (q.empty())
- {
- cout << "Queue is empty!";
- return;
- }
- while (!q.empty())
- {
- cout << q.front() << " ";
- q.pop();
- }
- }
- bool containsElement(queue<int> q, int element)
- {
- while(!q.empty())
- {
- if (q.front() == element)
- {
- return true;
- }
- q.pop();
- }
- return false;
- }
- // A is subset of B
- bool isSubset(queue<int> a, queue<int> b)
- {
- while(!a.empty())
- {
- if (!containsElement(b, a.front()))
- {
- return false;
- }
- a.pop();
- }
- return true;
- }
- queue<int> intersectQueues(queue<int> p, queue<int> q)
- {
- queue<int> resultq;
- while (!p.empty())
- {
- if (containsElement(q, p.front()))
- {
- resultq.push(p.front());
- }
- p.pop();
- }
- return resultq;
- }
- queue<int> differencePQ(queue<int> p, queue<int> q)
- {
- queue<int> resultq;
- while (!p.empty())
- {
- if (!containsElement(q, p.front()))
- {
- resultq.push(p.front());
- }
- p.pop();
- }
- return resultq;
- }
- queue<int> complementPQ(queue<int> p, queue<int> q)
- {
- queue<int> resultq;
- if (isSubset(p, q)) // p is subset of q
- {
- while (!q.empty())
- {
- if (!containsElement(p, q.front()))
- {
- resultq.push(q.front());
- }
- q.pop();
- }
- }
- else if (isSubset(q, p)) // q is subset of p
- {
- while (!p.empty())
- {
- if (!containsElement(q, p.front()))
- {
- resultq.push(p.front());
- }
- p.pop();
- }
- }
- return resultq;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement