Advertisement
Alexander672

Class integer

Apr 25th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int ppow(int n, int a)
  9. {
  10. if (a == 0)
  11. return 1;
  12. if (a % 2 == 0)
  13. {
  14. int temp = ppow(n, a / 2);
  15. return temp * temp;
  16. }
  17. return n * ppow(n, a - 1);
  18. }
  19.  
  20. class integer
  21. {
  22. private:
  23. int num;
  24.  
  25. public:
  26. integer() {}
  27. integer(int num)
  28. {
  29. this->num = num;
  30. }
  31. int get_int()
  32. {
  33. return num;
  34. }
  35. void change_int(int a)
  36. {
  37. num = a;
  38. }
  39. integer operator+(integer a)
  40. {
  41. return integer(num + a.get_int());
  42. }
  43. integer operator-(integer a)
  44. {
  45. return integer(num - a.get_int());
  46. }
  47. integer operator*(integer a)
  48. {
  49. return integer(num * a.get_int());
  50. }
  51. integer operator^(integer a)
  52. {
  53. return integer(ppow(num, a.get_int()));
  54. }
  55. integer modul()
  56. {
  57. return integer(abs(num));
  58. }
  59. void print()
  60. {
  61. cout << num;
  62. }
  63. vector<integer> operator*(vector<integer> a)
  64. {
  65. int n = a.size();
  66. vector<integer> res(n);
  67. for (int i = 0; i < n; i++)
  68. res[i] = *this * a[i];
  69. return res;
  70. }
  71. string operator*(string a)
  72. {
  73. string res = "";
  74. for (int i = 0; i < num; i++)
  75. res += a;
  76. return res;
  77. }
  78. void smart_print()
  79. {
  80. int d = 2, temp = num;
  81. while (temp > 1)
  82. {
  83. if (temp % d == 0)
  84. {
  85. if (temp < num)
  86. cout << " * ";
  87. int k = 0;
  88. while (temp % d == 0)
  89. {
  90. temp /= d;
  91. k++;
  92. }
  93. integer(d).print();
  94. if (k > 1)
  95. {
  96. cout << "^";
  97. integer(k).print();
  98. }
  99. }
  100. d++;
  101. }
  102. }
  103. int get_len()
  104. {
  105. int temp = this->modul().get_int(), res = 1;
  106. while (temp / 10 > 0)
  107. {
  108. temp /= 10;
  109. res++;
  110. }
  111. return res;
  112. }
  113. };
  114.  
  115. class bin_num : public integer
  116. {
  117. public:
  118. bin_num() : integer() {}
  119. bin_num(int num) : integer(num) {}
  120. bin_num(string s) : integer()
  121. {
  122. int temp = 0, p = 1;
  123. for (int i = s.size() - 1; i >= 0; i--)
  124. {
  125. if (s[i] == '1')
  126. temp += p;
  127. p *= 2;
  128. }
  129. this->change_int(temp);
  130. }
  131. bin_num operator+(bin_num a)
  132. {
  133. return bin_num(this->get_int() + a.get_int());
  134. }
  135. bin_num operator-(bin_num a)
  136. {
  137. return bin_num(this->get_int() - a.get_int());
  138. }
  139. bin_num operator*(bin_num a)
  140. {
  141. return bin_num(this->get_int() * a.get_int());
  142. }
  143. bin_num operator^(bin_num a)
  144. {
  145. return bin_num(ppow(this->get_int(), a.get_int()));
  146. }
  147. vector<bin_num> operator*(vector<bin_num> a)
  148. {
  149. int n = a.size();
  150. vector<bin_num> res(n);
  151. for (int i = 0; i < n; i++)
  152. res[i] = *this * a[i];
  153. return res;
  154. }
  155. string operator*(string a)
  156. {
  157. string res = "";
  158. for (int i = 0; i < this->get_int(); i++)
  159. res += a;
  160. return res;
  161. }
  162. void print()
  163. {
  164. int temp = this->get_int();
  165. string s = "";
  166. while (temp > 0)
  167. {
  168. if (temp % 2)
  169. s += '1';
  170. else
  171. s += '0';
  172. temp /= 2;
  173. }
  174. reverse(s.begin(), s.end());
  175. cout << s;
  176. }
  177. void smart_print()
  178. {
  179. int d = 2, temp = this->get_int();
  180. while (temp > 1)
  181. {
  182. if (temp % d == 0)
  183. {
  184. if (temp < this->get_int())
  185. cout << " * ";
  186. int k = 0;
  187. while (temp % d == 0)
  188. {
  189. temp /= d;
  190. k++;
  191. }
  192. bin_num(d).print();
  193. if (k > 1)
  194. {
  195. cout << "^";
  196. bin_num(k).print();
  197. }
  198. }
  199. d++;
  200. }
  201. }
  202. int get_len()
  203. {
  204. int p = 2, res = 1;
  205. while (p < this->get_int())
  206. {
  207. p *= 2;
  208. res++;
  209. }
  210. return res;
  211. }
  212. };
  213.  
  214. class bool_num : public integer
  215. {
  216. public:
  217. bool_num(int num) : integer(num)
  218. {
  219. if (this->get_int() != 0)
  220. this->change_int(1);
  221. }
  222. bool_num operator+(bool_num a)
  223. {
  224. return bool_num(this->get_int() || a.get_int());
  225. }
  226. bool_num operator*(bool_num a)
  227. {
  228. return bool_num(this->get_int() && a.get_int());
  229. }
  230. bool_num operator^(bool_num a)
  231. {
  232. return bool_num(!(this->get_int() || a.get_int()));
  233. }
  234. bool_num operator-(bool_num a)
  235. {
  236. return bool_num(this->get_int() ^ a.get_int());
  237. }
  238. void print()
  239. {
  240. if (this->get_int())
  241. cout << "true";
  242. else
  243. cout << "false";
  244. }
  245. void smart_print()
  246. {
  247. cout << "you can't use this method to this class";
  248. }
  249. };
  250.  
  251. int main()
  252. {
  253. cout << "Integer test:\n";
  254. integer a(2), b(10);
  255. (a + b).print();
  256. cout << "\n";
  257. (b - a).print();
  258. cout << "\n";
  259. (a * b).print();
  260. cout << "\n";
  261. (a ^ b).print();
  262. cout << "\n";
  263. cout << a * "ab";
  264. cout << "\n";
  265. vector<integer> temp(2, 3);
  266. temp[1] = 6;
  267. temp = b * temp;
  268. for (auto elem : temp)
  269. {
  270. elem.print();
  271. cout << " ";
  272. }
  273. cout << "\n";
  274. (a + b).smart_print();
  275. cout << "\n";
  276. cout << "Bin_num test:\n";
  277. bin_num c("101"), d(7);
  278. (c + d).print();
  279. cout << "\n";
  280. (c + d).smart_print();
  281. cout << "\n";
  282. cout << "Bool_num test:\n";
  283. bool_num e(9), f(0);
  284. e.print();
  285. cout << "\n";
  286. f.print();
  287. cout << "\n";
  288. return 0;
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement