Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- #include<cmath>
- using namespace std;
- #include<iostream>
- #include<cstring>
- #include<cmath>
- using namespace std;
- class List
- {
- private:
- int *niza;
- int n;
- public:
- List(){
- this->niza=new int[0];
- this->n=0;
- }
- List(int n)
- {
- this->niza=new int[50];
- for(int i=0;i<n;i++)
- {
- this->niza[i]=niza[i];
- }
- this->n=n;
- }
- List(const List &l)
- {
- this->niza=new int[l.n];
- for(int i=0;i<n;i++)
- {
- this->niza[i]=l.niza[i];
- }
- this->n=l.n;
- }
- List& operator=(const List &l)
- {
- delete []niza;
- this->niza=new int[l.n];
- for(int i=0;i<l.n;i++)
- {
- this->niza[i]=l.niza[i];
- }
- this->n=l.n;
- return *this;
- }
- int sum()
- {
- int suma=0;
- for(int i=0;i<n;i++)
- {
- suma+=niza[i];
- }
- return suma;
- }
- double average()
- {
- double avg=0.0;
- avg=(double)sum()/n;
- return avg;
- }
- bool operator==(List &l)
- {
- if(sum()==l.sum())
- return true;
- else return false;
- }
- friend ostream& operator<<(ostream& out,List& l)
- {
- out<<l.n<<": ";
- for(int i=0;i<l.n;i++)
- {
- out<<l.niza[i]<<" ";
- }
- out<<"sum: "<<l.sum()<<" average: "<<l.average()<<endl;
- return out;
- }
- friend istream& operator>>(istream &inp,List& l)
- {
- inp>>l.n;
- for(int i=0;i<l.n;i++)
- {
- inp>>l.niza[i];
- }
- return inp;
- }
- friend List operator+ (List &l1, List &l2) {
- List x;
- List res(l1);
- if(l1.n==l2.n) {
- for(int i=0; i<l1.n; ++i)
- res.niza[i]+=l2.niza[i];
- return res;
- } else
- cout<<"The list is empty"<<endl;
- }
- List& operator-(const List &l)
- {
- if(n==l.n)
- {
- List tmp = List(n-l.n);
- for(int i=0;i<n;i++)
- {
- tmp.niza[i]=niza[i]-l.niza[i];
- }
- return tmp;
- }
- else return *this;
- }
- List& operator++()
- {
- for(int i=0;i<n;++i)
- {
- ++niza[i];
- }
- return *this;
- }
- List operator++(int)
- {
- List l(*this);
- for(int i=0;i<n;++i)
- {
- niza[i]++;
- }
- return l;
- }
- List& operator--(int)
- {
- for(int i=0;i<n;i++)
- {
- niza[i]--;
- }
- return *this;
- }
- List& operator+=(int l)
- {
- int *tmp=new int[n+1];
- for(int i=0;i<n;i++)
- {
- tmp[i]=niza[i];
- }
- tmp[n++]=l;
- delete []niza;
- niza=tmp;
- return *this;
- }
- int& operator[](int index) {
- return niza[index];
- }
- List& addList(List l)
- {
- List tmp=List(n+l.n);
- for(int i=0;i<n;i++)
- {
- tmp.niza[i]=niza[i]+l.niza[i];
- }
- return tmp;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement