Advertisement
Guest User

Untitled

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