Name Surname Score
1. Sam 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 42
Create a C++ program that has 3 Stacks.
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.
#include <iostream>
#include <stack>
using namespace std;
class Student{
public:
string name, surname;
int marks;
Student(){}
Student(string name, string surname, int marks){
this->name = name;
this->surname = surname;
this->marks = marks;
}
void display(){
cout<<"Name: "<<this->name<<endl;
cout<<"Surname: "<<this->surname<<endl;
cout<<"Marks: "<<this->marks<<endl<<endl;
}
};
int main(){
stack<Student> stack1, stack2, stack3;
stack1.push(Student("Sam", "Williams", 60));
stack1.push(Student("John", "Phoenix", 85));
stack1.push(Student("Simon", "Johnson", 75));
stack1.push(Student("Sarah", "Khosa", 81));
stack1.push(Student("Mat", "Jackson", 38));
stack1.push(Student("Nick", "Roberts", 26));
stack1.push(Student("Isaac", "Wayne", 74));
stack1.push(Student("Anna", "Mishima", 34));
stack1.push(Student("Daniel", "Rose", 64));
stack1.push(Student("Aaron", "Black", 83));
stack1.push(Student("Jack", "Mohamed", 27));
stack1.push(Student("Kathrine", "Bruckner", 42));
Student student;
cout<<"Stack 1 contents\n";
while(!stack1.empty()){
student = stack1.top();
student.display();
stack3.push(student);
stack1.pop();
}
while(!stack3.empty()){
stack1.push(stack3.top());
stack3.pop();
}
while(!stack1.empty()){
student = stack1.top();
if(student.surname[0] == 'R' || student.surname[0] == 'J' || student.surname[0] == 'M'){
stack2.push(student);
}
stack3.push(student);
stack1.pop();
}
cout<<"Stack 2 contents\n";
while(!stack2.empty()){
student = stack2.top();
student.display();
stack2.pop();
}
return 0;
}
Comments
Leave a comment