Advertisement
Dimoo23

Lista od celi broevi - preoptovaruvanje operatori

May 20th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cmath>
  4.  
  5. using namespace std;
  6.  
  7. class List{
  8. private:
  9.     int *niza;
  10.     int broj;
  11. public:
  12.     // default constructor
  13.     List(){ niza=new int [0]; broj=0;}
  14.     // constructor so argument
  15.     List(int n){
  16.         niza=new int [n];
  17.         broj=n;
  18.     }
  19.     // copy constructor
  20.     List(const List &l){
  21.         broj=l.broj;
  22.         niza= new int [broj];
  23.         for(int i=0;i<broj;i++){
  24.             niza[i]=l.niza[i];
  25.         }
  26.     }
  27.     // operator =
  28.     List & operator=(const List &l){
  29.         if(this!=&l){
  30.             delete [] niza;
  31.             broj=l.broj;
  32.             niza= new int [broj];
  33.             for(int i=0;i<broj;i++){
  34.             niza[i]=l.niza[i];
  35.         }
  36.         }
  37.         return *this;
  38.     }
  39.     // destructor
  40.     ~List(){ delete [] niza; }
  41.     int sum (){
  42.         int suma=0;
  43.         for(int i=0;i<broj;i++){
  44.             suma+=niza[i];
  45.         }
  46.         return suma;
  47.     }
  48.     double average () { return 1.0*sum()/broj; }
  49.     //friend funkcii
  50.     friend bool operator==(const List &l1, const List &l2);
  51.     friend ostream & operator<<(ostream &out,  List &l);
  52.     friend istream & operator>>(istream &in,  List &l);
  53.     friend List operator +(const List &l1,const List &l2);
  54.     friend List operator-(const List &l1,const List &l2);
  55.     // operator ++ prefix
  56.     List & operator++(){
  57.         for(int i=0;i<broj;++i)
  58.             ++niza[i];
  59.         return *this;
  60.     }
  61.     // operator -- postfix
  62.     List operator--(int){
  63.         List tmp(*this);
  64.         for(int i=0;i<broj;++i)
  65.             --niza[i];
  66.         return tmp;
  67.     }
  68.     // operator []
  69.     int & operator[](int index){
  70.         return niza[index];
  71.     }
  72.     List & operator+=(int ind){
  73.         int isti=0;
  74.         for(int i=0;i<broj;++i)
  75.             if(ind==niza[i])
  76.                 ++isti;
  77.         if(isti<2){
  78.             int *tmp=new int [broj+1];
  79.             for(int i=0;i<broj;++i)
  80.                 tmp[i]=niza[i];
  81.             tmp[broj]=ind;
  82.             broj++;
  83.             delete [] niza;
  84.             niza=tmp;
  85.         }
  86.         return *this;
  87.     }
  88.     List addList(List l){
  89.         List nova;
  90.         // nova.broj=broj+l.broj;
  91.         // nova.niza=new int [nova.broj];
  92.         for(int i=0;i<broj;++i)
  93.             nova+=niza[i];
  94.         for(int i=0;i<l.broj;++i)
  95.             nova+=l.niza[i];
  96.         return nova;
  97.     }
  98. };
  99. // operator ==
  100. bool operator==( List &l1,  List &l2){
  101.     return l1.sum()==l2.sum();
  102. }
  103. // operator <<
  104. ostream & operator<<(ostream &out,  List &l){
  105.     if(l.broj>0){
  106.     out<<l.broj<<": ";
  107.     for(int i=0;i<l.broj;i++)
  108.         out<<l.niza[i]<<' ';
  109.     out<<"sum: "<<l.sum()<<" average: "<<l.average()<<endl;
  110.     return out;
  111.     }
  112.     out<<"The list is empty";
  113.     return out;
  114. }
  115. // operator >>
  116. istream & operator>>(istream &in,  List &l){
  117.     in>>l.broj;
  118.     l.niza= new int [l.broj];
  119.     for(int i=0;i<l.broj;i++)
  120.         in>>l.niza[i];
  121.     return in;
  122. }
  123. // operator +
  124. List operator +(const List &l1,const List &l2){
  125.     if(l1.broj==l2.broj){
  126.         List k(l1.broj);
  127.         for(int i=0;i<k.broj;i++)
  128.             k.niza[i]=l1.niza[i]+l2.niza[i];
  129.         return k;
  130.     }
  131.         List l;
  132.         return l;
  133. }
  134. // operator -
  135. List operator-(const List &l1,const List &l2){
  136.     if(l1.broj==l2.broj){
  137.         List k(l1.broj);
  138.         for(int i=0;i<k.broj;i++){
  139.             k.niza[i]=l1.niza[i]-l2.niza[i];
  140.         }
  141.         return k;
  142.     }
  143.     List l;
  144.     return l;
  145. }
  146.  
  147. int main() {
  148.  
  149.     int testCase;
  150.     cin>>testCase;
  151.  
  152.     if (testCase==1){
  153.         cout<<"Testiranje na operatorite << i >>"<<endl;
  154.         List l;
  155.         cin >> l;
  156.         cout<< l;
  157.     }
  158.     else if (testCase == 2){
  159.         cout<<"Testiranje na operatorot = za klasata List"<<endl;
  160.         List l1;
  161.         cin >> l1;
  162.         List l2;
  163.         l2 = l1;
  164.         cout << l2;
  165.     }
  166.     else if (testCase == 3){
  167.         //2 test primeri tuka
  168.         cout << "Testiranje na operatorot +"<<endl;
  169.         List l1,l2;
  170.         cin >> l1;
  171.         cin >> l2;
  172.         List l3;
  173.         l3 = l1+l2;
  174.         cout << l3;
  175.     }
  176.     else if (testCase == 4){
  177.  
  178.         cout << "Testiranje na operatorot - "<<endl;
  179.         List l1,l2;
  180.         cin >> l1;
  181.         cin >> l2;
  182.         List l3;
  183.         l3 = l1-l2;
  184.         cout << l3;
  185.     }
  186.     else if (testCase == 5){
  187.         cout << "Testiranje na operator ++ vo prefix notacija"<<endl;
  188.         List l1;
  189.         cin >> l1;
  190.         cout << l1;
  191.         cout << ++l1;
  192.     }
  193.     else if (testCase == 6){
  194.         cout << "Testiranje na operatorot -- vo sufix notacija"<<endl;
  195.         List l1;
  196.         cin >> l1;
  197.         cout << "Pocetna vrednost: " << l1;
  198.         List l2;
  199.         l2 = l1--;
  200.         cout << "Se povikuva ++ vo sufiks: " << l2;
  201.         cout << "Posle povik: " << l1;
  202.     }
  203.     else if (testCase == 7) {
  204.  
  205.         cout << "Testiranje na operator += "<<endl;
  206.         List l1;
  207.         cin >> l1;
  208.         int n;
  209.         cin >> n;
  210.         for (int i=0;i<n;i++){
  211.             int broj;
  212.             cin >> broj;
  213.             l1 += broj;
  214.         }
  215.         cout << l1;
  216.     }
  217.     else if (testCase == 8){
  218.         cout << "Testiranje na operatorot [ ] "<< endl;
  219.         List l1;
  220.         cin >> l1;
  221.         int n;
  222.         cin >> n;
  223.         for (int i=0;i<n;i++){
  224.             int idx;
  225.             cin >> idx;
  226.             cout << "List["<< idx <<"] = " << l1[idx] << endl;
  227.         }
  228.     }
  229.     else {
  230.         //Dopolnitelno baranje
  231.         cout <<"Testiranje na addList() metodot"<<endl;
  232.         List l1,l2,l3;
  233.         cin >> l1;
  234.         cin >> l2;
  235.         l3 = l1.addList(l2);
  236.         //cout << l1.addList(l2);
  237.         cout<<l3;
  238.     }
  239.  
  240.     return 0;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement