Advertisement
MiaShuki

Ispit.1

Feb 2nd, 2021
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. class InstrumentTypeAlreadyExists {
  4. char msg[100];
  5.  
  6. };
  7.  
  8. using namespace std;
  9. class MusicalInstrument {
  10. private:
  11.     char *niza; //име: динамички алоцирана низа од знаци
  12.     char instrument[10];//вид на музичкиот инструмент: низа од најмногу 10 знаци
  13.     int cena;//цена на чинење на музичкиот инструмент: цел број
  14.     int godini;//години на старост на музичкиот инструмент: цел број
  15.     bool muzejskaVrednost//променлива што означува дали музичкиот инструмент има музејска вредност: boolean вредност
  16. public:
  17.     MusicalInstrument(){
  18.        strcpy(this->instrument,"");
  19.        this->cena = 0;
  20.        this->godini = 0;
  21.        this->muzejskaVrednost = False;
  22.     }
  23.    
  24.     MusicalInstrument(char *niza){
  25.        strcpy(this->instrument,instrument);
  26.        this->cena = cena;
  27.        this->godini = godini;
  28.     }
  29.    
  30.     MusicalInstrument(const MusicalInstrument &copy){
  31.        strcpy(this->instrument,copy.instrument);
  32.        this->cena = copy.cena;
  33.        this->godini = copy.godini;
  34.     }
  35.    
  36.     MusicalInstrument(&copy){
  37.     if(this!= &copy){
  38.         strcpy(this->instrument,copy.instrument);
  39.        this->cena = copy.cena;
  40.        this->godini = copy.godini;
  41.     }return *this;
  42.     }
  43.    
  44.     MusicalInstrument& operator+=(const dodatok &c){
  45.         if(muzejskaVrednost == True)
  46.             cena = dodatok + cena;
  47.         return cena;
  48.     }
  49.    
  50.     friend ostream &operator<<(ostream &o, MusicalInstrument &m){
  51.     o<<print.niza<<" - "<<print.instrument<<endl;
  52.     o<<print.godini<<", "<<print.cena<<endl;
  53.     return o;
  54.     }
  55.    
  56.     };
  57.    
  58. class Band{
  59. private:
  60.     char ime[100];//име: низа од најмногу 100 знаци
  61.     MusicalInstrument instrumenti;//инструменти: динамички алоцирана низа од објекти од класата MusicalInstrument
  62.     int br;//број на музички инструменти
  63. public:
  64.     Band(){
  65.         strcpy(this->ime,"");
  66.     }
  67.    
  68.     Band(char *ime){
  69.         strcpy(this->ime,ime);
  70.         this->instrumenti = new MusicalInstrument[0];
  71.         this->br = 0;
  72.     }
  73.    
  74.     Band(const Band &copy){
  75.         this->instrumenti = new MusicalInstrument[copy.br];
  76.         for(int i = 0; i<br; i++)
  77.             this->instrumenti[i] = copy.instrumenti[i];
  78.     }
  79.    
  80.     Band(&copy){
  81.     if(this!= &copy){
  82.         this->instrumenti = new MusicalInstrument[copy.br];
  83.         for(int i = 0; i<br; i++)
  84.             this->instrumenti[i] = copy.instrumenti[i];
  85.     }return *this;
  86.     }
  87.    
  88.     friend ostream &operator<<(ostream &o, MusicalInstrument &m){
  89.     o<<print.ime<<endl;
  90.     for(int i = 0; i<br; i++){o<<print.instrumenti[i]<<endl;}
  91.     return o;}
  92.    
  93.     Band &operator+=( instrumenti &add){
  94.         if(this->instrumenti == instrumenti)
  95.             throw InstrumentTypeAlreadyExists
  96.             else{
  97.                 MusicalInstrument *tmp = new MusicalInstrument[br + 1];
  98.                 for(int i = 0; i < br; i++){
  99.                     tmp[i] = niza[i];
  100.                     tmp[br++] = add;
  101.                     niza = tmp;
  102.                 }
  103.                 return *this;
  104.             }
  105.     }
  106.    
  107.     };
  108. void oldest_valuable_instrument(Band *bend, int br){
  109.    
  110. }
  111. int main(){
  112.     int test;
  113.     cin >> test;
  114.     if(test == 1){
  115.         cout << "Testing class MusicalInstrument" << endl;
  116.         char name[50], type[50];
  117.         int price, age;
  118.         bool museumValue;
  119.         cin >> name >> type >> price >> age >> museumValue;
  120.         MusicalInstrument a;
  121.         MusicalInstrument b(name, type, price, age, museumValue);
  122.         a=b;
  123.         cout << a.getName() << endl;
  124.         cout << b.getPrice() << endl;
  125.         cout << b.getAge() << endl;
  126.     }
  127.     else if(test == 2){
  128.         cout << "Testing operators += and << in the class MusicalInstrument" << endl;
  129.         char name[50], type[50];
  130.         int price, age;
  131.         bool muaseumValue;
  132.         cin >> name >> type >> price >> age >> muaseumValue;
  133.         MusicalInstrument a(name, type, price, age, muaseumValue);
  134.         a += 100;
  135.         cout << a << endl;
  136.     }
  137.     else if(test == 3){
  138.         cout << "Testing class Band constructor" << endl;
  139.         char name[50];
  140.         cin >> name;
  141.         Band b(name);
  142.         cout << b.getName() << endl;
  143.     }
  144.     else if(test == 4){
  145.         cout << "Testing class Band operator<<" << endl;
  146.         char ime[50], tip[50];
  147.         int cena_cinenje, godini_starost;
  148.         bool muzejska_vrednost;
  149.         cin>>ime;
  150.         Band b(ime);
  151.         for(int i = 0; i < 2; i++){
  152.             cin >> ime >> tip >> cena_cinenje >> godini_starost >> muzejska_vrednost;
  153.             MusicalInstrument a(ime, tip, cena_cinenje, godini_starost, muzejska_vrednost);
  154.             b+=a;
  155.         }
  156.         cout << b;
  157.     }
  158.     else if(test == 5){
  159.         cout << "Testing operator+= and class InstrumentTypeAlreadyExists" << endl;
  160.         char name[50], type[50];
  161.         int price, age;
  162.         bool museumValue;
  163.         cin>>name;
  164.         Band band(name);
  165.         cin >> name >> type >> price >> age >> museumValue;
  166.         MusicalInstrument a(name, type, price, age, museumValue);
  167.         cin >> name >> type >> price >> age >> museumValue;
  168.         MusicalInstrument b(name, type, price, age, museumValue);
  169.        
  170.         band += a;
  171.         band += b;
  172.        
  173.         cout << band;
  174.     }
  175.     else if(test == 6){
  176.         cout << "Testing class Band operator==" << endl;
  177.         char name[50], type[50];
  178.         int price, age;
  179.         bool museumValue;
  180.         cin>>name;
  181.         Band band1(name);
  182.         cin>>name;
  183.         Band band2(name);
  184.         for(int i = 0; i < 2; i++){
  185.             cin >> name >> type >> price >> age >> museumValue;
  186.             MusicalInstrument a(name, type, price, age, museumValue);
  187.             band1+=a;
  188.             band2+=a;
  189.         }
  190.         int c;
  191.         cin>>c;
  192.         if (c){
  193.             cin >> name >> type >> price >> age >> museumValue;
  194.             MusicalInstrument a(name, type, price, age, museumValue);
  195.             band1+=a;
  196.         }
  197.         if(band1 == band2){
  198.             cout << "True";
  199.         } else{
  200.             cout<<"False";
  201.         }
  202.     }
  203.     else if(test == 7){
  204.         cout << "Testing oldest_valuable_instrument" << endl;
  205.         Band b[2];
  206.         char name[50], type[50];
  207.         int price, age;
  208.         bool museumValue;
  209.         for(int i=0;i<2;i++){
  210.             cin>>name;
  211.             b[i].setIme(name);
  212.             for(int j=0;j<2;j++){
  213.                 cin >> name >> type >> price >> age >> museumValue;
  214.                 MusicalInstrument a(name, type, price, age, museumValue);
  215.                 b[i]+=a;
  216.             }
  217.         }
  218.         oldest_valuable_instrument(b,2);
  219.  
  220.     }
  221.     return 0;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement