Advertisement
JStefan

[Laboratoriski] Avtomobil

Mar 18th, 2017
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. void copy_string(char*, const char*, const int);
  7.  
  8. class Automobile {
  9.     char* marka;
  10.     int registracija[5];
  11.     int maksimalna_brzina;
  12. public:
  13.     Automobile() {
  14.         marka = 0;
  15.         for(int i = 0; i < 5; ++i) registracija[i] = 0;
  16.         maksimalna_brzina = 0;
  17.     }
  18.  
  19.     Automobile(const char* _marka, const int* _registracija, const int _maksimalna_brzina) {
  20.         marka = new char[strlen(_marka)+1];
  21.         copy_string(marka, _marka, strlen(_marka));
  22.         for(int i = 0; i < 5; ++i) registracija[i] = _registracija[i];
  23.         maksimalna_brzina = _maksimalna_brzina;
  24.     }
  25.  
  26.     Automobile(const Automobile& obj) {
  27.         marka = new char[strlen(obj.marka)+1];
  28.         copy_string(marka, obj.marka, strlen(obj.marka));
  29.         for(int i = 0; i < 5; ++i) registracija[i] = obj.registracija[i];
  30.         maksimalna_brzina = obj.maksimalna_brzina;
  31.     }
  32.  
  33.     Automobile& operator=(const Automobile& obj) {
  34.         if(this != &obj) {
  35.             delete [] marka;
  36.             marka = new char[strlen(obj.marka)+1];
  37.             copy_string(marka, obj.marka, strlen(obj.marka));
  38.             for(int i = 0; i < 5; ++i) registracija[i] = obj.registracija[i];
  39.             maksimalna_brzina = obj.maksimalna_brzina;
  40.         }
  41.         return *this;
  42.     }
  43.  
  44.     bool operator==(const Automobile& obj) {
  45.         int c = 0;
  46.         for(int i = 0; i < 5; ++i) if(registracija[i] == obj.registracija[i]) c++;
  47.         return c == 5 ? true : false;
  48.     }
  49.  
  50.     // Marka:име Registracija:[x y z k l]
  51.     friend ostream& operator<<(ostream& o, const Automobile& obj) {
  52.         o<<"Marka\t"<<obj.marka<<"\tRegistracija[ ";
  53.         for(int i = 0; i < 5; ++i) {
  54.             o<<obj.registracija[i];
  55.             if(i < 4) o << " ";
  56.         }
  57.         o << " ]" << endl;
  58.         return o;
  59.     }
  60.  
  61.     // getters
  62.     char* get_marka() {
  63.         return marka;
  64.     }
  65.     int* get_registracija() {
  66.         return registracija;
  67.     }
  68.     int get_maks_brzina() {
  69.         return maksimalna_brzina;
  70.     }
  71.  
  72.     // setters
  73.     void set_marka(const char* _marka) {
  74.         marka = new char[strlen(_marka)+1];
  75.         copy_string(marka, _marka, strlen(_marka));
  76.     }
  77.     void set_registracija(const int _registracija[5]) {
  78.         for(int i = 0; i < 5; ++i) registracija[i] = _registracija[i];
  79.     }
  80.     void set_maks_brzina(const int brzina) {
  81.         maksimalna_brzina = brzina;
  82.     }
  83.  
  84.     ~Automobile() {
  85.         delete [] marka;
  86.     }
  87. };
  88.  
  89. class RentACar {
  90.     char ime[100];
  91.     Automobile* avtomobili;
  92.     int broj_na_avtomobili;
  93. public:
  94.     RentACar(const char* _ime = "") {
  95.         strcpy(ime, _ime);
  96.         broj_na_avtomobili = 0;
  97.     }
  98.  
  99.     RentACar(const RentACar& obj) {
  100.         strcpy(ime, obj.ime);
  101.         broj_na_avtomobili = obj.broj_na_avtomobili;
  102.         avtomobili = new Automobile[broj_na_avtomobili];
  103.         for(int i = 0; i < broj_na_avtomobili; ++i) {
  104.             avtomobili[i] = obj.avtomobili[i];
  105.         }
  106.     }
  107.  
  108.     RentACar& operator=(const RentACar& obj) {
  109.         if(this != &obj) {
  110.             strcpy(ime, obj.ime);
  111.             broj_na_avtomobili = obj.broj_na_avtomobili;
  112.             delete [] avtomobili;
  113.             avtomobili = new Automobile[broj_na_avtomobili];
  114.             for(int i = 0; i < broj_na_avtomobili; ++i) {
  115.                 avtomobili[i] = obj.avtomobili[i];
  116.             }
  117.         }
  118.         return *this;
  119.     }
  120.  
  121.     RentACar& operator+=(Automobile& obj) {
  122.         Automobile* temp = new Automobile[broj_na_avtomobili+1];
  123.         for(int i = 0; i < broj_na_avtomobili; ++i) {
  124.             temp[i] = avtomobili[i];
  125.         }
  126.         if(broj_na_avtomobili != 0) delete [] avtomobili;
  127.         temp[broj_na_avtomobili++] = obj;
  128.         avtomobili = temp;
  129.         return *this;
  130.     }
  131.  
  132.     RentACar& operator-=(Automobile& obj) {
  133.         Automobile* temp = new Automobile[broj_na_avtomobili];
  134.         int j = 0;
  135.         for(int i = 0; i < broj_na_avtomobili; ++i) {
  136.             if(!(avtomobili[i] == obj))
  137.                 temp[j++] = avtomobili[i];
  138.         }
  139.         broj_na_avtomobili = j + 1;
  140.         delete [] avtomobili;
  141.         avtomobili = temp;
  142.         return *this;
  143.     }
  144.  
  145.     void pecatiNadBrzina(int brzina) {
  146.         cout << ime << endl;
  147.         for(int i = 0; i < broj_na_avtomobili; ++i) {
  148.             if(avtomobili[i].get_maks_brzina() > brzina)
  149.                 cout << avtomobili[i];
  150.         }
  151.     }
  152.  
  153.     ~RentACar() {
  154.         delete [] avtomobili;
  155.     }
  156. };
  157.  
  158. void copy_string(char* dest, const char* source, const int length) {
  159.     strncpy(dest, source, length);
  160.     dest[length] = 0;
  161. }
  162.  
  163. int main() {
  164.  
  165.     RentACar agencija("FINKI-Car");
  166.     int n;
  167.     cin>>n;
  168.  
  169.     for (int i=0; i<n; i++) {
  170.         char marka[100];
  171.         int regisracija[5];
  172.         int maximumBrzina;
  173.  
  174.         cin>>marka;
  175.         for (int i=0; i<5; i++) {
  176.             cin>>regisracija[i];
  177.         }
  178.         cin>>maximumBrzina;
  179.    
  180.         Automobile nov=Automobile(marka,regisracija,maximumBrzina);
  181.        
  182.         //dodavanje na avtomobil
  183.         agencija+=nov;
  184.     }
  185.     //se cita grehsniot avtmobil, za koj shto avtmobilot so ista registracija treba da se izbrishe
  186.     char marka[100];
  187.     int regisracija[5];
  188.     int maximumBrzina;
  189.     cin>>marka;
  190.     for (int i=0; i<5; i++)
  191.         cin>>regisracija[i];
  192.     cin>>maximumBrzina;
  193.  
  194.     Automobile greshka=Automobile(marka,regisracija,maximumBrzina);
  195.  
  196.     //brishenje na avtomobil
  197.     agencija-=greshka;
  198.     agencija.pecatiNadBrzina(150);
  199.  
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement