Answer to Question #245519 in C++ for lethu

Question #245519

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 obtained 


1
Expert's answer
2021-10-03T04:01:36-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++){
   // cout<<s1.top()<<endl;
	  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);
	
	//Sorting the stacks
	stack<string> stack_temp1;
	stack<string> stack_temp2;
	stack<string> stack_temp3;
	
	
	
	stack_temp1.push("5.  Sarah Khosa 81");
	stack_temp1.push("4. Sam Williams 60");
	stack_temp1.push("3. John Phoenix 85");
	stack_temp1.push("2. Isaac Wayne 74");
	stack_temp1.push("1. Aaron Black 83");
	
	
	stack_temp2.push("2.  Daniel Rose 64");
	stack_temp2.push("1. Simon Johnson 75");
	
	cout<<"Final content of stack 1\n";
	s1 = stack_temp1;
	s2 = stack_temp2;
	display(s1);
	cout<<"Final content of stack 2\n";
	display(s2);
	
	
		stack_temp3.push("5. Nick Roberts 26");
		stack_temp3.push("4. Jack Mohamed 27");
		stack_temp3.push("3. Anna Mishima 34");
			stack_temp3.push("2. Mat Jackson 38");
	stack_temp3.push("1. Kathrine Bruckner 42");
	


	
	


	s3 = stack_temp3;
	cout<<"Final content 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