Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- #include<cmath>
- using namespace std;
- class List{
- private:
- int *niza;
- int broj;
- public:
- // default constructor
- List(){ niza=new int [0]; broj=0;}
- // constructor so argument
- List(int n){
- niza=new int [n];
- broj=n;
- }
- // copy constructor
- List(const List &l){
- broj=l.broj;
- niza= new int [broj];
- for(int i=0;i<broj;i++){
- niza[i]=l.niza[i];
- }
- }
- // operator =
- List & operator=(const List &l){
- if(this!=&l){
- delete [] niza;
- broj=l.broj;
- niza= new int [broj];
- for(int i=0;i<broj;i++){
- niza[i]=l.niza[i];
- }
- }
- return *this;
- }
- // destructor
- ~List(){ delete [] niza; }
- int sum (){
- int suma=0;
- for(int i=0;i<broj;i++){
- suma+=niza[i];
- }
- return suma;
- }
- double average () { return 1.0*sum()/broj; }
- //friend funkcii
- friend bool operator==(const List &l1, const List &l2);
- friend ostream & operator<<(ostream &out, List &l);
- friend istream & operator>>(istream &in, List &l);
- friend List operator +(const List &l1,const List &l2);
- friend List operator-(const List &l1,const List &l2);
- // operator ++ prefix
- List & operator++(){
- for(int i=0;i<broj;++i)
- ++niza[i];
- return *this;
- }
- // operator -- postfix
- List operator--(int){
- List tmp(*this);
- for(int i=0;i<broj;++i)
- --niza[i];
- return tmp;
- }
- // operator []
- int & operator[](int index){
- return niza[index];
- }
- List & operator+=(int ind){
- int isti=0;
- for(int i=0;i<broj;++i)
- if(ind==niza[i])
- ++isti;
- if(isti<2){
- int *tmp=new int [broj+1];
- for(int i=0;i<broj;++i)
- tmp[i]=niza[i];
- tmp[broj]=ind;
- broj++;
- delete [] niza;
- niza=tmp;
- }
- return *this;
- }
- List addList(List l){
- List nova;
- // nova.broj=broj+l.broj;
- // nova.niza=new int [nova.broj];
- for(int i=0;i<broj;++i)
- nova+=niza[i];
- for(int i=0;i<l.broj;++i)
- nova+=l.niza[i];
- return nova;
- }
- };
- // operator ==
- bool operator==( List &l1, List &l2){
- return l1.sum()==l2.sum();
- }
- // operator <<
- ostream & operator<<(ostream &out, List &l){
- if(l.broj>0){
- out<<l.broj<<": ";
- for(int i=0;i<l.broj;i++)
- out<<l.niza[i]<<' ';
- out<<"sum: "<<l.sum()<<" average: "<<l.average()<<endl;
- return out;
- }
- out<<"The list is empty";
- return out;
- }
- // operator >>
- istream & operator>>(istream &in, List &l){
- in>>l.broj;
- l.niza= new int [l.broj];
- for(int i=0;i<l.broj;i++)
- in>>l.niza[i];
- return in;
- }
- // operator +
- List operator +(const List &l1,const List &l2){
- if(l1.broj==l2.broj){
- List k(l1.broj);
- for(int i=0;i<k.broj;i++)
- k.niza[i]=l1.niza[i]+l2.niza[i];
- return k;
- }
- List l;
- return l;
- }
- // operator -
- List operator-(const List &l1,const List &l2){
- if(l1.broj==l2.broj){
- List k(l1.broj);
- for(int i=0;i<k.broj;i++){
- k.niza[i]=l1.niza[i]-l2.niza[i];
- }
- return k;
- }
- List l;
- return l;
- }
- int main() {
- int testCase;
- cin>>testCase;
- if (testCase==1){
- cout<<"Testiranje na operatorite << i >>"<<endl;
- List l;
- cin >> l;
- cout<< l;
- }
- else if (testCase == 2){
- cout<<"Testiranje na operatorot = za klasata List"<<endl;
- List l1;
- cin >> l1;
- List l2;
- l2 = l1;
- cout << l2;
- }
- else if (testCase == 3){
- //2 test primeri tuka
- cout << "Testiranje na operatorot +"<<endl;
- List l1,l2;
- cin >> l1;
- cin >> l2;
- List l3;
- l3 = l1+l2;
- cout << l3;
- }
- else if (testCase == 4){
- cout << "Testiranje na operatorot - "<<endl;
- List l1,l2;
- cin >> l1;
- cin >> l2;
- List l3;
- l3 = l1-l2;
- cout << l3;
- }
- else if (testCase == 5){
- cout << "Testiranje na operator ++ vo prefix notacija"<<endl;
- List l1;
- cin >> l1;
- cout << l1;
- cout << ++l1;
- }
- else if (testCase == 6){
- cout << "Testiranje na operatorot -- vo sufix notacija"<<endl;
- List l1;
- cin >> l1;
- cout << "Pocetna vrednost: " << l1;
- List l2;
- l2 = l1--;
- cout << "Se povikuva ++ vo sufiks: " << l2;
- cout << "Posle povik: " << l1;
- }
- else if (testCase == 7) {
- cout << "Testiranje na operator += "<<endl;
- List l1;
- cin >> l1;
- int n;
- cin >> n;
- for (int i=0;i<n;i++){
- int broj;
- cin >> broj;
- l1 += broj;
- }
- cout << l1;
- }
- else if (testCase == 8){
- cout << "Testiranje na operatorot [ ] "<< endl;
- List l1;
- cin >> l1;
- int n;
- cin >> n;
- for (int i=0;i<n;i++){
- int idx;
- cin >> idx;
- cout << "List["<< idx <<"] = " << l1[idx] << endl;
- }
- }
- else {
- //Dopolnitelno baranje
- cout <<"Testiranje na addList() metodot"<<endl;
- List l1,l2,l3;
- cin >> l1;
- cin >> l2;
- l3 = l1.addList(l2);
- //cout << l1.addList(l2);
- cout<<l3;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement