Insert, into the first stack, all the data above (which is the data of student’s names,
surnames and the marks they obtain in a particular assessment)
Display the content of the stack on the screen (console)
Then, remove all the students whose surname starts with the alphabets ‘R’, ‘J’ and
‘M’, from the first stack and insert them into the second Stack.Display the contents of Stack1 and Stack2. Finally, remove all the students whose marks are less than 50 from both Stack1 and Stack2 and insert them into the Third Stack. Display the contents of all the 3 Stacks. (30) 3b. Sort all the stacks in this order: Stack1 in alphabetic order by name Stack2 in alphabetic order by Surname Stack3 in Numeric order (descending) by marks obtainedSam Williams 60 2. John Phoenix 85 3. Simon Johnson 75 4. Sarah Khosa 81 5. Mat Jackson 38 6. Nick Roberts 26 7. Isaac Wayne 74 8. Anna Mishima
34 9. Daniel Rose 64 10. Aaron Black 83 11. Jack Mohamed 27 12. Kathrine Bruckner 4
#include <iostream>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
int main() {
//The first Stack
stack<string> stack1;
//Inserting into the first stack
stack1.push("1. Sam Williams 60");
stack1.push("2. John Phoenix 85");
stack1.push("3. Simon Johnson 75");
stack1.push("4. Sarah Khosa 81");
stack1.push("5. Mat Jackson 38");
stack1.push("6. Nick Roberts 26");
stack1.push("7. Isaac Wayne 74");
stack1.push("8. Anna Mishima 34");
stack1.push("9. Daniel Rose 64");
stack1.push("10. Aaron Black 83");
stack1.push("11. Jack Mohamed 27");
stack1.push("12. Kathrine Bruckner 42");
//Displaying the first stack
cout << "The first stack before removing students whose surname starts with the alphabets R, J and M\n";
for (stack<string> displayStack = stack1; !displayStack.empty(); displayStack.pop())
cout << displayStack.top() << '\n';
cout << "(" << stack1.size() << " elements)\n";
//Second stack
stack<string> stack2;
stack<string> temp_stack1; //Creating another stack to store students whose surname don't starts with the alphabets R, J and M\n
// cout<<"The first stack after removing students whose surname starts with the alphabets R, J and M\n";
for (stack<string> displayStack = stack1; !displayStack.empty(); displayStack.pop()) {
string item = displayStack.top();
if (item == "3. Simon Johnson 75" || item == "9. Daniel Rose 64" || item == "6. Nick Roberts 26" ||
item == "5. Mat Jackson 38" || item == "11. Jack Mohamed 27" || item == "8. Anna Mishima 34"
) {
stack2.push(item);
// stack1.pop();
} else {
temp_stack1.push(item);
}
stack1.pop();
}
stack1 = temp_stack1;
cout << "The first stack after the removal of students whose surname starts with the alphabets R, J and M\n\n";
for (stack<string> displayStack = stack1; !displayStack.empty(); displayStack.pop())
cout << displayStack.top() << '\n';
cout << "(" << stack1.size() << " elements)\n";
cout << "\n\nThe second stack containing students whose surname starts with the alphabets R, J and M\n";
for (stack<string> displayStack = stack2; !displayStack.empty(); displayStack.pop())
cout << displayStack.top() << '\n';
cout << "(" << stack2.size() << " elements)\n";
stack<string> stack3;
return 0;
}
Output:
The first stack before removing students whose surname starts with the alphabets R, J and M
12. Kathrine Bruckner 42
11. Jack Mohamed 27
10. Aaron Black 83
9. Daniel Rose 64
8. Anna Mishima 34
7. Isaac Wayne 74
6. Nick Roberts 26
5. Mat Jackson 38
4. Sarah Khosa 81
3. Simon Johnson 75
2. John Phoenix 85
1. Sam Williams 60
(12 elements)
The first stack after the removal of students whose surname starts with the alphabets R, J and M
1. Sam Williams 60
2. John Phoenix 85
4. Sarah Khosa 81
7. Isaac Wayne 74
10. Aaron Black 83
12. Kathrine Bruckner 42
(6 elements)
The second stack containing students whose surname starts with the alphabets R, J and M
3. Simon Johnson 75
5. Mat Jackson 38
6. Nick Roberts 26
8. Anna Mishima 34
9. Daniel Rose 64
11. Jack Mohamed 27
(6 elements)
Comments
Leave a comment