Mitrezzz

Листа од цели броеви

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