Advertisement
metalni

OOP Zadaci za vezbanje 2 Transport

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