Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <vector>
- #include <ctime>
- #include <string>
- #include <iostream>
- using namespace std;
- const string DefaultVehicleName = "Untyped vehicle";
- const string DefaultCoachName = "Default Coach";
- const string DefaultAutomobileName = "Default Automobile";
- const string DefaultAeroplaneName = "Default Aeroplane";
- const double DefaultVehicleSpeed = -1.;
- const double DefaultCoachSpeed = 10.;
- const double DefaultAutomobileSpeed = 100.;
- const double DefaultAeroplaneSpeed = 500.;
- const double defaultMAX_FLY_TIME = 1000.;
- const double DefaultTimeToBoot = 0.01;
- const double DefaultMAX_DISTANCE = 1000.;
- class Vehicle {
- public:
- Vehicle() : totalDistance(0), totalTime(0), Speed(DefaultVehicleSpeed), nameCString(DefaultVehicleName) {}
- Vehicle(string inNameCString, double inBaseSpeed) : totalDistance(0), totalTime(0), Speed(inBaseSpeed), nameCString(inNameCString) {
- if (inNameCString.length() > 50) {
- while (inNameCString.length() > 50)
- inNameCString.pop_back();
- nameCString = inNameCString;
- }
- }
- virtual ~Vehicle() {}
- string GetName() {
- return nameCString;
- }
- double GetSpeed() {
- return Speed;
- }
- double GetPrice() {
- return theprice;
- }
- double GetTotalDistance() {
- return totalDistance;
- }
- double GetTotalTime() {
- return totalTime;
- }
- virtual bool MakeTrip(double distanceOfTrip)=0;
- bool operator< (Vehicle &rhs) {
- if (GetSpeed() < rhs.GetSpeed()) {
- return true;
- }
- return false;
- }
- protected:
- double totalDistance;
- double totalTime;
- double Speed, theprice;
- string nameCString;
- };
- class Computer {
- public:
- Computer() : baseTimeToBoot(DefaultTimeToBoot) {}
- double GetTimeToBoot() {
- return baseTimeToBoot;
- }
- double baseTimeToBoot;
- ~Computer() {}
- };
- class aeroplane : public Vehicle
- {
- public:
- aeroplane() :Vehicle() {
- MAX_FLY_TIME = defaultMAX_FLY_TIME;
- timeSinceLastRepair = 0;
- }
- aeroplane(double MAX_FLY_TIMe) :MAX_FLY_TIME(MAX_FLY_TIMe), timeSinceLastRepair(0), Vehicle() {}
- aeroplane(string name, double Speed, double MAX_FLY_TIMe) :MAX_FLY_TIME(MAX_FLY_TIMe), timeSinceLastRepair(0), Vehicle(name, Speed) {}
- double GetTimeToBoot() {
- return PC.baseTimeToBoot;
- }
- virtual bool MakeTrip(double distanceOfTrip) {
- double timeOfTrip = distanceOfTrip / GetSpeed() + GetTimeToBoot();
- if (timeSinceLastRepair + timeOfTrip > MAX_FLY_TIME) {
- return false;
- }
- timeSinceLastRepair += timeOfTrip;
- totalDistance += distanceOfTrip;
- totalTime += timeOfTrip;
- theprice = 12 * distanceOfTrip;
- return true;
- }
- void Repair() {
- timeSinceLastRepair = 0;
- }
- double GetTimeSinceLastRepair() const {
- return timeSinceLastRepair;
- }
- void ComputerUpdate(double newTimeToBoot) {
- PC.baseTimeToBoot = newTimeToBoot;
- }
- private:
- double MAX_FLY_TIME;
- double timeSinceLastRepair;
- Computer PC;
- };
- class coach : public Vehicle
- {
- public:
- coach() : MAX_DISTANCE(DefaultMAX_DISTANCE), Vehicle() {}
- coach(double MaxDis) : MAX_DISTANCE(MaxDis), Vehicle() {}
- coach(string name, double Price, double MaxDis) : Vehicle(name, Price), MAX_DISTANCE(MaxDis) {}
- virtual bool MakeTrip(double distanceOfTrip) {
- double timeOfTrip = distanceOfTrip / GetSpeed();
- totalDistance += distanceOfTrip;
- Speed = GetSpeed() * exp(-totalTime / 500.);
- totalTime += timeOfTrip;
- theprice = 5* timeOfTrip;
- if (totalDistance>MAX_DISTANCE) {
- return false;
- }
- return true;
- }
- private:
- double MAX_DISTANCE;
- };
- class automobile : public Vehicle
- {
- public:
- automobile() : price(0), Vehicle() {}
- automobile(int Price) : price(Price), Vehicle() {}
- automobile(string inNameCString, double speed, int Price) : Vehicle(inNameCString, speed), price(Price) {}
- ~automobile() {}
- virtual bool MakeTrip(double distanceOfTrip) {
- double timeOfTrip = distanceOfTrip / GetSpeed();
- totalDistance += distanceOfTrip;
- Speed = GetSpeed() * exp(-totalDistance / 500.);
- totalTime += timeOfTrip;
- theprice = 8 * distanceOfTrip;
- return true;
- }
- private:
- int price;
- };
- template <class MyType>
- void CommitRandomTrips(vector<MyType *> &Vehicle) {
- for (int i = 0; i < Vehicle.capacity(); ++i) {
- double randomDistance = double(rand() % 20001) / 10.;
- Vehicle[i]->MakeTrip(randomDistance);
- }
- }
- template <class MyType>
- void MySwap(MyType &v1, MyType &v2) {
- MyType v3 = v1;
- v1 = v2;
- v2 = v3;
- }
- template <class ArrayType, class LessFunctionType>
- int FindMinimumIndex(ArrayType &data_array, int beginIndex, int endIndex, LessFunctionType LessFunction) {
- int minimumIndex = beginIndex;
- for (int element_number = beginIndex + 1; element_number <= endIndex; ++element_number) {
- if (LessFunction(data_array[element_number], data_array[minimumIndex])) {
- minimumIndex = element_number;
- }
- }
- return minimumIndex;
- }
- template <class ArrayType, class LessFunctionType>
- void SelectionSort(ArrayType &data_array, int beginIndex, int endIndex, LessFunctionType LessFunction) {
- for (int element_number = beginIndex; element_number <= endIndex; ++element_number) {
- int minimumIndex = FindMinimumIndex(data_array, element_number, endIndex, LessFunction);
- MySwap(data_array[minimumIndex], data_array[element_number]);
- }
- }
- void DisplayVehicles(vector<Vehicle *> &vehicles) {
- printf("%s\t%s\t%s\t%s\t%s\n", "Name", "Speed", "Dist", "Time", "Price");
- for (int i = 0; i < vehicles.capacity(); ++i) {
- cout << vehicles[i]->GetName() << "\t";
- printf("%5.2lf\t %5.2lf\t %5.2lf\t %5.2lf\n", vehicles[i]->GetSpeed(), vehicles[i]->GetTotalDistance(), vehicles[i]->GetTotalTime(), vehicles[i]->GetPrice());
- }
- }
- bool CompareTime(Vehicle *lhs, Vehicle *rhs) {
- return lhs->GetTotalTime() < rhs->GetTotalTime();
- }
- int main()
- {
- srand(0);
- vector<Vehicle *> coachPointers;
- vector<Vehicle *> automobilePointers;
- vector<Vehicle *> aeroplanePointers;
- coach coach1("coach1", 10, 40);
- coachPointers.push_back(&coach1);
- coach1.MakeTrip(33);
- coach coach2("coach2", 17, 40);
- coachPointers.push_back(&coach2);
- CommitRandomTrips(coachPointers);
- SelectionSort(coachPointers, 0, coachPointers.capacity() - 1, CompareTime);
- DisplayVehicles(coachPointers);
- cout << endl;
- automobile auto1("auto1", 1.4, 12);
- automobile auto2("auto2", 2.4, 18);
- automobilePointers.push_back(&auto2);
- automobilePointers.push_back(&auto1);
- auto2.MakeTrip(200);
- CommitRandomTrips(automobilePointers);
- SelectionSort(automobilePointers, 0, automobilePointers.capacity() - 1, CompareTime);
- DisplayVehicles(automobilePointers);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement