Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- class ArithmeticException
- {
- private:
- char error[40];
- public:
- void message()
- {
- cout<<error<<endl;
- }
- };
- class ArrayFullException
- {
- private:
- char error[40];
- public:
- void message()
- {
- cout<<error<<endl;
- }
- };
- class IndexOutOfBounds
- {
- private:
- int x;
- public:
- IndexOutOfBounds(int x)
- {
- this->x=x;
- }
- void message()
- {
- cout<<"Index "<<x<<" is out of bounds"<<endl;
- }
- };
- class NumbersNotDivisibleException
- {
- private:
- int x;
- public:
- NumbersNotDivisibleException(int x)
- {
- this->x=x;
- }
- void message()
- {
- cout<<"Division by number "<<x<<" is not supported"<<endl;
- }
- };
- class NumberIsNotPositiveException
- {
- private:
- int x;
- public:
- NumberIsNotPositiveException(int x)
- {
- this->x=x;
- }
- void message()
- {
- cout<<"Number "<<x<<" is not positive integer"<<endl;
- }
- };
- class PositiveIntegers
- {
- int *broevi;
- int n;
- int kapacitet;
- public:
- PositiveIntegers(int kap=0)
- {
- kapacitet=kap;
- broevi=new int[0];
- n=0;
- }
- void increaseCapacity(int c)
- {
- int *temp=broevi;
- broevi=new int[kapacitet+c];
- for(int i=0;i<n;i++)
- {
- broevi[i]=temp[i];
- }
- delete [] temp;
- kapacitet=kapacitet+c;
- }
- PositiveIntegers operator+=(int x)
- {
- if(x<=0)
- throw NumberIsNotPositiveException(x);
- if(n==kapacitet)
- throw ArrayFullException();
- int *temp=broevi;
- this->broevi=new int[n+1];
- for(int i=0;i<n;i++)
- {
- broevi[i]=temp[i];
- }
- delete [] temp;
- broevi[n++]+=x;
- return *this;
- }
- PositiveIntegers operator*(int x)
- {
- for(int i=0;i<n;i++)
- {
- broevi[i]*=x;
- }
- return *this;
- }
- PositiveIntegers operator/(int x)
- {
- if (x==0)
- throw ArithmeticException("Division by zero is not allowed");
- for(int i=0;i<n;i++)
- {
- if(broevi[i]%x!=0)
- throw NumbersNotDivisibleException(x);
- else
- {
- broevi[i]/=x;
- }
- }
- return *this;
- }
- int operator[](int i)
- {
- if(i<0 || i>n-1)
- throw IndexOutOfBoundsException(i)
- return broevi[i];
- }
- void print()
- {
- cout<<"Size: "<<n<<" Capacity: "<<kapacitet<<" Numbers: ";
- for(int i=0;i<n;i++)
- cout<<broevi[i]<<" ";
- cout<<endl;
- }
- };
- int main() {
- int n,capacity;
- cin >> n >> capacity;
- PositiveIntegers pi (capacity);
- for (int i=0;i<n;i++){
- int number;
- cin>>number;
- try{
- pi+=number;
- }
- catch(NumberIsNotPositiveException &a)
- {
- a.message();
- }
- catch(ArrayFullException &a)
- {
- a.message();
- }
- }
- cout<<"===FIRST ATTEMPT TO ADD NUMBERS==="<<endl;
- pi.print();
- int incCapacity;
- cin>>incCapacity;
- pi.increaseCapacity(incCapacity);
- cout<<"===INCREASING CAPACITY==="<<endl;
- pi.print();
- int n1;
- cin>>n1;
- for (int i=0;i<n1;i++){
- int number;
- cin>>number;
- try{
- pi+=number;
- }
- catch(NumberIsNotPositiveException &a)
- {
- a.message();
- }
- catch(ArrayFullException &a)
- {
- a.message();
- }
- }
- cout<<"===SECOND ATTEMPT TO ADD NUMBERS==="<<endl;
- pi.print();
- PositiveIntegers pi1;
- cout<<"===TESTING DIVISION==="<<endl;
- try{
- pi1 = pi/0;
- pi1.print();
- pi1 = pi/1;
- pi1.print();
- pi1 = pi/2;
- pi1.print();
- }catch(NumbersNotDivisibleException &a)
- {
- a.message();
- }
- catch(ArithmeticException &a)
- {
- a.message();
- }
- cout<<"===TESTING MULTIPLICATION==="<<endl;
- pi1 = pi*3;
- pi1.print();
- try{
- cout<<"===TESTING [] ==="<<endl;
- cout<<"PositiveIntegers[-1] = "<<pi[-1]<<endl;
- cout<<"PositiveIntegers[2] = "<<pi[2]<<endl;
- cout<<"PositiveIntegers[3] = "<<pi[3]<<endl;
- cout<<"PositiveIntegers[12] = "<<pi[12]<<endl;
- }catch(IndexOutOfBoundsException &a)
- {
- a.message();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement