Advertisement
Guest User

Transport

a guest
May 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. // вашиот код треба да биде тука
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class Transport
  7. {
  8.    protected:
  9.       char destinacija[20];
  10.       int cena, restojanie;
  11.    public:
  12.       // Constructors
  13.       Transport(){}
  14.       Transport(const char *destinacija, int cena, int restojanie)
  15.          : cena(cena), restojanie(restojanie) { strcpy(this->destinacija, destinacija); }
  16.       virtual ~Transport(){}
  17.  
  18.       // Functions
  19.       virtual float cenaTransport() const = 0;
  20.       int getCena() const { return cena; }
  21.       int getRestojanie() const { return restojanie; }
  22.       const char *getDestinacija() const { return destinacija; }
  23. };
  24.  
  25. class AvtomobilTransport : public Transport
  26. {
  27.    private:
  28.       bool shofer;
  29.    public:
  30.       // Constructors
  31.       AvtomobilTransport(){}
  32.       AvtomobilTransport(const char *destinacija, int cena, int restojanie, bool shofer)
  33.          : Transport(destinacija, cena, restojanie), shofer(shofer){}
  34.       ~AvtomobilTransport(){}
  35.  
  36.       // Functions
  37.       void setShofer(bool shofer) { this->shofer = shofer; }
  38.       bool getShofer() const { return shofer; }
  39.       float cenaTransport() const override
  40.       {
  41.          if(shofer) return cena + cena*0.2;
  42.          else return cena;
  43.       }
  44. };
  45.  
  46. class KombeTransport : public Transport
  47. {
  48.    private:
  49.       int lugje;
  50.    public:
  51.       // Constructors
  52.       KombeTransport(){}
  53.       KombeTransport(const char *destinacija, int cena, int restojanie, int lugje)
  54.          : Transport(destinacija, cena, restojanie), lugje(lugje){}
  55.      
  56.       // Functions
  57.       void setLugje(int lugje) { this->lugje = lugje; }
  58.       int getLugje() const { return lugje; }
  59.       float cenaTransport() const override { return cena - lugje * 200; }
  60. };
  61.  
  62. bool operator <(const Transport &ob1, const Transport &ob2)  { return ob1.getRestojanie() < ob2.getRestojanie(); }
  63. bool operator >(const Transport &ob1, const Transport &ob2)  { return !(ob1.getRestojanie() < ob2.getRestojanie()); }
  64. void pecatiPoloshiPonudi(Transport **ponudi, int n, const Transport &ob)
  65. {
  66.    int count = 0;
  67.  
  68.     // tmp length
  69.    for(int i = 0; i < n; i++)
  70.    {
  71.         if(ob.cenaTransport() < ponudi[i]->cenaTransport()) count++;
  72.    }
  73.  
  74.     // Cast
  75.     Transport **tmp = new Transport *[count];
  76.    for(int i = 0, j = 0; i < n; i++)
  77.     {
  78.         if(ob.cenaTransport() < ponudi[i]->cenaTransport())
  79.         {
  80.             AvtomobilTransport *at = dynamic_cast <AvtomobilTransport*>(ponudi[i]);
  81.             if(at) tmp[j++] = new AvtomobilTransport(*at);
  82.             else
  83.             {
  84.                 KombeTransport *kt = dynamic_cast <KombeTransport*>(ponudi[i]);
  85.                 tmp[j++] = new KombeTransport(*kt);
  86.             }
  87.         }
  88.     }
  89.  
  90.     // Sort
  91.     for(int i = 0; i < count - 1; i++)
  92.     {
  93.         for(int j = i + 1; j < count; j++)
  94.         {
  95.             if(*tmp[i] > *tmp[j])
  96.             {
  97.                 Transport *temp = tmp[i];
  98.                 tmp[i] = tmp[j];
  99.                 tmp[j] = temp;
  100.             }
  101.         }
  102.     }
  103.  
  104.     for(int i = 0; i < count; i++)
  105.     {
  106.         std::cout<<tmp[i]->getDestinacija()<<" "<<tmp[i]->getRestojanie()<<" "<<tmp[i]->cenaTransport()<< std::endl;
  107.     }
  108. }
  109.  
  110.  
  111. int main(){
  112.  
  113. char destinacija[20];
  114. int tip,cena,rastojanie,lugje;
  115. bool shofer;
  116. int n;
  117. cin>>n;
  118. Transport  **ponudi;
  119. ponudi=new Transport *[n];
  120.  
  121. for (int i=0;i<n;i++){
  122.  
  123.     cin>>tip>>destinacija>>cena>>rastojanie;
  124.     if (tip==1) {
  125.         cin>>shofer;
  126.         ponudi[i]=new AvtomobilTransport(destinacija,cena,rastojanie,shofer);
  127.  
  128.     }
  129.     else {
  130.         cin>>lugje;
  131.         ponudi[i]=new KombeTransport(destinacija,cena,rastojanie,lugje);
  132.     }
  133.  
  134.  
  135. }
  136.  
  137. AvtomobilTransport nov("Ohrid",2000,600,false);
  138. pecatiPoloshiPonudi(ponudi,n,nov);
  139.  
  140. for (int i=0;i<n;i++) delete ponudi[i];
  141. delete [] ponudi;
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement