Answer to Question #240880 in C++ for Rolly

Question #240880

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. 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)  


1
Expert's answer
2021-09-23T01:50:09-0400
#include <iostream>
#include <string>
#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<<this->name<<" ";
        cout<<this->surname<<" ";
        cout<<this->marks<<endl;
    }
};
int main(){
    stack<Student> stack1, stack2, stack3, temp;
    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<<"\nStack 1 contents: All records\n";
    while(!stack1.empty()){
        student = stack1.top();
        student.display();
        temp.push(student);
        stack1.pop();
    }
    while(!temp.empty()){
        stack1.push(temp.top());
        temp.pop();
    }
    while(!stack1.empty()){
        student = stack1.top();
        if(student.surname[0] == 'R' || student.surname[0] == 'J' || student.surname[0] == 'M'){
            stack2.push(student);
            stack1.pop();
            continue;
        }
        stack1.pop();
        temp.push(student);
    }
    while(!temp.empty()){
        stack1.push(temp.top());
        temp.pop();
    }
    cout<<"\nStack 1 contents: Surnames not starting with J, M or R\n";
    while(!stack1.empty()){
        student = stack1.top();
        student.display();
        temp.push(student);
        stack1.pop();
    }
    while(!temp.empty()){
        stack1.push(temp.top());
        temp.pop();
    }
    cout<<"\nStack 2 contents: Surnames starting with J, M or R\n";
    while(!stack2.empty()){
        student = stack2.top();
        temp.push(student);
        student.display();
        stack2.pop();
    }
    while(!temp.empty()){
        stack2.push(temp.top());
        temp.pop();
    }
    while(!stack1.empty()){
        student = stack1.top();
        if(student.marks < 50){
            stack3.push(student);
            stack1.pop();
            continue;
        }
        stack1.pop();
        temp.push(student);
    }
    while(!temp.empty()){
        stack1.push(temp.top());
        temp.pop();
    }
    while(!stack2.empty()){
        student = stack2.top();
        if(student.marks < 50){
            stack3.push(student);
            stack2.pop();
            continue;
        }
        stack2.pop();
        temp.push(student);
    }
    while(!temp.empty()){
        stack2.push(temp.top());
        temp.pop();
    }
    cout<<"\nStack 1 contents: Marks >= 50\n";
    while(!stack1.empty()){
        student = stack1.top();
        student.display();
        stack1.pop();
    }
    cout<<"\nStack 2 contents: Marks >= 50\n";
    while(!stack2.empty()){
        student = stack2.top();
        student.display();
        stack2.pop();
    }
    cout<<"\nStack 3 contents: Marks < 50\n";
    while(!stack3.empty()){
        student = stack3.top();
        student.display();
        stack3.pop();
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS