metalni

OOP Labs 9 Broevi

Jun 2nd, 2020
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class ArithmeticException{
  7.     public:
  8.         const void message(){
  9.             cout << "Division by zero is not allowed" << endl;
  10.         }
  11. };
  12. class NumbersNotDivisibleException{
  13.     private:
  14.         int delenik;
  15.     public:
  16.         NumbersNotDivisibleException(const int delenik){
  17.             this->delenik = delenik;
  18.         }
  19.         const void message(){
  20.             cout << "Division by " << this->delenik << " is not supported" << endl;
  21.         }
  22. };
  23. class ArrayFullException {
  24.     public:
  25.         const void message(){
  26.             cout << "The array is full. Increase the capacity" << endl;
  27.         }
  28. };
  29. class IndexOutOfBoundsException{
  30.     private:
  31.         int index;
  32.     public:
  33.         IndexOutOfBoundsException(const int index){
  34.             this->index = index;
  35.         }
  36.         const void message(){
  37.             cout << "Index " << this->index << " is out of bounds" << endl;
  38.         }
  39. };
  40. class NumberIsNotPositiveException{
  41.     private:
  42.         int number;
  43.     public:
  44.         NumberIsNotPositiveException(const int number){
  45.             this->number = number;
  46.         }
  47.         const void message(){
  48.             cout << "Number " << this->number << " is not positive integer" << endl;
  49.         }
  50. };
  51.  
  52.  
  53. class PositiveIntegers{
  54.     private:
  55.         int * integers;
  56.         int noEl;
  57.         int maxSize;
  58.         void copy(const PositiveIntegers &orig){
  59.             this->noEl = orig.noEl;
  60.             this->maxSize = orig.maxSize;
  61.             this->integers = new int[orig.noEl + 1];
  62.             for(int i=0; i<this->noEl; i++)
  63.                 this->integers[i] = orig.integers[i];
  64.         }
  65.     public:
  66.         PositiveIntegers(){
  67.             this->integers = new int[1];
  68.             this->noEl = 0;
  69.             this->maxSize = 0;
  70.         }
  71.         PositiveIntegers(const int maxSize){
  72.             this->integers = new int[maxSize];
  73.             this->noEl = 0;
  74.             this->maxSize = maxSize;
  75.         }
  76.         PositiveIntegers(const PositiveIntegers &orig){
  77.             this->copy(orig);
  78.         }
  79.         PositiveIntegers &operator=(const PositiveIntegers &orig){
  80.             if(this != &orig){
  81.                 delete [] this->integers;
  82.                 this->copy(orig);
  83.             }
  84.             return *this;
  85.         }
  86.         ~PositiveIntegers(){
  87.            
  88.             //delete [] this->integers;
  89.         }
  90.         const void increaseCapacity(const int c){
  91.             int * tmp = new int[this->maxSize + c];
  92.             for(int i=0; i<this->noEl; i++)
  93.                 tmp[i] = this->integers[i];
  94.             delete [] this->integers;
  95.             this->integers = tmp;
  96.             this->maxSize += c;
  97.         }
  98.         PositiveIntegers &operator +=(int newInt){
  99.             if(this->noEl >= this->maxSize)
  100.                 throw ArrayFullException();
  101.             if(newInt <= 0)
  102.                 throw NumberIsNotPositiveException(newInt);
  103.  
  104.             this->integers[this->noEl++] = newInt;
  105.             return *this;
  106.         }
  107.         PositiveIntegers operator*(int orig){
  108.            PositiveIntegers pi (*this);
  109.             for (int i=0;i<pi.noEl;i++)
  110.                 pi.integers[i]*=orig;
  111.        
  112.             return pi;
  113.         }
  114.         PositiveIntegers operator/(int orig){
  115.             if(orig == 0)
  116.                 throw ArithmeticException();
  117.             for(int i=0; i<this->noEl; i++){
  118.                 if(this->integers[i] % orig != 0)
  119.                     throw NumbersNotDivisibleException(orig);
  120.             }
  121.  
  122.             PositiveIntegers pi (*this);
  123.             for (int i=0;i<pi.noEl;i++)
  124.                 pi.integers[i]/=orig;
  125.        
  126.             return pi;
  127.         }
  128.         int &operator[] (int i){
  129.             if((i < 0) || i > this->noEl)
  130.                 throw IndexOutOfBoundsException(i);
  131.             return this->integers[i];
  132.         }
  133.         const void print(){
  134.             cout << "Size: " << this->noEl << " Capacity: " << this->maxSize << " Numbers: ";
  135.             for(int i=0; i<this->noEl; i++)
  136.                 cout << this->integers[i] << " ";
  137.             cout << endl;
  138.         }
  139. };
  140.  
  141. int main() {
  142.    
  143.     int n,capacity;
  144.     cin >> n >> capacity;
  145.     PositiveIntegers pi (capacity);
  146.     for (int i=0;i<n;i++){
  147.         int number;
  148.         cin>>number;
  149.         try{
  150.             pi+=number;  
  151.         }
  152.         catch (ArrayFullException &ex){
  153.             ex.message();
  154.         }
  155.         catch (NumberIsNotPositiveException &ex){
  156.             ex.message();
  157.         }
  158.     }
  159.     cout<<"===FIRST ATTEMPT TO ADD NUMBERS==="<<endl;
  160.     pi.print();
  161.     int incCapacity;
  162.     cin>>incCapacity;
  163.     pi.increaseCapacity(incCapacity);
  164.     cout<<"===INCREASING CAPACITY==="<<endl;
  165.     pi.print();
  166.    
  167.     int n1;
  168.     cin>>n1;
  169.     for (int i=0;i<n1;i++){
  170.         int number;
  171.         cin>>number;
  172.         try{
  173.             pi+=number;    
  174.         }
  175.         catch (ArrayFullException &ex){
  176.             ex.message();
  177.         }
  178.         catch (NumberIsNotPositiveException &ex){
  179.             ex.message();
  180.         }
  181.     }
  182.     cout<<"===SECOND ATTEMPT TO ADD NUMBERS==="<<endl;
  183.    
  184.     pi.print();
  185.     PositiveIntegers pi1;
  186.    
  187.     cout<<"===TESTING DIVISION==="<<endl;  
  188.    
  189.     try{
  190.         pi1 = pi/0;
  191.         pi1.print();
  192.     }
  193.     catch(ArithmeticException &ex){
  194.         ex.message();
  195.     }
  196.     catch(NumbersNotDivisibleException &ex){
  197.         ex.message();
  198.     }
  199.  
  200.     try{
  201.         pi1 = pi/1;
  202.         pi1.print();   
  203.     }
  204.     catch(ArithmeticException &ex){
  205.         ex.message();
  206.     }
  207.     catch(NumbersNotDivisibleException &ex){
  208.         ex.message();
  209.     }
  210.    
  211.     try{
  212.         pi1 = pi/2;
  213.         pi1.print();
  214.     }
  215.     catch(ArithmeticException &ex){
  216.         ex.message();
  217.     }
  218.     catch(NumbersNotDivisibleException &ex){
  219.         ex.message();
  220.     }
  221.    
  222.     cout<<"===TESTING MULTIPLICATION==="<<endl;
  223.     pi1 = pi*3;
  224.     pi1.print();
  225.    
  226.    
  227.     cout<<"===TESTING [] ==="<<endl;   
  228.    
  229.     try{
  230.         cout<<"PositiveIntegers[-1] = "<<pi[-1]<<endl; 
  231.     }
  232.     catch(IndexOutOfBoundsException &ex){
  233.         ex.message();
  234.     }
  235.     try{
  236.         cout<<"PositiveIntegers[2] = "<<pi[2]<<endl;
  237.     }
  238.     catch(IndexOutOfBoundsException &ex){
  239.         ex.message();
  240.     }
  241.     try{
  242.         cout<<"PositiveIntegers[3] = "<<pi[3]<<endl;   
  243.     }
  244.     catch(IndexOutOfBoundsException &ex){
  245.         ex.message();
  246.     }
  247.     try{
  248.         cout<<"PositiveIntegers[12] = "<<pi[12]<<endl;
  249.     }
  250.     catch(IndexOutOfBoundsException &ex){
  251.         ex.message();
  252.     }
  253.  
  254.     return 0;
  255. }
Add Comment
Please, Sign In to add comment