Advertisement
Filip_Markoski

Automobile (Working) - 30 March

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