2

 avatar
gorazd
c_cpp
a year ago
4.6 kB
9
Indexable
#include <iostream>
using namespace std;

class List {
    int *nums;
    int numberOfElements;
public:
    List() {
        nums = nullptr;
        numberOfElements = 0;
    }
    List(int *_nums, int n) {
        nums = new int[n];
        for (int i = 0; i<n; i++)
            nums[i]=_nums[i];
        numberOfElements = n;
    }
    ~List() {
        delete[] nums;
    }
    List(const List &l) {
        numberOfElements = l.numberOfElements;
        nums = new int[numberOfElements];
        for (int i = 0; i<numberOfElements; i++)
            nums[i] = l.nums[i];
    }
    List &operator=(const List &l) {
        if (this!=&l) {
            delete[] nums;

            numberOfElements = l.numberOfElements;
            nums = new int[numberOfElements];
            for (int i = 0; i<numberOfElements; i++)
                nums[i] = l.nums[i];
        }
        return *this;
    }

    int getNumberOfElements(){return numberOfElements;}

    int sum() {
        int sum = 0;
        for (int i = 0; i<numberOfElements; i++)
            sum+=nums[i];
        return sum;
    }
    double average() {
        double sum = 0;
        for (int i = 0; i<numberOfElements; i++)
            sum+=nums[i];
        return sum/(double)numberOfElements;
    }

    void pecati() {
        cout<<numberOfElements<<": ";
        for (int i = 0; i<numberOfElements; i++) {
            cout<<nums[i]<<" ";
        }
        cout<<"sum: "<<sum()<<" average: "<<average()<<" ";
    }
};
class ListContainer {
    List *listi;
    int numberOfElements;
    int s_attempts;
    int f_attempts;

public:
    ListContainer() {
        s_attempts = 0;
        f_attempts = 0;
        numberOfElements = 0;
        listi = nullptr;
    }
    ListContainer(const ListContainer &lc) {
        s_attempts = lc.s_attempts;
        f_attempts = lc.f_attempts;
        numberOfElements = lc.numberOfElements;
        listi = new List[numberOfElements];
        for (int i = 0; i<numberOfElements; i++)
            listi[i] = lc.listi[i];
    }
    ~ListContainer(){if (listi) delete[] listi;}
    ListContainer & operator=(const ListContainer &lc) {
        if (this!=&lc) {
            delete[] listi;
            s_attempts = lc.s_attempts;
            f_attempts = lc.f_attempts;
            numberOfElements = lc.numberOfElements;
            listi = new List[numberOfElements];
            for (int i = 0; i<numberOfElements; i++)
                listi[i] = lc.listi[i];
        }
        return *this;
    }

    int sum() {
        int sum = 0;
        for (int i = 0; i < numberOfElements; i++) {
                sum+=listi[i].sum();
        }
        return sum;
    }

    double average() {
        if (numberOfElements == 0 ) return 0;
        double av = 0.0;
        int totalCount = 0;
        for (int i = 0; i<numberOfElements; i++) {
            av+=listi[i].sum();
            totalCount+=listi[i].getNumberOfElements();
        }
        return totalCount == 0 ? 0 : av/static_cast<double>(totalCount);
    }

    void print() {
        if (numberOfElements == 0) {
            cout<<"The list is empty"<<endl;
            return;
        }
        for (int i = 0; i < numberOfElements; i++) {
            cout<<"List number: "<<i+1<<" List info: ";
            listi[i].pecati();
            cout<<endl;
        }
        cout<<"Sum: "<<sum()<<" Average: "<<average()<<endl;
        cout<<"Successful attempts: "<<s_attempts<<" Failed attempts: "<<f_attempts<<endl;

    }

    void addNewList(List &l) {
        for (int i = 0; i<numberOfElements; i++)
            if (l.sum()==listi[i].sum()) {
                f_attempts++;
                return;
            }
        List *temp = new List[numberOfElements+1];
        for (int i = 0; i<numberOfElements; i++)
            temp[i] = listi[i];
        temp[numberOfElements] = l;
        delete[] listi;
        listi = temp;
        numberOfElements++;
        s_attempts++;
    }
};

int main() {

    ListContainer lc;
    int N;
    cin>>N;

    for (int i=0;i<N;i++) {
        int n;
        int niza[100];

        cin>>n;

        for (int j=0;j<n;j++){
            cin>>niza[j];

        }

        List l=List(niza,n);

        lc.addNewList(l);
    }


    int testCase;
    cin>>testCase;

    if (testCase==1) {
        cout<<"Test case for operator ="<<endl;
        ListContainer lc1;
        lc1.print();
        cout<<lc1.sum()<<" "<<lc.sum()<<endl;
        lc1=lc;
        lc1.print();
        cout<<lc1.sum()<<" "<<lc.sum()<<endl;
        lc1.sum();
        lc1.average();

    }
    else {
        lc.print();
    }
}
Editor is loading...
Leave a Comment