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.
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 obtained
#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct Person
{
string name;
string surname;
int score;
};
void displayStack(stack<Person, vector<Person>> s)
{
vector<Person> temp;
while(!s.empty())
{
Person el;
el = s.top();
cout << el.name << " " << el.surname << " " << el.score << "\n";
s.pop();
temp.push_back(el);
}
for (int i = 0; i < size(temp); i++)
s.push(temp[i]);
}
bool byName(Person a, Person b)
{
return a.name > b.name;
}
bool bySurName(Person a, Person b)
{
return a.surname > b.surname;
}
int main()
{
Person student[] = { { "Sam", "Williams", 60 },
{ "John", "Phoenix", 85 },
{ "Simon", "Johnson", 75 },
{ "Sarah", "Khosa", 81 },
{ "Mat", "Jackson", 38 },
{ "Nick", "Roberts", 26 },
{ "Isaac", "Wayne", 74 },
{ "Anna", "Mishima", 34 },
{ "Daniel", "Rose", 64 },
{ "Aaron", "Black", 83 },
{ "Jack", "Mohamed", 27 },
{ "Kathrine", "Bruckner", 42 } };
stack <Person, vector<Person>> Stack1;
for (int i = 0; i < size(student); i++)
{
Stack1.push(student[i]);
}
cout << "View Stack1 " << "\n";
displayStack(Stack1);
stack <Person, vector<Person>> Stack2;
vector<Person> temp;
Person el;
while (!Stack1.empty())
{
el = Stack1.top();
temp.push_back(el);
if (el.surname[0] != 'R' && el.surname[0] != 'M' && el.surname[0] != 'J')
{
Stack2.push(el);
}
Stack1.pop();
}
for (int i = 0; i < size(temp); i++)
{
Stack1.push(temp[i]);
}
cout << endl;
cout << "View Stack1 " << "\n";
displayStack(Stack1);
cout << "View Stack2 " << "\n";
displayStack(Stack2);
vector<Person> temp1;
while (!Stack1.empty())
{
el = Stack1.top();
temp1.push_back(el);
Stack1.pop();
}
std::sort(temp1.begin(), temp1.end(), byName);
for (int i = 0; i < size(temp1); i++)
{
Stack1.push(temp1[i]);
}
cout << endl;
cout << "View sort Stack1 by Name " << "\n";
displayStack(Stack1);
vector<Person> temp2;
while (!Stack2.empty())
{
el = Stack2.top();
temp2.push_back(el);
Stack2.pop();
}
std::sort(temp2.begin(), temp2.end(), bySurName);
for (int i = 0; i < size(temp2); i++)
{
Stack2.push(temp2[i]);
}
cout << endl;
cout << "View sort Stack2 by Surname " << "\n";
displayStack(Stack2);
// Stack3 is not defined in job conditions
return 0;
}
Comments
Leave a comment