Advertisement
Guest User

Untitled

a guest
May 21st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class Transport {
  7. protected:
  8.     char destinacija[100];
  9.     int cena;
  10.     int km;
  11. public:
  12.     Transport() {
  13.         cena = 0;
  14.         km = 0;
  15.     }
  16.  
  17.     Transport(char *destinacija, int cena, int km) {
  18.         strcpy(this->destinacija, destinacija);
  19.         this->cena = cena;
  20.         this->km = km;
  21.     }
  22.  
  23.     /* Transport(const Transport &t) {
  24.          strcpy(this->destinacija, t.destinacija);
  25.          this->cena = t.cena;
  26.          this->km = t.km;
  27.      }
  28.  
  29.      Transport &operator=(const Transport &t) {
  30.          if (this != &t) {
  31.              strcpy(this->destinacija, t.destinacija);
  32.              this->cena = t.cena;
  33.              this->km = t.km;
  34.          }
  35.          return *this;
  36.      }*/
  37.  
  38.     bool operator<(Transport &t2) {
  39.         if (this->km < t2.km) {
  40.             return true;
  41.         } else
  42.             return false;
  43.     }
  44.  
  45.     virtual ~Transport() {}
  46.  
  47.     virtual double cenaTransport() = 0;
  48.  
  49.     //=============SETTERS & GETTERS ================
  50.     int getKm() {
  51.         return km;
  52.     }
  53.  
  54.     char *getDestinacija() {
  55.         return destinacija;
  56.     }
  57. };
  58.  
  59. class AvtomobilTransport : public Transport {
  60. private:
  61.     bool sofer;
  62. public:
  63.     AvtomobilTransport() : Transport() {
  64.         sofer = false;
  65.     }
  66.  
  67.     AvtomobilTransport(char *destinacija, int cena, int km, bool sofer)
  68.             : Transport(destinacija, cena, km) {
  69.         this->sofer = sofer;
  70.     }
  71.  
  72.     ~AvtomobilTransport() {}
  73.  
  74.     double cenaTransport() {
  75.         if (sofer) {
  76.             return cena * 1.20;
  77.         } else
  78.             return cena;
  79.     }
  80. };
  81.  
  82.  
  83. class KombeTransport : public Transport {
  84. private:
  85.     int brLugje;
  86. public:
  87.     KombeTransport() : Transport() {
  88.         brLugje = 0;
  89.     }
  90.  
  91.     KombeTransport(char *destinacija, int cena, int km, int brLugje)
  92.             : Transport(destinacija, cena, km) {
  93.         this->brLugje = brLugje;
  94.     }
  95.  
  96.     ~KombeTransport() {}
  97.  
  98.     int getBrLugje() {
  99.         return brLugje;
  100.     }
  101.  
  102.     double cenaTransport() {
  103.         return cena - (200 * brLugje);
  104.     }
  105. };
  106.  
  107. void sort(Transport **ponudi, int n) {
  108.     for (int i = 0; i < n; ++i) {
  109.         for (int j = i + 1; j < n; ++j) {
  110.             if (!(*ponudi[i] < *ponudi[j])) {
  111.                 Transport *temp = ponudi[i];
  112.                 ponudi[i] = ponudi[j];
  113.                 ponudi[j] = temp;
  114.             }
  115.         }
  116.     }
  117. }
  118.  
  119. void pecatiPoloshiPonudi(Transport **ponudi, int n, Transport &nov) {
  120.     // pomosna niza od poskapite ponudi koi ke gi sortiram
  121.  
  122.     int j = 0;
  123.     int elementsCounter = 0;
  124.     for (int i = 0; i < n; ++i) {
  125.         // gi dodavam ponudite so se poskapi od &nov vo temp
  126.         if (ponudi[i]->cenaTransport() > nov.cenaTransport()) {
  127.             elementsCounter++;
  128.         }
  129.     }
  130.  
  131.     Transport **temp = new Transport *[elementsCounter];
  132.  
  133.     j = 0;
  134.     for (int i = 0; i < n; ++i) {
  135.         // gi dodavam ponudite so se poskapi od &nov vo temp
  136.         if (ponudi[i]->cenaTransport() > nov.cenaTransport()) {
  137.             temp[j++] = ponudi[i];
  138.         }
  139.     }
  140.  
  141.     sort(temp, elementsCounter);
  142.  
  143.     for (int i = 0; i < j; ++i) {
  144.         cout << temp[i]->getDestinacija() << " "
  145.              << temp[i]->getKm() << " "
  146.              << temp[i]->cenaTransport() << endl;
  147.     }
  148.  
  149.     delete[] temp;
  150. }
  151.  
  152. int main() {
  153.  
  154.     char destinacija[20];
  155.     int tip, cena, rastojanie, lugje;
  156.     bool shofer;
  157.     int n;
  158.     cin >> n;
  159.     Transport **ponudi;
  160.     ponudi = new Transport *[n];
  161.  
  162.     for (int i = 0; i < n; i++) {
  163.  
  164.         cin >> tip >> destinacija >> cena >> rastojanie;
  165.         if (tip == 1) {
  166.             cin >> shofer;
  167.             ponudi[i] = new AvtomobilTransport(destinacija, cena, rastojanie, shofer);
  168.  
  169.         } else {
  170.             cin >> lugje;
  171.             ponudi[i] = new KombeTransport(destinacija, cena, rastojanie, lugje);
  172.         }
  173.  
  174.  
  175.     }
  176.  
  177.     AvtomobilTransport nov("Ohrid", 2000, 600, false);
  178.     pecatiPoloshiPonudi(ponudi, n, nov);
  179.  
  180.     for (int i = 0; i < n; i++) delete ponudi[i];
  181.     delete[] ponudi;
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement