Advertisement
Alex-Flexer

Untitled

Apr 30th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. class Int {
  8. bool sign = true;
  9. string value_string;
  10.  
  11. void align_number_length(int lenght) {
  12. while (value_string.size() < lenght) {
  13. value_string = "0" + value_string;
  14. }
  15. }
  16.  
  17. void strip_number() {
  18. while (value_string[0] == '0' && value_string.size() > 1)
  19. {
  20. value_string.erase(value_string.begin());
  21. }
  22. }
  23.  
  24. static int sum_char_digits(char object_digit, char other_digit) {
  25. return (object_digit - 48) + (other_digit - 48);
  26. }
  27.  
  28. static int dif_char_digits(char object_digit, char other_digit) {
  29. return (object_digit - 48) - (other_digit - 48);
  30. }
  31.  
  32. char& operator[](int index) {
  33. return value_string[index];
  34. }
  35.  
  36. public:
  37.  
  38. Int(int value) {
  39. string str_value = to_string(value);
  40.  
  41. if (str_value[0] == '-') {
  42. sign = false;
  43. str_value.erase(str_value.begin());
  44. }
  45.  
  46. value_string = str_value;
  47. }
  48.  
  49. Int(string value) {
  50. if (value[0] == '-') {
  51. sign = false;
  52. value.erase(value.begin());
  53. }
  54.  
  55. value_string = value;
  56. }
  57.  
  58. Int(string value, int n) {
  59. Int res;
  60. int len = value.size();
  61. int k = 1;
  62. for (int i = len - 1; i >= 0; --i)
  63. {
  64. char char_digit = tolower(value[i]);
  65. int int_digit;
  66.  
  67. if (char_digit >= '0' && char_digit <= '9') {
  68. int_digit = char_digit - '0';
  69. if (int_digit >= n) {
  70. throw invalid_argument("Unknown digit");
  71. }
  72. }
  73. else if ('a' <= char_digit && char_digit <= 'z') {
  74. int_digit = char_digit - 'a';
  75. }
  76. else {
  77. throw invalid_argument("Unknown digit");
  78. }
  79.  
  80. Int digit((int_digit) * k);
  81. res += digit;
  82. k *= n;
  83. }
  84.  
  85. value_string = res.value_string;
  86. sign = res.sign;
  87. }
  88.  
  89. Int() {
  90. value_string = "0";
  91. }
  92.  
  93. size_t size() {
  94. return value_string.size();
  95. }
  96.  
  97. friend ostream& operator<<(ostream& out, Int& object){
  98. if (!object.sign)
  99. out << '-';
  100.  
  101. out << object.value_string;
  102. return out;
  103. }
  104.  
  105. friend istream& operator>>(istream& in, Int& object) {
  106. string temp_val;
  107. in >> temp_val;
  108. if (temp_val[0] == '-') {
  109. object.sign = false;
  110. temp_val.erase(temp_val.begin());
  111. }
  112. object.value_string = temp_val;
  113. return in;
  114. }
  115.  
  116. Int operator-() const{
  117. Int res;
  118. res.value_string = this->value_string;
  119. res.sign = !res.sign;
  120.  
  121. return res;
  122. }
  123.  
  124. friend Int operator++(Int& object) {
  125. object += 1;
  126. return object;
  127. }
  128.  
  129. friend bool operator>(Int& object, int other_int) {
  130. Int other(other_int);
  131. return object > other;
  132. }
  133.  
  134. friend bool operator<(Int& object, int other_int) {
  135. Int other(other_int);
  136. return object < other;
  137. }
  138.  
  139. friend bool operator==(Int& object, int other_int) {
  140. return object.value_string == to_string(other_int);
  141. }
  142.  
  143. friend bool operator>=(Int& object, int other_int) {
  144. return object == other_int || object > other_int;
  145. }
  146.  
  147. friend bool operator<=(Int& object, int other_int) {
  148. return object == other_int || object < other_int;
  149. }
  150.  
  151. // ВЫЧИТАНИЕ
  152.  
  153. friend Int operator-(Int& object, int other_int) {
  154. Int other(other_int);
  155. return object - other;
  156. }
  157.  
  158. friend Int operator-=(Int& object, int other_int) {
  159. Int other(other_int);
  160. return object -= other;
  161. }
  162.  
  163. // СЛАЖЕНИЕ
  164.  
  165. friend Int operator+(Int& object, int other_int) {
  166. Int other(other_int);
  167. return object + other;
  168. }
  169.  
  170. friend Int operator+=(Int& object, int other_int) {
  171. Int other(other_int);
  172. return object += other;
  173. }
  174.  
  175. // УМНОЖЕНИЕ
  176.  
  177. friend Int operator*(Int& object, int other_int) {
  178. Int other(other_int);
  179. return object * other;
  180. }
  181.  
  182. friend Int operator*=(Int& object, int other_int) {
  183. Int other(other_int);
  184. object *= other;
  185. return object;
  186. }
  187.  
  188. // ДЕЛЕНИЯ
  189.  
  190. friend Int operator/(Int& object, int other_int) {
  191. Int other(other_int);
  192. return object / other;
  193. }
  194.  
  195. friend Int operator/=(Int& object, int other_int) {
  196. Int other(other_int);
  197. return object = object / other;
  198. }
  199.  
  200. ///////////////////////////////////////////////////////////////////
  201. // int / Int
  202. ///////////////////////////////////////////////////////////////////
  203.  
  204. friend bool operator>(Int& object, Int& other) {
  205. if (object.sign != other.sign)
  206. return object.sign;
  207.  
  208. if (object.size() != other.size())
  209. return object.size() > other.size();
  210.  
  211. for (int i = 0; i < object.size(); i++)
  212. {
  213. if (object[i] != other[i])
  214. return object[i] > other[i];
  215. }
  216.  
  217. return false;
  218. }
  219.  
  220. friend bool operator<(Int& object, Int& other) {
  221. if (object.sign != other.sign)
  222. return !object.sign;
  223.  
  224. if (object.size() != other.size())
  225. return object.size() < other.size();
  226.  
  227. for (int i = 0; i < object.size(); i++)
  228. {
  229. if (object[i] != other[i])
  230. return object[i] < other[i];
  231. }
  232.  
  233. return false;
  234. }
  235.  
  236. friend bool operator==(Int& object, Int& other) {
  237. return object.value_string == other.value_string;
  238. }
  239.  
  240. friend bool operator>=(Int& object, Int& other) {
  241. return object == other || object > other;
  242. }
  243.  
  244. friend bool operator<=(Int& object, Int& other) {
  245. return object == other || object < other;
  246. }
  247.  
  248. // ВЫЧИТАНИЕ
  249.  
  250. friend Int operator-(Int& object, Int& other) {
  251. if (other.sign == false) {
  252. other.sign = true;
  253. Int sum = object + other;
  254.  
  255. other.sign = false;
  256.  
  257. return sum;
  258. }
  259.  
  260. if (object.sign == false) {
  261. object.sign = true;
  262. Int sum = other + object;
  263. sum.sign = false;
  264.  
  265. object.sign = false;
  266.  
  267. return sum;
  268. }
  269.  
  270. bool is_swaped = false;
  271. if (object < other) {
  272. swap(object, other);
  273. is_swaped = true;
  274. }
  275.  
  276. int object_length = object.value_string.size();
  277. int other_length = other.value_string.size();
  278. int final_length = object_length;
  279.  
  280. if (object_length > other_length) {
  281. other.align_number_length(object_length);
  282. final_length = object_length;
  283. }
  284. else if (object_length < other_length)
  285. {
  286. object.align_number_length(other_length);
  287. final_length = other_length;
  288. }
  289.  
  290. Int dif;
  291. dif.align_number_length(final_length);
  292.  
  293. for (int i = 0; i < final_length; ++i)
  294. {
  295. int digits_dif = Int::dif_char_digits(object[i], other[i]);
  296. if (digits_dif < 0)
  297. {
  298. digits_dif += 10;
  299. dif[i] = '0' + digits_dif;
  300. for (int j = i - 1; ; --j)
  301. {
  302. if (dif[j] == '0')
  303. dif[j] = '9';
  304. else {
  305. dif[j]--;
  306. break;
  307. }
  308. }
  309. }
  310. dif[i] = char(digits_dif + 48);
  311. }
  312.  
  313. object.strip_number();
  314. other.strip_number();
  315. dif.strip_number();
  316.  
  317. if (is_swaped) {
  318. swap(object, other);
  319. return -dif;
  320. }
  321.  
  322. return dif;
  323. }
  324.  
  325. friend Int operator-=(Int& object, Int& other) {
  326. return object = object - other;;
  327. }
  328.  
  329. // СЛАЖЕНИЕ
  330.  
  331. friend Int operator+(Int& object, Int& other) {
  332. if (object.sign == true && other.sign == false) {
  333. other.sign = true;
  334. Int sum = object - other;
  335.  
  336. other.sign = false;
  337.  
  338. return sum;
  339. }
  340.  
  341. if (object.sign == false && other.sign == true) {
  342. object.sign = true;
  343. Int sum = other - object;
  344.  
  345. object.sign = false;
  346.  
  347. return sum;
  348. }
  349.  
  350. bool sign = object.sign;
  351.  
  352. int object_length = object.value_string.size();
  353. int other_length = other.value_string.size();
  354. int final_length = object_length;
  355.  
  356. if (object_length > other_length) {
  357. other.align_number_length(object_length);
  358. final_length = object_length;
  359. }
  360. else if (object_length < other_length)
  361. {
  362. object.align_number_length(other_length);
  363. final_length = other_length;
  364. }
  365.  
  366. Int sum;
  367. sum.align_number_length(final_length);
  368. sum.sign = sign;
  369.  
  370. int additional_sum = 0;
  371. for (int i = final_length - 1; i >= 0; --i)
  372. {
  373. int digits_sum = Int::sum_char_digits(object[i], other[i]) + additional_sum;
  374. sum[i] = char(digits_sum % 10 + 48);
  375. additional_sum = digits_sum / 10;
  376. }
  377. if (additional_sum > 0) {
  378. sum.align_number_length(final_length + 1);
  379. sum[0] = additional_sum + 48;
  380. }
  381. object.strip_number();
  382. other.strip_number();
  383. sum.strip_number();
  384. return sum;
  385. }
  386.  
  387. friend Int operator+=(Int& object, Int& other) {
  388. return object = object + other;;
  389. }
  390.  
  391. // УМНОЖЕНИЕ
  392.  
  393. friend Int operator*(Int& object, Int& other) {
  394. bool sign = object.sign ^ other.sign;
  395.  
  396. Int res("0");
  397.  
  398. for (Int i(0); i < other; i += 1)
  399. {
  400. res;
  401. }
  402.  
  403. return res;
  404. }
  405.  
  406. friend Int operator*=(Int& object, Int& other) {
  407. return object = object * other;
  408. }
  409.  
  410. // ДЕЛЕНИЯ
  411.  
  412. friend Int operator/(Int& object, Int& other) {
  413. Int res(0);
  414. if (object < other)
  415. return res;
  416.  
  417. do
  418. {
  419. object -= other;
  420. res += 1;
  421. } while (object >= 0);
  422. res -= 1;
  423.  
  424. return res;
  425. }
  426.  
  427. friend Int operator/=(Int& object, Int& other) {
  428. return object = object / other;
  429. }
  430. };
  431.  
  432. int
  433. maina()
  434. {
  435. Int a(1);
  436. cout << a;
  437. return 0;
  438. }
  439.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement