blufzzz

Fivt_4/task-A/12.12.2017 (Aborted(core dumped)

Dec 11th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.47 KB | None | 0 0
  1. #ifndef FIVT_4_BIGINTEGER_H
  2. #define FIVT_4_BIGINTEGER_H
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7.  
  8. typedef int data;
  9.  
  10. const data BASE = 10;
  11.  
  12. class BigInteger {
  13. public:
  14. BigInteger(int integer_value);
  15. BigInteger();
  16. BigInteger(const BigInteger& other); // copy-constructor
  17. BigInteger(BigInteger&& other) noexcept; // move-constructor
  18. BigInteger(const std::vector<data>& _numbers, bool _is_positive);
  19.  
  20. BigInteger& operator = (const BigInteger& other);
  21. BigInteger& operator = (BigInteger&& other) noexcept;
  22.  
  23.  
  24. friend BigInteger operator + (const BigInteger& that, const BigInteger& other);
  25. friend BigInteger operator - (const BigInteger& that, const BigInteger& other);
  26. friend BigInteger operator * (const BigInteger& that, const BigInteger& other);
  27. friend BigInteger operator / (const BigInteger& that, const BigInteger& other);
  28. friend BigInteger operator % (const BigInteger& that, const BigInteger& other);
  29.  
  30.  
  31. friend BigInteger& operator += (BigInteger& that, const BigInteger& other);
  32. friend int& operator += (int& that, const BigInteger& other);
  33.  
  34. friend BigInteger& operator -= (BigInteger& that, const BigInteger& other);
  35. friend int& operator -= (int& that, const BigInteger& other);
  36.  
  37. friend BigInteger& operator *= (BigInteger& that, const BigInteger& other);
  38. friend int& operator *= (int& that, const BigInteger& other);
  39.  
  40. friend BigInteger& operator /= (BigInteger& that, const BigInteger& other);
  41. friend int& operator /= (int& that, const BigInteger& other);
  42.  
  43. friend BigInteger& operator %= (BigInteger& that, const BigInteger& other);
  44. friend int& operator %= (int& that, const BigInteger& other);
  45.  
  46. BigInteger operator-() const;
  47. BigInteger& operator++(); // prefix-form
  48. BigInteger operator++(int); // postfix-form
  49. BigInteger& operator--(); // prefix-form
  50. BigInteger operator--(int); // postfix-form
  51.  
  52. bool operator==(const BigInteger& other) const;
  53. bool operator!=(const BigInteger& other) const;
  54. bool operator<=(const BigInteger& other) const;
  55. bool operator>=(const BigInteger& other) const;
  56. bool operator<(const BigInteger& other) const;
  57. bool operator>(const BigInteger& other) const;
  58.  
  59. // input - output
  60. friend std::ostream &operator<<(std::ostream& stream, const BigInteger& big_integer);
  61. friend std::istream &operator>>(std::istream& stream, BigInteger& big_integer);
  62.  
  63. // type conversion
  64. explicit operator int();
  65. explicit operator bool();
  66.  
  67.  
  68. // other methods
  69. std::string toString() const;
  70. bool GetIsPositive() const;
  71. size_t GetNumberOfDigits() const;
  72. std::vector<data> GetVectorOfDigits() const;
  73. void SetIsPositive(bool sign);
  74. void MultiplyOnBase();
  75. void DeleteLeadZeros();
  76.  
  77.  
  78. private:
  79. std::vector<data> vector_of_digits;
  80. bool is_positive;
  81. bool CompareByAbs(const BigInteger& other) const;
  82. };
  83.  
  84.  
  85.  
  86.  
  87. BigInteger::BigInteger(const std::vector<data>& _numbers, bool _is_positive) {
  88. vector_of_digits = _numbers;
  89. is_positive = _is_positive;
  90. }
  91.  
  92.  
  93.  
  94. BigInteger::BigInteger(int integer_value) {
  95.  
  96. std::vector<data> digits;
  97. is_positive = (integer_value >= 0);
  98. integer_value = ( integer_value < 0 )? -integer_value : integer_value;
  99.  
  100. if ( integer_value == 0 ) {
  101. digits.push_back(0);
  102. } else {
  103. unsigned long long current_rest = 0;
  104. unsigned long long prev_rest = 0;
  105. unsigned long long charge = BASE;
  106. size_t iter = 0;
  107. while (true) {
  108. prev_rest = current_rest;
  109. current_rest = integer_value % charge;
  110.  
  111. try {
  112. digits.push_back(static_cast<data>((current_rest - prev_rest) / (charge / BASE)));
  113. if (current_rest == static_cast<unsigned int>(integer_value)) {
  114. break;
  115. }
  116. } catch (...) {
  117. std::cerr<<"bad_cast in: BigInteger(int integer_value) ";
  118. exit(2);
  119. }
  120.  
  121. charge *= BASE;
  122. iter++;
  123. if (iter > 20) {
  124. std::cerr<<"loop in: BigInteger(int integer_value) ";
  125. exit(2);
  126. }
  127. }
  128. }
  129. vector_of_digits = digits;
  130. }
  131.  
  132.  
  133.  
  134.  
  135. BigInteger::BigInteger() {
  136. std::vector<data> digits;
  137. vector_of_digits = digits;
  138. is_positive = true;
  139. }
  140.  
  141.  
  142.  
  143. BigInteger::BigInteger(const BigInteger &other) {
  144. vector_of_digits = other.GetVectorOfDigits();
  145. is_positive = other.GetIsPositive();
  146. }
  147.  
  148.  
  149.  
  150. BigInteger::BigInteger(BigInteger &&other) noexcept {
  151. vector_of_digits = other.vector_of_digits;
  152. is_positive = other.is_positive;
  153. other.vector_of_digits.clear();
  154. other.is_positive = true;
  155. }
  156.  
  157.  
  158.  
  159. std::vector<data> BigInteger::GetVectorOfDigits() const {
  160. return vector_of_digits;
  161. }
  162.  
  163.  
  164.  
  165. bool BigInteger::GetIsPositive() const {
  166. return is_positive;
  167. }
  168.  
  169.  
  170. BigInteger& BigInteger::operator=(const BigInteger &other) {
  171. this->vector_of_digits = other.GetVectorOfDigits();
  172. this->is_positive = other.GetIsPositive();
  173. return *this;
  174. }
  175.  
  176.  
  177. BigInteger& BigInteger::operator=(BigInteger &&other) noexcept {
  178. this->vector_of_digits = other.vector_of_digits;
  179. this->is_positive = other.is_positive;
  180. other.vector_of_digits.clear();
  181. other.is_positive = true;
  182. return *this;
  183. }
  184.  
  185.  
  186. BigInteger operator + (const BigInteger& that, const BigInteger& other) {
  187.  
  188. // this is positive, other is negative
  189. if (that.GetIsPositive() && !other.GetIsPositive()) { // (+a) + (-b)
  190. BigInteger other_positive = other; // MEMORY!
  191. other_positive.SetIsPositive(true);
  192. return (that - other_positive);
  193.  
  194. // this is negative, other is positive
  195. } else if (!that.GetIsPositive() && other.GetIsPositive()) { // (-a) + (+b)
  196. BigInteger this_positive = that; // MEMORY!
  197. this_positive.SetIsPositive(true);
  198. return (other - this_positive);
  199.  
  200. // this & other are negative
  201. } else if (!that.GetIsPositive() && !other.GetIsPositive()) {
  202. BigInteger this_positive = that; // MEMORY!
  203. this_positive.SetIsPositive(true);
  204. BigInteger other_positive = other; // MEMORY!
  205. other_positive.SetIsPositive(true);
  206. return -(this_positive + other_positive);
  207.  
  208. // both operands are positive
  209. } else {
  210.  
  211. auto other_vector_of_numbers = other.GetVectorOfDigits();
  212. std::vector<data> result;
  213. size_t n = that.vector_of_digits.size();
  214. size_t m = other_vector_of_numbers.size();
  215.  
  216. data to_next_charge = 0;
  217. for (size_t i = 0; i < n || i < m; ++i) {
  218. data rest = 0;
  219. data number = 0;
  220.  
  221. try {
  222. number = static_cast<data>(((i < m) ? other_vector_of_numbers[i] : 0) +
  223. ((i < n) ? that.vector_of_digits[i] : 0) + to_next_charge);
  224. rest = static_cast<data>(number % BASE);
  225. } catch (...) {
  226. std::cerr<<"bad_cast in operator+";
  227. exit(3);
  228. }
  229.  
  230. if ( number >= BASE) {
  231. to_next_charge = 1;
  232. number = rest;
  233. } else {
  234. to_next_charge = 0;
  235. }
  236.  
  237. result.push_back(number);
  238. }
  239.  
  240. if (to_next_charge != 0) {
  241. result.push_back(to_next_charge);
  242. }
  243.  
  244. return {result, true};
  245. }
  246. }
  247.  
  248.  
  249.  
  250. BigInteger operator-(const BigInteger& that, const BigInteger& other) {
  251.  
  252. // this is positive, other is negative
  253. if (that.GetIsPositive() && !other.GetIsPositive()) {
  254. BigInteger other_positive = other; // (+a) - (-b)
  255. other_positive.SetIsPositive(true);
  256. return (that + other_positive);
  257.  
  258. // this is negative, other is positive
  259. } else if (!that.GetIsPositive() && other.GetIsPositive()) { // (-a) - (+b)
  260. BigInteger this_positive = that;
  261. this_positive.SetIsPositive(true);
  262. BigInteger other_positive = other;
  263. other_positive.SetIsPositive(true);
  264. return -(this_positive + other_positive); // -((+a) + (+b))
  265.  
  266. // this & other are negative
  267. } else if (!that.GetIsPositive() && !other.GetIsPositive()) { //(-a) - (-b)
  268. BigInteger this_positive = that;
  269. this_positive.SetIsPositive(true);
  270. BigInteger other_positive = other;
  271. other_positive.SetIsPositive(true);
  272. return (other_positive - this_positive);
  273.  
  274. // both operands are positive
  275. } else { //(+a) - (+b)
  276. std::vector<data> result;
  277. // из большего - меньшее
  278. if ( that > other ) {
  279.  
  280. auto other_vector_of_numbers = other.GetVectorOfDigits();
  281.  
  282. size_t n = that.vector_of_digits.size();
  283. size_t m = other_vector_of_numbers.size();
  284.  
  285. data to_next_charge = 0;
  286. int number = 0;
  287.  
  288. if ( n < m ) {
  289. std::cerr<<" n < m in operator-";
  290. exit(1);
  291. }
  292.  
  293. for (size_t i = 0; i < n || i < m; ++i) {
  294. number = (that.vector_of_digits[i] - ((i < m) ? other_vector_of_numbers[i] : 0) - to_next_charge);
  295. if (number < 0) {
  296. result.emplace_back(static_cast<data>(number + BASE));
  297. to_next_charge = 1;
  298. } else {
  299. result.push_back(static_cast<data>(number));
  300. to_next_charge = 0;
  301. }
  302. }
  303.  
  304. // убираем нули в конце
  305. while (result.back() == 0 && result.size() > 1) {
  306. result.pop_back();
  307. }
  308.  
  309. return {result, true};
  310.  
  311. // сводим к вычитанию из большего меньшее
  312. } else if (that == other) {
  313. result.emplace_back(0);
  314. return {result, true};
  315. } else {
  316. return -(other - that);
  317. }
  318. }
  319. }
  320.  
  321.  
  322.  
  323.  
  324. BigInteger operator*(const BigInteger& that, const BigInteger& other) {
  325.  
  326. size_t n = that.GetNumberOfDigits();
  327. size_t m = other.GetNumberOfDigits();
  328.  
  329. // умножение длинного на короткое
  330. if ( n >= m ) {
  331.  
  332. std::vector<data> this_vector = that.GetVectorOfDigits();
  333. std::vector<data> other_vector = other.GetVectorOfDigits();
  334.  
  335. bool result_sign = that.GetIsPositive() & other.GetIsPositive();
  336.  
  337. std::vector<BigInteger> parts;
  338. for (size_t j = 0; j < m; ++j) { // идем по короткому
  339. data to_next_charge = 0;
  340. std::vector<data> new_part(n + j);
  341. for (size_t i = 0; i < n; ++i) { // по длинному
  342.  
  343. data value = (other_vector[j] * this_vector[i]) + to_next_charge;
  344. data rest = value % BASE;
  345.  
  346. if ( rest < value ) { //число больше 10
  347. to_next_charge = (value - rest)/BASE;
  348. } else {
  349. to_next_charge = 0;
  350. }
  351. new_part[i + j] = rest;
  352.  
  353. }
  354.  
  355. if ( to_next_charge > 0 ) {
  356. new_part.push_back(to_next_charge);
  357. }
  358.  
  359. parts.push_back({new_part,true});
  360. }
  361.  
  362. BigInteger result = parts[0];
  363. for(size_t i = 1; i < parts.size(); ++i) {
  364. result += parts[i];
  365. }
  366.  
  367. result.DeleteLeadZeros();
  368. result.SetIsPositive(result_sign);
  369.  
  370. return result;
  371.  
  372. } else {
  373. return (other * that);
  374. }
  375. }
  376.  
  377.  
  378. void BigInteger::MultiplyOnBase() {
  379. vector_of_digits.insert(vector_of_digits.begin(),0);
  380. }
  381.  
  382.  
  383.  
  384. void BigInteger::DeleteLeadZeros() {
  385. while (vector_of_digits.back() == 0 && vector_of_digits.size() > 1) {
  386. vector_of_digits.pop_back();
  387. }
  388. }
  389.  
  390.  
  391. BigInteger operator / (const BigInteger& that, const BigInteger& other) {
  392.  
  393. bool result_sign = that.GetIsPositive() && other.GetIsPositive();
  394. BigInteger other_positive = {other.GetVectorOfDigits(), true};
  395. BigInteger this_positive = {that.GetVectorOfDigits(), true};
  396.  
  397. std::vector<data> result_vector;
  398.  
  399. if (other_positive > this_positive) {
  400. result_vector.emplace_back(0);
  401. return {result_vector, true};
  402.  
  403. } else if (other_positive == this_positive) {
  404. result_vector.emplace_back(1);
  405. return {result_vector, result_sign};
  406.  
  407. } else { //this > other // what if this == {0}
  408.  
  409. size_t n = that.GetNumberOfDigits();
  410. result_vector.resize(n); // число разрядов в частном не больше чем в делимом
  411. std::vector<data> this_vector = that.GetVectorOfDigits();
  412. BigInteger current_value;
  413.  
  414. for (int i = n - 1; i >= 0; --i) {
  415.  
  416. current_value.MultiplyOnBase();
  417. current_value.vector_of_digits[0] = this_vector[i]; //?? private
  418.  
  419. // подбираем максимальное число x, такое что other * attempt <= curent_value
  420. for (data l = 0; l <= BASE; ++l) {
  421.  
  422. BigInteger attempt({l}, true);
  423. BigInteger cur = other_positive * attempt;
  424.  
  425. if (cur > current_value) {
  426.  
  427. try {
  428.  
  429. result_vector[i] = static_cast<data>(l - 1);
  430. current_value = current_value - other_positive * BigInteger({static_cast<data>(l - 1)}, true);
  431.  
  432. } catch (...) {
  433. std::cerr<<"bad_cast in: operator/ ";
  434. exit(5);
  435. }
  436.  
  437. current_value.DeleteLeadZeros();
  438. break;
  439. }
  440. }
  441. }
  442.  
  443. BigInteger result {result_vector, result_sign};
  444. result.DeleteLeadZeros();
  445.  
  446. return result;
  447. }
  448. }
  449.  
  450.  
  451.  
  452. BigInteger operator % (const BigInteger& that, const BigInteger& other) {
  453.  
  454. bool result_sign = that.GetIsPositive() & other.GetIsPositive();
  455. BigInteger other_positive = {other.GetVectorOfDigits(), true};
  456. BigInteger this_positive = {that.GetVectorOfDigits(), true};
  457.  
  458.  
  459. std::vector<data> result_vector;
  460.  
  461. if (other_positive > this_positive) {
  462. return {that.GetVectorOfDigits(), result_sign};
  463.  
  464. } else if (other_positive == this_positive) {
  465. result_vector.push_back(0);
  466. return {result_vector, result_sign};
  467.  
  468. } else {
  469.  
  470. size_t n = that.GetNumberOfDigits();
  471. std::vector<data> this_vector = that.GetVectorOfDigits();
  472. BigInteger current_value;
  473.  
  474. for (int i = n - 1; i >= 0; --i) {
  475.  
  476. current_value.MultiplyOnBase();
  477. current_value.vector_of_digits[0] = this_vector[i];
  478.  
  479. // подбираем максимальное число x, такое что b * attempt <= curValue
  480. for (data l = 0; l <= BASE; ++l) {
  481.  
  482. BigInteger cur = other_positive * l;
  483.  
  484. if (cur > current_value) {
  485. current_value -= other_positive * (l-1);
  486. current_value.DeleteLeadZeros();
  487. break;
  488. }
  489. }
  490. }
  491. current_value.SetIsPositive( result_sign );
  492. return current_value;
  493. }
  494. }
  495.  
  496.  
  497. BigInteger& operator += (BigInteger& that, const BigInteger& other) {
  498. that = that + other;
  499. return (that);
  500. }
  501.  
  502. int& operator+=(int& that, const BigInteger &other) {
  503. that = (int)(other + that);
  504. return that;
  505. }
  506.  
  507.  
  508.  
  509. BigInteger& operator-=(BigInteger& that, const BigInteger& other) {
  510. that = (that - other);
  511. return (that);
  512. }
  513.  
  514. int& operator-=(int& that, const BigInteger &other) {
  515. that = (int)(that - other);
  516. return (that);
  517. }
  518.  
  519.  
  520.  
  521.  
  522. BigInteger& operator*=(BigInteger& that, const BigInteger& other) {
  523. that = (that * other);
  524. return (that);
  525. }
  526.  
  527. int& operator*=(int& that, const BigInteger& other) {
  528. that = (int)(that * other);
  529. return (that);
  530. }
  531.  
  532.  
  533.  
  534. BigInteger& operator/=(BigInteger& that, const BigInteger& other) {
  535. that = (that / other);
  536. return (that);
  537. }
  538.  
  539. int& operator/=(int& that, const BigInteger& other) {
  540. that = (int)(that / other);
  541. return (that);
  542. }
  543.  
  544.  
  545.  
  546. BigInteger& operator%=(BigInteger& that, const BigInteger& other) {
  547. that = (that % other);
  548. return (that);
  549. }
  550.  
  551. int& operator%=(int& that, const BigInteger& other) {
  552. that = (int)(that % other);
  553. return (that);
  554. }
  555.  
  556.  
  557. BigInteger BigInteger::operator-() const {
  558. try {
  559. BigInteger temp = {this->GetVectorOfDigits(), !this->GetIsPositive()};
  560. return temp;
  561. } catch (...) {
  562. std::cerr<<"operator-";
  563. exit(1);
  564. }
  565. }
  566.  
  567.  
  568.  
  569. BigInteger& BigInteger::operator++() {
  570. (*this) = (*this) + BigInteger(1);
  571. return *this;
  572. }
  573.  
  574.  
  575.  
  576. BigInteger BigInteger::operator++(int) {
  577. BigInteger temp = *this;
  578. (*this) = (*this) + BigInteger(1);
  579. return temp;
  580. }
  581.  
  582.  
  583. BigInteger& BigInteger::operator--() {
  584. (*this) = (*this) - BigInteger(1);
  585. return *this;
  586. }
  587.  
  588.  
  589. BigInteger BigInteger::operator--(int) {
  590. BigInteger temp = *this;
  591. (*this) = (*this) - BigInteger(1);
  592. return temp;
  593. }
  594.  
  595.  
  596. bool BigInteger::operator==(const BigInteger &other) const {
  597. size_t n = this->GetNumberOfDigits();
  598. if ( n != other.GetNumberOfDigits() ) {
  599. return false;
  600. } else {
  601. if ( this->GetIsPositive() != other.GetIsPositive() ) {
  602. return false;
  603. } else {
  604. auto this_vector = this->GetVectorOfDigits();
  605. auto other_vector = other.GetVectorOfDigits();
  606. for (size_t i = 0; i < n; ++i) {
  607. if (this_vector[i] != other_vector[i]) {
  608. return false;
  609. }
  610. }
  611. return true;
  612. }
  613. }
  614. }
  615.  
  616.  
  617.  
  618. size_t BigInteger::GetNumberOfDigits() const {
  619. return vector_of_digits.size();
  620. }
  621.  
  622. bool BigInteger::operator!=(const BigInteger &other) const {
  623. return !( *this == other );
  624. }
  625.  
  626. bool BigInteger::operator<=(const BigInteger &other) const {
  627. bool is_equal = ( *this == other );
  628. bool is_less = (*this < other);
  629. return is_equal || is_less;
  630. }
  631.  
  632.  
  633.  
  634. bool BigInteger::operator>=(const BigInteger &other) const {
  635. bool is_equal = ( *this == other );
  636. bool is_more = (*this > other);
  637. return is_equal || is_more;
  638. }
  639.  
  640.  
  641.  
  642. bool BigInteger::CompareByAbs(const BigInteger &other) const { // is this < other by abs?
  643. size_t n = this->GetNumberOfDigits();
  644. size_t m = other.GetNumberOfDigits();
  645.  
  646. if ( n < m ) {
  647. return true;
  648. } else if ( m == n ) {
  649.  
  650. auto this_vector = this->GetVectorOfDigits();
  651. auto other_vector = other.GetVectorOfDigits();
  652. for (int i = n - 1; i >= 0; --i) { // start from the end
  653. if (this_vector[i] < other_vector[i]) {
  654. return true;
  655. } else if (this_vector[i] > other_vector[i]) {
  656. return false;
  657. }
  658. }
  659. return false;
  660. } else {
  661. return false;
  662. }
  663. }
  664.  
  665.  
  666.  
  667. bool BigInteger::operator<(const BigInteger &other) const {
  668. bool is_less = false;
  669. // this - negative , other - positive
  670. if ( !this->GetIsPositive() && other.GetIsPositive() ) {
  671. is_less = true;
  672.  
  673. // this - positive , other - negative
  674. } else if ( this->GetIsPositive() && !other.GetIsPositive() ) {
  675. is_less = false;
  676.  
  677. // this & other have similar sign
  678. } else {
  679. // this & other negative
  680. if ( !this->GetIsPositive() && !other.GetIsPositive() ) {
  681. is_less = !(this->CompareByAbs(other));
  682. }
  683. // this & other positive
  684. if ( this->GetIsPositive() && other.GetIsPositive() ){
  685. is_less = this->CompareByAbs(other);
  686. }
  687. }
  688. return is_less && (*this != other);
  689. }
  690.  
  691.  
  692.  
  693. bool BigInteger::operator>(const BigInteger &other) const {
  694. return (!(*this < other)) && (*this != other);
  695. }
  696.  
  697.  
  698. std::ostream& operator<<(std::ostream &stream, const BigInteger& big_integer) {
  699.  
  700. std::vector<data> vector_of_digits = big_integer.GetVectorOfDigits();
  701. if ( !big_integer.is_positive ) {
  702. stream << '-';
  703. }
  704. for (int i = big_integer.GetNumberOfDigits() - 1; i >= 0; --i) {
  705. stream << vector_of_digits[i];
  706. }
  707. return stream;
  708. }
  709.  
  710.  
  711. std::istream& operator>>(std::istream &stream, BigInteger& big_integer) {
  712.  
  713. std::string str;
  714.  
  715. stream.setf(std::ios::skipws);
  716.  
  717. stream >> str;
  718.  
  719. int first_position_of_number = 0;
  720. if (str[0] == '-') {
  721. big_integer.is_positive = false;
  722. first_position_of_number = 1;
  723. if ( str.size() == 1 ) {
  724. std::string rest_of_str;
  725. stream >> rest_of_str;
  726. str.append(rest_of_str);
  727. }
  728. } else {
  729. big_integer.is_positive = true;
  730. }
  731.  
  732. for (int i = str.size() - 1; i >= first_position_of_number; --i) {
  733. std::string digit = {str[i]};
  734.  
  735. try {
  736. big_integer.vector_of_digits.emplace_back(static_cast<data&&>(std::stoi(digit, nullptr, BASE)));
  737. } catch (...){
  738. std::cerr<<"bad_cast in: operator>> ";
  739. exit(7);
  740. }
  741.  
  742. }
  743. return stream;
  744. }
  745.  
  746.  
  747.  
  748. BigInteger::operator int() {
  749.  
  750. int int_value = 0;
  751.  
  752. unsigned long long charge = 1;
  753. for (auto digit : this->GetVectorOfDigits()) {
  754. int_value += digit*charge;
  755. charge *= BASE;
  756. }
  757.  
  758. return int_value;
  759. }
  760.  
  761.  
  762.  
  763. BigInteger::operator bool() {
  764. for (auto digit : this->GetVectorOfDigits()) {
  765. if ( digit != 0 ) {
  766. return true;
  767. }
  768. }
  769. return false;
  770. }
  771.  
  772.  
  773. void BigInteger::SetIsPositive(bool sign) {
  774. this->is_positive = sign;
  775. }
  776.  
  777.  
  778. std::string BigInteger::toString() const {
  779. // std::string string_number = "";
  780. // if ( !this->GetIsPositive() ) {
  781. // string_number += "-";
  782. // }
  783. // for (size_t i = GetNumberOfDigits() - 1; i < GetNumberOfDigits(); --i) {
  784. //// try {
  785. //// std::string digit = std::to_string((vector_of_digits[i]));
  786. //// string_number.append(digit);
  787. //// } catch (...) {
  788. //// std::cout<<"toString exception";
  789. //// exit(1);
  790. //// }
  791. // string_number += '0' + vector_of_digits[i];
  792. // }
  793. return "1234";
  794. }
  795.  
  796.  
  797. #endif //FIVT_4_BIGINTEGER_H
Add Comment
Please, Sign In to add comment