Advertisement
HeatPulse

Numbers

May 2nd, 2019
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Number {
  5. public:
  6. //TODO da se deklariraat trite cisto virtuelni metodi
  7. virtual double doubleValue() = 0;
  8. virtual int intValue() = 0;
  9. virtual void print() = 0;
  10. bool operator==(Number& n)
  11. {
  12. return (this->doubleValue() == n.doubleValue());
  13. }
  14. };
  15.  
  16. //ТODO preoptovaruvanje na operatorot ==
  17.  
  18. class Integer : public Number{ //TODO da se dodadi izraz za nasleduvanje od Number
  19. private:
  20. //TODO da se deklariraat promenlivite
  21. int broj;
  22. public:
  23. //TODO konstruktor so eden argument
  24. Integer(int broj)
  25. {
  26. this->broj = broj;
  27. }
  28. //TODO da se prepokrijat metodite od klasata Number
  29. int intValue()
  30. {
  31. return broj;
  32. }
  33. double doubleValue()
  34. {
  35. return double(broj);
  36. }
  37. void print()
  38. {
  39. cout << broj;
  40. }
  41. };
  42.  
  43. class Double : public Number{ //TODO da se dodadi izraz za nasleduvanje od Number
  44. private:
  45. //TODO da se deklariraat promenlivite
  46. double broj;
  47. public:
  48. //TODO konstruktor so eden argument
  49. Double(double broj)
  50. {
  51. this->broj = broj;
  52. }
  53. //TODO da se prepokrijat metodite od klasata Number
  54. int intValue()
  55. {
  56. return int(broj);
  57. }
  58. double doubleValue()
  59. {
  60. return broj;
  61. }
  62. void print()
  63. {
  64. cout << broj;
  65. }
  66.  
  67. };
  68.  
  69. class Numbers{
  70. private:
  71. //TODO deklariranje na promenlivite
  72. Number** niza;
  73. int brElem;
  74. public:
  75. //TODO default konstruktor
  76. Numbers()
  77. {
  78. niza = new Number*[0];
  79. brElem = 0;
  80. }
  81. //TODO copy konstruktor
  82. Numbers(const Numbers& n)
  83. {
  84. brElem = n.brElem;
  85. niza = new Number*[brElem];
  86. for (int i = 0; i < n.brElem; i++)
  87. {
  88. niza[i] = n.niza[i];
  89. }
  90. }
  91. //TODO operator =
  92. Numbers& operator=(const Numbers& n)
  93. {
  94. brElem = n.brElem;
  95. niza = new Number*[brElem];
  96. for (int i = 0; i < n.brElem; i++)
  97. {
  98. niza[i] = n.niza[i];
  99. }
  100. return *this;
  101. }
  102. //TODO destruktor
  103. ~Numbers()
  104. {
  105. delete[] niza;
  106. }
  107. //TODO operator += (operatorot da prima pointer od objekt od Number, a ne referenca)
  108. Numbers& operator+=(Number* n)
  109. {
  110. bool add = true;
  111. for (int i = 0; i < brElem; i++)
  112. {
  113. if (*niza[i] == *n)
  114. {
  115. add = false;
  116. }
  117. }
  118. if (add)
  119. {
  120. Number** nova = new Number*[brElem + 1];
  121. for (int i = 0; i < brElem; i++)
  122. {
  123. nova[i] = niza[i];
  124. }
  125. if (add)
  126. {
  127. nova[brElem] = n;
  128. brElem++;
  129. }
  130. delete[] niza;
  131. niza = nova;
  132. }
  133. return *this;
  134.  
  135. }
  136. //TODO void statistics()
  137. void statistics()
  138. {
  139. cout << "Count of numbers: " << brElem << endl;
  140. double allSum = 0;
  141. for (int i = 0; i < brElem; i++)
  142. {
  143. allSum += niza[i]->doubleValue();
  144. }
  145. cout << "Sum of all numbers: " << allSum << endl;
  146. int countIntegers = 0;
  147. int intSum = 0;
  148. for (int i = 0; i < brElem; i++)
  149. {
  150. if (dynamic_cast<Integer*>(niza[i]))
  151. {
  152. countIntegers++;
  153. intSum += niza[i]->intValue();
  154. }
  155. }
  156. cout << "Count of integer numbers: " << countIntegers << endl;
  157. cout << "Sum of integer numbers: " << intSum << endl;
  158. int countDoubles = 0;
  159. double doubleSum = 0;
  160. for (int i = 0; i < brElem; i++)
  161. {
  162. if (dynamic_cast<Double*>(niza[i]))
  163. {
  164. countDoubles++;
  165. doubleSum += niza[i]->doubleValue();
  166. }
  167. }
  168. cout <<"Count of double numbers: " << countDoubles << endl;
  169. cout <<"Sum of double numbers: " << doubleSum << endl;
  170. }
  171. //TODO void integersLessThan (Integer n)
  172. void integersLessThan(Integer n)
  173. {
  174. bool none = true;
  175. for (int i = 0; i < brElem; i++)
  176. {
  177. if (dynamic_cast<Integer*>(niza[i]))
  178. {
  179. if (niza[i]->intValue() < n.intValue())
  180. {
  181. none = false;
  182. cout << "Integer: " << niza[i]->intValue() << endl;
  183. }
  184. }
  185. }
  186. if (none) cout << "None" << endl;
  187. }
  188. //TODO void doublesBiggerThan (Double n)
  189. void doublesBiggerThan(Double n)
  190. {
  191. bool none = true;
  192. for (int i = 0; i < brElem; i++)
  193. {
  194. if (dynamic_cast<Double*>(niza[i]))
  195. {
  196. if (niza[i]->doubleValue() > n.doubleValue())
  197. {
  198. none = false;
  199. cout << "Double: " << niza[i]->doubleValue() << endl;
  200. }
  201. }
  202. }
  203. if (none) cout << "None" << endl;
  204. }
  205. };
  206.  
  207. int main() {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement