Answer to Question #240590 in C++ for Hunter

Question #240590

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. 


1
Expert's answer
2021-09-22T23:44:56-0400
#include<iostream>
#include<stack>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
void display(stack<string>s){
	stack<string>temp;
    int size_of_stack = s.size();
   
    for(int i=0; i<size_of_stack; i++){
        cout<<s.top()<<endl;
    	temp.push(s.top());
    	s.pop();
    	
	}
	s = temp;
}
int main() {
 
    stack<string> s1,s2,s3;
    stack<string>temp;
    s1.push("1. Sam Williams 60");
    s1.push("2. John Phoenix 85");
    s1.push("3. Simon Johnson 75");
    s1.push("4. Sarah Khosa 81");
    s1.push("5. Mat Jackson 38");
    s1.push("6. Nick Roberts 26");
    s1.push("7. Isaac Wayne 74");
    s1.push("8. Anna Mishima 34");
    s1.push("9. Daniel Rose 64");
    s1.push("10. Aaron Black 83");
    s1.push("11. Jack Mohamed 27");
    s1.push("12. Kathrine Bruckner 42");
    
    cout<<"Content of the first stack before removal of names\n";
    display(s1);
    int len= s1.size();
    for(int i=0; i<len; i++){
  
	  string line = s1.top();
    string arr[5];
    int x = 0;
    int n = 1;
    stringstream ssin(line);
    while (ssin.good() && x < len){
        ssin >> arr[x];
        ++x;
        n++;
    }
    
    if(arr[n-3][0]=='R' || arr[n-3][0]=='J'||arr[n-3][0]=='M'){
    	s2.push(s1.top());
    	
	}
	else{
		temp.push(s1.top());
	}
    
	s1.pop();	
	}
	s1 = temp;
	cout<<"The content of the stack 1:\n";
	display(s1);
	
	
	cout<<"The contents of stack 2:\n";
	display(s2);
	stack<string> temp1;
	int size_stack1 = s1.size();
	for(int i=0; i<size_stack1; i++){
		string line = s1.top();
    string arr[5];
    int x = 0;
    int n = 1;
    stringstream ssin(line);
    while (ssin.good() && x < size_stack1){
        ssin >> arr[x];
        ++x;
        n++;
    }
    
    
	if(arr[n-2]<to_string(50)){
		s3.push(s1.top());
	}
	else{
		temp1.push(s1.top());
	}
    
	s1.pop();
	}
	
	stack<string> temp2;
	int size_stack2 = s2.size();
	for(int i=0; i<size_stack2; i++){
		string line = s2.top();
    string arr[5];
    int x = 0;
    int n = 1;
    stringstream ssin(line);
    while (ssin.good() && x < size_stack2){
        ssin >> arr[x];
        ++x;
        n++;
    }
    
    
	if(arr[n-2]<to_string(50)){
		s3.push(s2.top());
	}
	else{
		temp2.push(s2.top());
	}
    
	s2.pop();
	}
	
	
	
	
	
	
	
	s1 = temp1;
	s2 = temp2;
	cout<<"The contents of the final stacks after performing all operations mentioned in the question:\n";
	cout<<"The contents of stack 1:\n";
	display(s1);
	
	cout<<"\nThe contents of stack 2:\n";
	display(s2);
	cout<<"The contents of stack 3:\n";
	display(s3);
    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
New on Blog
APPROVED BY CLIENTS