masterm1nd99

brooooooo

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