void PrintTrain(TrainCar* train) { if (train == NULL) { std::cout << "PrintTrain: empty train!" << std::endl; return; } // Print each of the 5 rows of the TrainCar ASCII art PrintHelper(train, 0); PrintHelper(train, 1); PrintHelper(train, 2); PrintHelper(train, 3); PrintHelper(train, 4); // UNCOMMENT THESE ADDITIONAL STATISTICS AS YOU WORK int total_weight = 0; int num_engines = 0; int num_freight_cars = 0; int num_passenger_cars = 0; int num_dining_cars = 0; int num_sleeping_cars = 0; CountEnginesAndTotalWeight(train, total_weight,num_engines,num_freight_cars, num_passenger_cars, num_dining_cars, num_sleeping_cars); int total_cars = num_engines+num_freight_cars+num_passenger_cars+num_dining_cars+num_sleeping_cars; float speed = CalculateSpeed(train); std::cout << "#cars = " << total_cars; std::cout << ", total weight = " << total_weight; std::cout << ", speed on 2% incline = " << std::setprecision(1) << std::fixed << speed; // If there is at least one passenger car, print the average // distance to dining car statistic if (num_passenger_cars > 0) { float dist_to_dining = AverageDistanceToDiningCar(train); if (dist_to_dining < 0) { // If one or more passenger cars are blocked from accessing the // dining car (by an engine car) then the distance is infinity! std::cout << ", avg distance to dining = inf"; } else { std::cout << ", avg distance to dining = " << std::setprecision(1) << std::fixed << dist_to_dining; } } // If there is at least one sleeping car, print the closest engine // to sleeper car statistic if (num_sleeping_cars > 0) { int closest_engine_to_sleeper = ClosestEngineToSleeperCar(train); std::cout << ", closest engine to sleeper = " << closest_engine_to_sleeper; } std::cout << std::endl; }
// This helper function prints one of the 5 rows of the TrainCar ASCII art void PrintHelper(TrainCar* t, int which_row) { if (t == NULL) { // end of the line std::cout << std::endl; return; } if (which_row == 0) { // the top row only contains "smoke" for engine traincars if (t->isEngine()) { std::cout << " ~~~~"; } else { std::cout << " "; } } else if (which_row == 1) { // the 2nd row only contains the smoke stack for engine traincars if (t->isEngine()) { std::cout << " || "; } else { std::cout << " "; } } else if (which_row == 2) { // the 3rd row contains the ID for each traincar // (and engine traincars are shaped a little differently) if (t->isEngine()) { std::cout << " " << std::setw(6) << std::setfill('-') << t->getID(); } else { std::cout << std::setw(9) << std::setfill('-') << t->getID(); } std::cout << std::setfill(' '); } else if (which_row == 3) { // the 4th row is different for each TrainCar type if (t->isEngine()) { std::cout << " / ENGINE"; } else if (t->isFreightCar()) { // freight cars display their weight std::cout << "|" << std::setw(5) << t->getWeight() << " |"; } else if (t->isPassengerCar()) { // passenger cars are simple empty boxes std::cout << "| |"; } else if (t->isDiningCar()) { std::cout << "| dine |"; } else { assert (t->isSleepingCar()); std::cout << "| sleep |"; } } else if (which_row == 4) { // final row is the same for all cars, just draw the wheels std::cout << "-oo---oo-"; } // between cars display the '+' link symbol on the 5th row // (only if there is a next car) if (t->next != NULL) { if (which_row == 4) { std::cout << " + "; } else { std::cout << " "; } } // recurse to print the rest of the row PrintHelper(t->next, which_row); }
void Print(const Args&... xs) { PrintHelper(xs...); }
std::ostream& operator<<(std::ostream& out, std::tuple<Types...> const& tuple) { return PrintHelper(out, tuple, std::make_index_sequence<sizeof...(Types)>()); }
void PrintHelper(const A& x, const Args&... xs) { std::cout << x << ' '; PrintHelper(xs...); }