# STL List Exercise: Solar System Case ```cpp= #include <iostream> #include <iomanip> #include <iterator> #include <list> #include <algorithm> #include <math.h> using namespace std; class planets { public: planets() { id_ = 0; mass_ = 0; radius_ = 0; } planets(int id, double mass, double radius) { id_ = id; mass_ = mass; radius_ = radius; } planets(const planets & obj) { id_ = obj.id_; mass_ = obj.mass_; radius_ = obj.radius_; } int id_; double get_density() const { // * 5.972 * pow(10, 24) return mass_ / get_volume(); } double get_volume() const { //* 6378000 return 4.0 / 3.0 * M_PI * pow(radius_ , 3); } private: double mass_; double radius_; }; void show_planets(list<planets> & solarSystem) { cout << "id \tVolume: \tDensity:" << endl; list<planets>::iterator it; for (it = solarSystem.begin(); it!= solarSystem.end(); it++) { cout << it->id_ << fixed << setprecision(2); cout << "\t" << it->get_volume() << setfill(' ') << setw(6) << fixed << setprecision(2); cout << " \t" << it->get_density() << endl; } } void print_id(list<planets> & solarSystem) { cout << "Lists: "; for(auto const& s : solarSystem) { cout << s.id_ << " "; } cout << endl; } int main() { list<planets> solarSystem; list<planets> dwarf; planets buffer; cout << "Big Bang! Universe is expanding!" << endl; cout << "Build your own solar system!" << endl; cout << "(Type: help to get list of commands)" << endl; string cmd; while(cin >> cmd) { if (cmd == "help") { cout << "Command (q for quit):" << endl; cout << "-> Grab" << endl; cout << "-> Put" << endl; cout << "-> God" << endl; cout << "-> Insert" << endl; cout << "-> Apocalypse" << endl; cout << "-> Thanos" << endl; cout << "-> Armageddon" << endl; cout << "-> Reverse" << endl; cout << "-> Sort" << endl; cout << "-> Jovian" << endl; cout << "-> Inner" << endl; cout << "-> Swap" << endl; cout << "-> Display" << endl; } else if(cmd == "Grab") { cout << "Front or End? "; string endpoint; cin >> endpoint; if(endpoint == "Front") { if(!solarSystem.empty()) { buffer = solarSystem.front(); solarSystem.pop_front(); } } if(endpoint == "End") { if(!solarSystem.empty()){ buffer = solarSystem.back(); solarSystem.pop_back(); } } } else if(cmd == "Put") { cout << "Front or End? "; string endpoint; cin >> endpoint; if(endpoint == "Front") { solarSystem.push_front(buffer); print_id(solarSystem); } if(endpoint == "End") { solarSystem.push_back(buffer); print_id(solarSystem); } } else if(cmd == "God") { planets mercurius(1, 0.055, 0.38); planets venus(2, 0.815, 0.95); planets earth(3, 1.0, 1.0); planets mars(4, 0.107, 0.53); planets jupiter(5, 318, 10.8); planets saturn(6, 95, 9.0); planets uranus(7, 14.5, 3.93); planets neptune(8, 17.2, 3.87); // planets pluto(9, 0.002, 0.178); solarSystem.push_back(mercurius); solarSystem.push_back(venus); solarSystem.push_back(earth); solarSystem.push_back(mars); solarSystem.push_back(jupiter); solarSystem.push_back(saturn); solarSystem.push_back(uranus); solarSystem.push_back(neptune); planets Ceres(81, 0.00016, 0.076/2); planets Haumea(82, 0.0007, 0.110/2); planets Makemake(83, 0.00067, 0.102/2); planets Eris(84, 0.0028, 0.235/2); dwarf.push_back(Ceres); dwarf.push_back(Haumea); dwarf.push_back(Makemake); dwarf.push_back(Eris); } else if(cmd == "Insert") { if(!solarSystem.empty()) { cout << "Current Solar System:" << endl; print_id(solarSystem); int pos; cout << "Where do you want to insert Pluto? : "; cin >> pos; cout << endl; double radius = 0.178; double mass = 0.002; planets pluto(9, mass, radius); list<planets>::iterator it = solarSystem.begin(); advance(it, pos); //1 + pos solarSystem.insert(it, pluto); cout << "New Solar System:" << endl; print_id(solarSystem); } else { planets pluto(9, 0.002, 0.178); solarSystem.push_back(pluto); } } else if(cmd == "Apocalypse") { cout << "--List:" << endl; cout << "-->Even" << endl; cout << "-->Odd" << endl; cout << "-->Jovian" << endl; cout << "-->Inner" << endl; //Remove string cond; cin >> cond; if(cond == "Even") { solarSystem.remove_if([&](planets obj) {return (obj.id_ % 2) == 0;}); print_id(solarSystem); } else if(cond == "Odd") { solarSystem.remove_if([&](planets obj) {return (obj.id_ % 2) == 1;}); print_id(solarSystem); } else if(cond == "Jovian") { solarSystem.remove_if([&](planets obj) {return obj.id_ > 4 && obj.id_ < 9;}); print_id(solarSystem); } else if(cond == "Inner") { solarSystem.remove_if([&](planets obj) {return obj.id_ < 5;}); print_id(solarSystem); } } else if(cmd == "Thanos") { int num; cout << "Pick a number: "; cin >> num; list<planets>::iterator it = solarSystem.begin(); advance(it, num); //For range, use two iterators solarSystem.erase(it); print_id(solarSystem); } else if(cmd == "Fair") { //Remove Duplicates solarSystem.sort([](const planets& a, const planets& b) {return a.id_ < b.id_;}); solarSystem.unique([](const planets& first, const planets& second) {return first.id_ == second.id_;}); print_id(solarSystem); } else if(cmd == "Armageddon") { //clear solarSystem.clear(); } else if(cmd == "Reverse") { solarSystem.reverse(); print_id(solarSystem); } else if(cmd == "Sort") { cout << "--List:" << endl; cout << "-->Natural" << endl; cout << "-->Size" << endl; cout << "-->Density" << endl; string order; cin >> order; if(order == "Natural") { solarSystem.sort([](const planets& a, const planets& b) {return a.id_ < b.id_;}); //list<planets>::reverse_iterator it = solarSystem.rbegin(); list<planets>::iterator it = solarSystem.end(); advance(it, -4); //begin = 1 list<planets>::iterator target = solarSystem.begin(); advance(target, 4); //Between Mars and Jupiter 4 and 5 solarSystem.splice(target, solarSystem, it); print_id(solarSystem); } else if(order == "Size") { solarSystem.sort([](const planets& a, const planets& b) {return a.get_volume() < b.get_volume();}); print_id(solarSystem); } else if(order == "Density") { solarSystem.sort([](const planets& a, const planets& b) {return a.get_density() < b.get_density();}); print_id(solarSystem); } } else if(cmd == "Jovian") { //Put Jovian Front list<planets>::iterator it = solarSystem.begin(); advance(it, 4); solarSystem.splice(solarSystem.begin(), solarSystem, it, solarSystem.end()); print_id(solarSystem); } else if(cmd == "Cold") { list<planets>::iterator it = solarSystem.begin(); advance(it, 2); //begin = 1 solarSystem.splice(solarSystem.end(), solarSystem, it); print_id(solarSystem); } else if(cmd == "Dwarf") { solarSystem.merge(dwarf, [](planets & first, planets & second) {return first.id_ > second.id_;}); } else if(cmd == "Swap") { //Alternate Reality } else if(cmd == "Display") { show_planets(solarSystem); } else if(cmd == "q") { break; } } return 0; } ```