Advertisement
Guest User

laba 1 sem2 5var

a guest
Mar 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.48 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <vector>
  3. #include <ctime>
  4. #include <string>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8. const string DefaultVehicleName = "Untyped vehicle";
  9. const string DefaultCoachName = "Default Coach";
  10. const string DefaultAutomobileName = "Default Automobile";
  11. const string DefaultAeroplaneName = "Default Aeroplane";
  12. const double DefaultVehicleSpeed = -1.;
  13. const double DefaultCoachSpeed = 10.;
  14. const double DefaultAutomobileSpeed = 100.;
  15. const double DefaultAeroplaneSpeed = 500.;
  16. const double defaultMAX_FLY_TIME = 1000.;
  17. const double DefaultTimeToBoot = 0.01;
  18. const double DefaultMAX_DISTANCE = 1000.;
  19.  
  20. class Vehicle {
  21. public:
  22.     Vehicle() : totalDistance(0), totalTime(0), Speed(DefaultVehicleSpeed), nameCString(DefaultVehicleName) {}
  23.     Vehicle(string inNameCString, double inBaseSpeed) : totalDistance(0), totalTime(0), Speed(inBaseSpeed), nameCString(inNameCString) {
  24.         if (inNameCString.length() > 50) {
  25.             while (inNameCString.length() > 50)
  26.                 inNameCString.pop_back();
  27.             nameCString = inNameCString;
  28.         }
  29.     }
  30.     virtual ~Vehicle() {}
  31.     string GetName() {
  32.         return nameCString;
  33.     }
  34.     double GetSpeed() {
  35.         return Speed;
  36.     }
  37.     double GetPrice() {
  38.         return theprice;
  39.     }
  40.     double GetTotalDistance() {
  41.         return totalDistance;
  42.     }
  43.     double GetTotalTime() {
  44.         return totalTime;
  45.     }
  46.     virtual bool MakeTrip(double distanceOfTrip)=0;
  47.     bool operator< (Vehicle &rhs) {
  48.         if (GetSpeed() < rhs.GetSpeed()) {
  49.             return true;
  50.         }
  51.         return false;
  52.     }
  53.  
  54. protected:
  55.     double totalDistance;
  56.     double totalTime;
  57.     double Speed, theprice;
  58.     string nameCString;
  59. };
  60.  
  61. class Computer {
  62. public:
  63.     Computer() : baseTimeToBoot(DefaultTimeToBoot) {}
  64.     double GetTimeToBoot() {
  65.         return baseTimeToBoot;
  66.     }
  67.     double baseTimeToBoot;
  68.     ~Computer() {}
  69. };
  70.  
  71. class aeroplane : public Vehicle
  72. {
  73. public:
  74.     aeroplane() :Vehicle() {
  75.         MAX_FLY_TIME = defaultMAX_FLY_TIME;
  76.         timeSinceLastRepair = 0;
  77.     }
  78.     aeroplane(double MAX_FLY_TIMe) :MAX_FLY_TIME(MAX_FLY_TIMe), timeSinceLastRepair(0), Vehicle() {}
  79.     aeroplane(string name, double Speed, double MAX_FLY_TIMe) :MAX_FLY_TIME(MAX_FLY_TIMe), timeSinceLastRepair(0), Vehicle(name, Speed) {}
  80.     double GetTimeToBoot() {
  81.         return PC.baseTimeToBoot;
  82.     }
  83.     virtual bool MakeTrip(double distanceOfTrip) {
  84.         double timeOfTrip = distanceOfTrip / GetSpeed() + GetTimeToBoot();
  85.         if (timeSinceLastRepair + timeOfTrip > MAX_FLY_TIME) {
  86.             return false;
  87.         }
  88.         timeSinceLastRepair += timeOfTrip;
  89.         totalDistance +=  distanceOfTrip;
  90.         totalTime += timeOfTrip;
  91.         theprice = 12 * distanceOfTrip;
  92.         return true;
  93.     }
  94.     void Repair() {
  95.         timeSinceLastRepair = 0;
  96.     }
  97.     double GetTimeSinceLastRepair() const {
  98.         return timeSinceLastRepair;
  99.     }
  100.     void ComputerUpdate(double newTimeToBoot) {
  101.         PC.baseTimeToBoot = newTimeToBoot;
  102.     }
  103. private:
  104.     double MAX_FLY_TIME;
  105.     double timeSinceLastRepair;
  106.     Computer PC;
  107. };
  108.  
  109. class coach : public Vehicle
  110. {
  111. public:
  112.     coach() : MAX_DISTANCE(DefaultMAX_DISTANCE), Vehicle() {}
  113.     coach(double MaxDis) : MAX_DISTANCE(MaxDis), Vehicle() {}
  114.     coach(string name, double Price, double MaxDis) : Vehicle(name, Price), MAX_DISTANCE(MaxDis) {}
  115.     virtual bool MakeTrip(double distanceOfTrip) {
  116.         double timeOfTrip = distanceOfTrip / GetSpeed();
  117.         totalDistance += distanceOfTrip;
  118.         Speed = GetSpeed() * exp(-totalTime / 500.);
  119.         totalTime += timeOfTrip;
  120.         theprice = 5* timeOfTrip;
  121.         if (totalDistance>MAX_DISTANCE) {
  122.             return false;
  123.         }
  124.         return true;
  125.     }
  126. private:
  127.     double MAX_DISTANCE;
  128. };
  129.  
  130. class automobile : public Vehicle
  131. {
  132. public:
  133.     automobile() : price(0), Vehicle() {}
  134.     automobile(int Price) : price(Price), Vehicle() {}
  135.     automobile(string inNameCString, double speed, int Price) : Vehicle(inNameCString, speed), price(Price) {}
  136.     ~automobile() {}
  137.     virtual bool MakeTrip(double distanceOfTrip) {
  138.         double timeOfTrip = distanceOfTrip / GetSpeed();
  139.         totalDistance += distanceOfTrip;
  140.         Speed = GetSpeed() * exp(-totalDistance / 500.);
  141.         totalTime += timeOfTrip;
  142.         theprice = 8 * distanceOfTrip;
  143.         return true;
  144.     }
  145. private:
  146.     int price;
  147. };
  148.  
  149. template <class MyType>
  150. void CommitRandomTrips(vector<MyType *> &Vehicle) {
  151.     for (int i = 0; i < Vehicle.capacity(); ++i) {
  152.         double randomDistance = double(rand() % 20001) / 10.;
  153.         Vehicle[i]->MakeTrip(randomDistance);
  154.     }
  155. }
  156.  
  157. template <class MyType>
  158. void MySwap(MyType &v1, MyType &v2) {
  159.     MyType v3 = v1;
  160.     v1 = v2;
  161.     v2 = v3;
  162. }
  163.  
  164. template <class ArrayType, class LessFunctionType>
  165. int FindMinimumIndex(ArrayType &data_array, int beginIndex, int endIndex, LessFunctionType LessFunction) {
  166.     int minimumIndex = beginIndex;
  167.     for (int element_number = beginIndex + 1; element_number <= endIndex; ++element_number) {
  168.         if (LessFunction(data_array[element_number], data_array[minimumIndex])) {
  169.             minimumIndex = element_number;
  170.         }
  171.     }
  172.     return minimumIndex;
  173. }
  174.  
  175. template <class ArrayType, class LessFunctionType>
  176. void SelectionSort(ArrayType &data_array, int beginIndex, int endIndex, LessFunctionType LessFunction) {
  177.     for (int element_number = beginIndex; element_number <= endIndex; ++element_number) {
  178.         int minimumIndex = FindMinimumIndex(data_array, element_number, endIndex, LessFunction);
  179.         MySwap(data_array[minimumIndex], data_array[element_number]);
  180.     }
  181. }
  182.  
  183. void DisplayVehicles(vector<Vehicle *> &vehicles) {
  184.     printf("%s\t%s\t%s\t%s\t%s\n", "Name", "Speed", "Dist", "Time", "Price");
  185.     for (int i = 0; i < vehicles.capacity(); ++i) {
  186.         cout << vehicles[i]->GetName() << "\t";
  187.         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());
  188.     }
  189. }
  190.  
  191.  
  192. bool CompareTime(Vehicle *lhs, Vehicle *rhs) {
  193.     return lhs->GetTotalTime() < rhs->GetTotalTime();
  194. }
  195.  
  196. int main()
  197. {
  198.     srand(0);
  199.     vector<Vehicle *> coachPointers;
  200.     vector<Vehicle *> automobilePointers;
  201.     vector<Vehicle *> aeroplanePointers;
  202.  
  203.     coach coach1("coach1", 10, 40);
  204.     coachPointers.push_back(&coach1);
  205.     coach1.MakeTrip(33);
  206.     coach coach2("coach2", 17, 40);
  207.     coachPointers.push_back(&coach2);
  208.     CommitRandomTrips(coachPointers);
  209.     SelectionSort(coachPointers, 0, coachPointers.capacity() - 1,  CompareTime);
  210.     DisplayVehicles(coachPointers);
  211.  
  212.     cout << endl;
  213.  
  214.     automobile auto1("auto1", 1.4, 12);
  215.     automobile auto2("auto2", 2.4, 18);
  216.     automobilePointers.push_back(&auto2);
  217.     automobilePointers.push_back(&auto1);
  218.     auto2.MakeTrip(200);
  219.     CommitRandomTrips(automobilePointers);
  220.     SelectionSort(automobilePointers, 0, automobilePointers.capacity() - 1,  CompareTime);
  221.     DisplayVehicles(automobilePointers);
  222.  
  223.     system("pause");
  224.     return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement