metalni

OOP Labs 4 Zadaca 1

May 30th, 2020
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include<iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. // class Avtomobil
  7. class Avtomobil{
  8.     private:
  9.         char color[20];
  10.         char brand[20];
  11.         char model[20];
  12.     public:
  13.         Avtomobil(){}
  14.         Avtomobil(const char * boja, const char * brend, const char * model);
  15.         Avtomobil& operator=(const Avtomobil &orig);
  16.         void print();
  17. };
  18. Avtomobil::Avtomobil(const char * boja, const char * brend, const char * model){
  19.     strcpy(this->color,boja);
  20.     strcpy(this->brand,brend);
  21.     strcpy(this->model,model);
  22. }
  23. Avtomobil &Avtomobil::operator=(const Avtomobil &orig){
  24.     if(this!=&orig){
  25.         strcpy(this->color,orig.color);
  26.         strcpy(this->brand,orig.brand);
  27.         strcpy(this->model,orig.model);
  28.     }
  29.     return *this;
  30. }
  31. void Avtomobil::print(){
  32.     cout << this->color << " " << this->brand << " " << this->model << endl;
  33. }
  34.  
  35. //class ParkingPlac
  36. class ParkingPlac {
  37.     private:
  38.         char address[20];
  39.         char id[50];
  40.         int costperhour;
  41.         int profit;
  42.         Avtomobil * a;
  43.         int noParked;
  44.     public:
  45.         ParkingPlac(){}
  46.         ParkingPlac(const char * adresa, const char * id, const int cenanacas);
  47.         ~ParkingPlac();
  48.         void parkirajVozilo(const Avtomobil * n, int br);
  49.         void pecati();
  50.         const char * getId();
  51.         void platiCasovi(int brojcasovi);
  52.         bool daliIstaAdresa(ParkingPlac p);
  53.         void pecatiParkiraniVozila();
  54. };
  55. ParkingPlac::ParkingPlac(const char * adresa, const char * id, const int cenanacas){
  56.     strcpy(this->address,adresa);
  57.     strcpy(this->id,id);
  58.     this->costperhour=cenanacas;
  59.     this->profit=0;
  60. }
  61. ParkingPlac::~ParkingPlac(){
  62.     delete [] this->a;
  63. }
  64. void ParkingPlac::pecati(){
  65.     if(this->profit){
  66.         cout << this->id << " " << this->address << " - " << this->profit << " denari" << endl;
  67.     } else {
  68.         cout << this->id << " " << this->address << endl;
  69.     }
  70. }
  71. const char * ParkingPlac::getId(){
  72.     return this->id;
  73. }
  74. void ParkingPlac::platiCasovi(int brojcasovi){
  75.     this->profit = profit + (brojcasovi * this->costperhour);
  76. }
  77. bool ParkingPlac::daliIstaAdresa(const ParkingPlac p){
  78.     if(!strcmp(this->address,p.address))
  79.         return true;
  80.     else
  81.         return false;
  82. }
  83. void ParkingPlac::parkirajVozilo(const Avtomobil * n, int br){
  84.     this->noParked=br;
  85.     this->a = new Avtomobil[noParked];
  86.     for(int i=0; i<br; i++){
  87.         *(a+i) = *(n+i);
  88.     }
  89. }
  90. void ParkingPlac::pecatiParkiraniVozila(){
  91.     cout << "Vo parkingot se parkirani slednite vozila: " << endl;
  92.     for(int i=0; i<this->noParked; i++){
  93.         cout << i+1 << ".";
  94.         a[i].print();
  95.     }
  96. }
  97.  
  98. int main(){
  99.  
  100.     ParkingPlac p[100];
  101.     int n,m;
  102.     char adresa[50],id[50];
  103.     int brojcasovi,cenacas;
  104.     cin>>n;
  105.     if(n > 0){
  106.  
  107.  
  108.         for (int i=0;i<n;i++){
  109.             cin.get();
  110.             cin.getline(adresa,50);
  111.             cin>>id>>cenacas;
  112.            
  113.             ParkingPlac edna(adresa,id,cenacas);
  114.            
  115.             p[i]=edna;
  116.         }
  117.        
  118.         //plakjanje
  119.         cin>>m;
  120.         for (int i=0;i<m;i++){
  121.  
  122.             cin>>id>>brojcasovi;
  123.            
  124.             int findId=false;
  125.             for (int j=0;j<m;j++){
  126.                 if (strcmp(p[j].getId(),id)==0){
  127.                     p[j].platiCasovi(brojcasovi);
  128.                     findId=true;
  129.                 }
  130.             }
  131.             if (!findId)
  132.             cout<<"Ne e platen parking. Greshen ID."<<endl;
  133.         }
  134.  
  135.         cout<<"========="<<endl;
  136.         ParkingPlac pCentar("Cvetan Dimov","C10",80);
  137.         for (int i=0;i<n;i++){
  138.             if (p[i].daliIstaAdresa(pCentar))
  139.                 p[i].pecati();
  140.         }
  141.  
  142.     } else {
  143.  
  144.         ParkingPlac najdobarPlac("Mars", "1337", 1);
  145.         int brVozila;
  146.         cin >> brVozila;
  147.         Avtomobil *n = new Avtomobil[brVozila];
  148.         for(int i = 0; i < brVozila; ++i){
  149.  
  150.             char boja[20];
  151.             char brend[20];
  152.             char model[20];
  153.  
  154.             cin >> boja >> brend >> model;
  155.             *(n+i) = Avtomobil(boja, brend, model);
  156.            
  157.         }
  158.         najdobarPlac.parkirajVozilo(n,brVozila);
  159.         delete [] n;
  160.         if(brVozila != 0)
  161.         najdobarPlac.pecatiParkiraniVozila();
  162.  
  163.     }  
  164. }
Add Comment
Please, Sign In to add comment