Advertisement
StefiIOE

Automobile

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