Advertisement
MikiStrail

ООП Лаб 5

Apr 15th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.19 KB | None | 0 0
  1. 1. Матрица
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class Matrica {
  9. private:
  10.     int brojNaElementi;
  11.     float arr[10][10];
  12. public:
  13.     Matrica() {
  14.         brojNaElementi = 10;
  15.         for(int i=0; i<brojNaElementi; i++)
  16.             for(int j=0; j<brojNaElementi; j++)
  17.                 arr[i][j] = 0;
  18.     }
  19.     Matrica(int brojNaElementi, float *arr[]) {
  20.         this->brojNaElementi = brojNaElementi;
  21.         for(int i=0; i<brojNaElementi; i++)
  22.             for(int j=0; j<brojNaElementi; j++)
  23.                 this->arr[i][j] = arr[i][j];
  24.     }
  25.     Matrica& operator+(const int n) {
  26.         for(int i=0; i < brojNaElementi; i++)
  27.             for(int j=0; j < brojNaElementi; j++)
  28.                 arr[i][j] += n;
  29.         return *this;
  30.     }
  31.     Matrica& operator-(const Matrica& m) {
  32.         for(int i=0; i < brojNaElementi; i++)
  33.             for(int j=0; j < brojNaElementi; j++)
  34.                 arr[i][j] -= m.arr[i][j];
  35.         return *this;
  36.     }
  37.     Matrica operator*(const Matrica& m) {
  38.         Matrica temp;
  39.         for(int i=0; i < brojNaElementi; i++)
  40.             for(int j=0; j < brojNaElementi; j++) {
  41.                 for(int l=0; l < brojNaElementi; l++)
  42.                     temp.arr[i][j] += arr[i][l] * m.arr[l][j];                 
  43.             }
  44.         return temp;
  45.     }
  46.     friend ostream& operator<<(ostream &screamAndShout, const Matrica &m) {
  47.         for(int i=0; i < m.brojNaElementi; i++) {
  48.             for(int j=0; j < m.brojNaElementi; j++)
  49.                 screamAndShout << m.arr[i][j] << " ";
  50.             screamAndShout << endl;
  51.         }
  52.         return screamAndShout;
  53.     }
  54.     friend istream& operator>>(istream&, Matrica&);
  55. };
  56.  
  57. istream& operator>>(istream &takeItIn, Matrica &m) {
  58.     takeItIn >> m.brojNaElementi >> m.brojNaElementi;
  59.     for(int i=0; i < m.brojNaElementi; i++)
  60.         for(int j=0; j < m.brojNaElementi; j++)
  61.             takeItIn >> m.arr[i][j];
  62.     return takeItIn;
  63.     }
  64.  
  65.  
  66. int main()
  67. {
  68.     Matrica A, B, C;
  69.     cin >> A >> B >> C;
  70.     Matrica D = B * C;
  71.     Matrica R = A - D + 2;
  72.     cout << R;
  73. }
  74.  
  75. 2. Планинарско друштво
  76. #include <iostream>
  77. #include <cstring>
  78. using namespace std;
  79.  
  80. class PlDrustvo
  81. {
  82. private:
  83.     char *name;
  84.     int toursPassed, members;
  85. public:
  86.     PlDrustvo(const char *name="", const int toursPassed=0, const int members=0)
  87.     {
  88.         this->name=new char[strlen(name)+1];
  89.         strcpy(this->name, name);
  90.         this->toursPassed=toursPassed;
  91.         this->members=members;
  92.     }
  93.     PlDrustvo(const PlDrustvo &r)
  94.     {
  95.         name=new char[strlen(r.name)+1];
  96.         strcpy(name, r.name);
  97.         toursPassed=r.toursPassed;
  98.         members=r.members;
  99.     }
  100.     PlDrustvo &operator=(const PlDrustvo &r)
  101.     {
  102.         if(this!=&r)
  103.         {
  104.             delete [] name;
  105.             name=new char[strlen(r.name)+1];
  106.             strcpy(name, r.name);
  107.             toursPassed=r.toursPassed;
  108.             members=r.members;
  109.         }
  110.         return *this;
  111.     }
  112.     ~PlDrustvo()
  113.     {
  114.         delete [] name;
  115.     }
  116.     friend ostream &operator<<(ostream &out, const PlDrustvo &o)
  117.     {
  118.         return out<<"Ime: "<<o.name<<" Turi: "<<o.toursPassed<<" Clenovi: "<<o.members<<endl;
  119.     }
  120.     PlDrustvo operator+(const PlDrustvo &r)
  121.     {
  122.         PlDrustvo newDrustvo;
  123.         newDrustvo.members=members+r.members;
  124.         delete [] newDrustvo.name;
  125.         if(members>r.members)
  126.         {
  127.             newDrustvo.name=new char[strlen(name)+1];
  128.             strcpy(newDrustvo.name, name);
  129.             newDrustvo.toursPassed=toursPassed;
  130.         }
  131.         else
  132.         {
  133.             newDrustvo.name=new char[strlen(r.name)+1];
  134.             strcpy(newDrustvo.name, r.name);
  135.             newDrustvo.toursPassed=r.toursPassed;
  136.         }
  137.         return newDrustvo;
  138.     }
  139.     bool operator>(const PlDrustvo &r) { return members>r.members; }
  140.     bool operator<(const PlDrustvo &r) { return members<r.members; }
  141. };
  142.  
  143. void najmnoguClenovi(PlDrustvo *dr, int n)
  144. {
  145.     PlDrustvo tmp=dr[0];
  146.     for(int i=1; i<n; i++)
  147.     {
  148.         if(dr[i]>tmp)
  149.         {
  150.             tmp=dr[i];
  151.         }
  152.     }
  153.     cout<<"Najmnogu clenovi ima planinarskoto drustvo: "<<tmp;
  154. }
  155.  
  156. int main()
  157. {              
  158.     PlDrustvo drustva[3];
  159.     PlDrustvo pl;
  160.     for (int i=0;i<3;i++)
  161.     {
  162.         char ime[100];
  163.         int brTuri;
  164.         int brClenovi;
  165.         cin>>ime;
  166.         cin>>brTuri;
  167.         cin>>brClenovi;
  168.         PlDrustvo p(ime,brTuri,brClenovi);
  169.         drustva[i] = p;
  170.     }
  171.     pl = drustva[0] + drustva[1];
  172.     cout<<pl;
  173.     najmnoguClenovi(drustva, 3);
  174.     return 0;
  175. }
  176.  
  177. 3. Автомобил
  178. #include <iostream>
  179. #include <cstring>
  180. using namespace std;
  181. class Automobile{
  182.     char *marka;
  183.     int *registracija;
  184.     int maxSpeed;
  185. public:
  186.     Automobile(){marka=0;registracija=0;}
  187.     Automobile(char *marka,int *registracija,int maxSpeed)
  188.     {
  189.         this->marka = new char[strlen(marka)+1];
  190.             strcpy(this->marka,marka);
  191.         this->registracija = new int[5];
  192.         for(int i=0;i<5;i++)
  193.         {
  194.             this->registracija[i]=registracija[i];
  195.            
  196.         }
  197.         this->maxSpeed=maxSpeed;
  198.     }
  199.     Automobile(const Automobile &a)
  200.     {
  201.         marka = new char[strlen(a.marka)+1];
  202.         strcpy(marka,a.marka);
  203.         registracija = new int[5];
  204.         for(int i=0;i<5;i++)
  205.         {
  206.             registracija[i]=a.registracija[i];
  207.         }
  208.         maxSpeed=a.maxSpeed;
  209.     }
  210.     Automobile & operator =(const Automobile &a)
  211.     {
  212.         if(this!=&a)
  213.         {
  214.         marka = new char[strlen(a.marka)+1];
  215.         strcpy(marka,a.marka);
  216.         registracija = new int[5];
  217.         for(int i=0;i<5;i++)
  218.         {
  219.             registracija[i]=a.registracija[i];
  220.         }
  221.         maxSpeed=a.maxSpeed;
  222.         }
  223.         return *this;
  224.     }
  225.     int getMax()
  226.     {
  227.         return maxSpeed;
  228.     }
  229.     ~Automobile(){
  230.         delete [] marka;
  231.         delete [] registracija;
  232.     }
  233.     bool operator ==(const Automobile &a)
  234.     {
  235.         for(int i=0;i<5;i++)
  236.         {
  237.             if(registracija[i]!=a.registracija[i])
  238.             return false;
  239.         }
  240.         return true;
  241.     }
  242.     friend ostream & operator <<(ostream &out,const Automobile &a)
  243.     {
  244.         out<<"Marka\t"<<a.marka<<"\tRegistracija[ ";
  245.         for(int i=0;i<5;i++)
  246.         {
  247.             out<<a.registracija[i]<<" ";
  248.         }
  249.         out<<"]";
  250.         return out;
  251.     }
  252. };
  253. class RentACar{
  254.     char ime[100];
  255.     Automobile *cars;
  256.     int broj;
  257. public:
  258.     RentACar(){cars=0;broj=0;}
  259.     RentACar(const char *ime)
  260.     {
  261.         strcpy(this->ime,ime);
  262.         broj=0;
  263.     }
  264.     RentACar(const RentACar &r)
  265.     {
  266.         strcpy(ime,r.ime);
  267.         cars = new Automobile[r.broj];
  268.         for(int i=0;i<r.broj;i++)
  269.         {
  270.             cars[i]=r.cars[i];
  271.         }
  272.         broj = r.broj;
  273.     }
  274.     ~RentACar(){delete [] cars;}
  275.     RentACar & operator +=(const Automobile &a)
  276.     {
  277.         Automobile *temp = cars;
  278.         cars = new Automobile[broj+1];
  279.         for(int i=0;i<broj;i++)
  280.         {
  281.             cars[i]=temp[i];
  282.         }
  283.         cars[broj]=a;
  284.         broj++;
  285.         return *this;
  286.     }
  287.     RentACar & operator -=(const Automobile &a)
  288.     {
  289.         for(int i=0;i<broj;i++)
  290.         {
  291.             if(cars[i]==a)
  292.             {
  293.                 for(int j=i;j<broj-1;j++)
  294.                 {
  295.                     cars[j]=cars[j+1];
  296.                 }
  297.                 broj--;
  298.             }
  299.            
  300.         }
  301.         return *this;
  302.     }
  303.     void pecatiNadBrzina(int max)
  304.     {
  305.         cout<<ime<<endl;
  306.         for(int i=0;i<broj;i++)
  307.         {
  308.             if(cars[i].getMax()>max)
  309.                 cout<<cars[i];
  310.         }
  311.     }
  312. };
  313. int main()
  314. {
  315.    RentACar agencija("FINKI-Car");
  316.    int n;
  317.    cin>>n;
  318.    
  319.    for (int i=0;i<n;i++)
  320.    {
  321.         char marka[100];
  322.         int regisracija[5];
  323.         int maximumBrzina;
  324.    
  325.         cin>>marka;
  326.    
  327.         for (int i=0;i<5;i++)
  328.             cin>>regisracija[i];
  329.    
  330.         cin>>maximumBrzina;
  331.    
  332.         Automobile nov=Automobile(marka,regisracija,maximumBrzina);
  333.        
  334.         //dodavanje na avtomobil
  335.         agencija+=nov;  
  336.        
  337.    }
  338.     //se cita grehsniot avtmobil, za koj shto avtmobilot so ista registracija treba da se izbrishe
  339.     char marka[100];
  340.     int regisracija[5];
  341.     int maximumBrzina;
  342.     cin>>marka;
  343.     for (int i=0;i<5;i++)
  344.     cin>>regisracija[i];
  345.     cin>>maximumBrzina;
  346.    
  347.     Automobile greshka=Automobile(marka,regisracija,maximumBrzina);
  348.    
  349.     //brishenje na avtomobil
  350.     agencija-=greshka;
  351.    
  352.     agencija.pecatiNadBrzina(150);
  353.    
  354.     return 0;
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement