Advertisement
chrissj

Planinarsko Drushtvo

Mar 21st, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. class PlDrustvo {
  6.     char *ime;
  7.     int turi;
  8.     int clenovi;
  9. public:
  10.     PlDrustvo(const char *name = "", int t=0, int n=0){
  11.         ime = new char[strlen(name)+1];
  12.         strcpy(ime, name);
  13.         turi = t;
  14.         clenovi = n;
  15.     }
  16.     PlDrustvo(const PlDrustvo &pl){
  17.         ime = new char[strlen(pl.ime)+1];
  18.         strcpy(ime, pl.ime);
  19.         turi = pl.turi;
  20.         clenovi = pl.clenovi;
  21.     }
  22.     ~PlDrustvo(){delete[] ime;}
  23.     PlDrustvo &operator=(const PlDrustvo &pl){
  24.         if(this!=&pl){
  25.             delete[] ime;
  26.             ime = new char[strlen(pl.ime)+1];
  27.             strcpy(ime, pl.ime);
  28.             turi = pl.turi;
  29.             clenovi = pl.clenovi;
  30.         }
  31.         return *this;
  32.     }
  33.     bool operator>(const PlDrustvo &pl){
  34.         return (clenovi > pl.clenovi);
  35.     }
  36.     bool operator<(const PlDrustvo &pl){
  37.         return (clenovi < pl.clenovi);
  38.     }
  39.     PlDrustvo operator+(const PlDrustvo &pl){
  40.         PlDrustvo p;
  41.         p.clenovi = clenovi + pl.clenovi;
  42.         delete[] p.ime;
  43.         if(*this > pl){
  44.             p.ime = new char[strlen(ime)+1];
  45.             strcpy(p.ime, ime);
  46.             p.turi = turi;
  47.         }
  48.         else{
  49.             p.ime = new char[strlen(pl.ime)+1];
  50.             strcpy(p.ime, pl.ime);
  51.             p.turi = pl.turi;
  52.         }
  53.         return p;
  54.     }
  55.     friend ostream &operator<<(ostream &o, const PlDrustvo &pl){
  56.         return o << "Ime: " << pl.ime << " Turi: " << pl.turi << " Clenovi: " << pl.clenovi << endl;
  57.     }
  58.     friend void najmnoguClenovi(PlDrustvo*, int);
  59. };
  60.  
  61. void najmnoguClenovi(PlDrustvo *pl, int n){
  62.     int maxi = 0;
  63.     int max = 0;
  64.     for(int i=0; i < n; i++){
  65.         if(pl[i].clenovi > max){
  66.             maxi = i;
  67.             max = pl[i].clenovi;
  68.         }
  69.     }
  70.     cout << "Najmnogu clenovi ima planinarskoto drustvo: " << pl[maxi];
  71. }
  72.  
  73. int main()
  74. {              
  75.     PlDrustvo drustva[3];
  76.     PlDrustvo pl;
  77.     for (int i=0;i<3;i++)
  78.     {
  79.         char ime[100];
  80.         int brTuri;
  81.         int brClenovi;
  82.         cin>>ime;
  83.         cin>>brTuri;
  84.         cin>>brClenovi;
  85.         PlDrustvo p(ime,brTuri,brClenovi);
  86.         drustva[i] = p;
  87.        
  88.     }
  89.    
  90.     pl = drustva[0] + drustva[1];
  91.     cout<<pl;
  92.    
  93.     najmnoguClenovi(drustva, 3);
  94.    
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement