Advertisement
blufzzz

Fivt_4/task-A/05.12.2017 (wrong +)

Dec 5th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. const int BASE = 10;
  6.  
  7. class BigInteger {
  8. public:
  9.     BigInteger(const std::vector<int>& _numbers, bool _is_positive);
  10.     void Print() const;
  11.     bool GetIsPositive() const;
  12.     void SetIsPositive();
  13.  
  14.  
  15.     BigInteger operator + (const BigInteger& other) const;
  16.     BigInteger operator - (const BigInteger& other) const;
  17.     BigInteger operator - ();
  18.     BigInteger operator * (const BigInteger& other) const;
  19.     BigInteger operator / (const BigInteger& other) const;
  20.  
  21.     void operator += (const BigInteger& other);
  22.     void operator -= (const BigInteger& other);
  23.     void operator *= (const BigInteger& other);
  24.     void operator /= (const BigInteger& other);
  25.  
  26.     BigInteger operator%(const BigInteger& other) const;
  27.     BigInteger operator++() const;              // prefix-form
  28.     BigInteger operator++(int notused) const; // postfix-form
  29.     BigInteger operator--() const;              // prefix-form
  30.     BigInteger operator--(int notused) const; // postfix-form
  31.  
  32.     bool operator==(const BigInteger& other) const;
  33.     bool operator!=(const BigInteger& other) const;
  34.     bool operator<=(const BigInteger& other) const;
  35.     bool operator>=(const BigInteger& other) const;
  36.     bool operator<(const BigInteger& other) const;
  37.     bool operator>(const BigInteger& other) const;
  38.  
  39.     std::string toString();
  40. private:
  41.     std::vector<int> vector_of_numbers ;
  42.     bool is_positive;
  43.     std::vector<int> GetVectorOfNumbers () const;
  44.     size_t GetNumberOfDigits() const;
  45.     bool CompareByAbs(const BigInteger& other) const;
  46. };
  47.  
  48.  
  49.  
  50. BigInteger::BigInteger(const std::vector<int>& _numbers, bool _is_positive) {
  51.     vector_of_numbers = _numbers;
  52.     is_positive = _is_positive;
  53. }
  54.  
  55.  
  56.  
  57. std::vector<int> BigInteger::GetVectorOfNumbers() const {
  58.     return vector_of_numbers;
  59. }
  60.  
  61.  
  62.  
  63. bool BigInteger::GetIsPositive() const {
  64.     return is_positive;
  65. }
  66.  
  67.  
  68.  
  69. BigInteger BigInteger::operator+(const BigInteger &other) const {
  70.  
  71.         // this is positive, other is negative
  72.     if (this->GetIsPositive() && !other.GetIsPositive()) {
  73.         BigInteger other_copy = other;                                      // MEMORY!
  74.         other_copy.SetIsPositive();
  75.         return (*this - other_copy);
  76.  
  77.         // this is negative, other is positive
  78.     } else if (!this->GetIsPositive() && other.GetIsPositive()) {
  79.         BigInteger this_copy = *this;                                       // MEMORY!
  80.         this_copy.SetIsPositive();
  81.         return (other - this_copy);
  82.  
  83.         // this & other are negative
  84.     } else if (!this->GetIsPositive() && !other.GetIsPositive()) {
  85.         BigInteger this_copy = *this;                                       // MEMORY!
  86.         this_copy.SetIsPositive();
  87.         BigInteger other_copy = other;                                      // MEMORY!
  88.         other_copy.SetIsPositive();
  89.         return -(this_copy + other_copy);
  90.  
  91.         // both operands are positive
  92.     } else {
  93.  
  94.         auto other_vector_of_numbers = other.GetVectorOfNumbers();
  95.         std::vector<int> result;
  96.         size_t n = vector_of_numbers.size();
  97.         size_t m = other_vector_of_numbers.size();
  98.  
  99.         int to_next_charge = 0;
  100.         for (size_t i = 0; i < n || i < m; ++i) {
  101.             int rest = 0;
  102.             int number = 0;
  103.             if (i >= n) {
  104.                 number = other_vector_of_numbers[i] + to_next_charge;
  105.                 to_next_charge = 0;
  106.             } else if (i >= m) {
  107.                 number = vector_of_numbers[i] + to_next_charge;
  108.                 to_next_charge = 0;
  109.             } else {
  110.                 number = other_vector_of_numbers[i] + vector_of_numbers[i] + to_next_charge;
  111.                 rest = number%BASE;
  112.                 if ( number >= 10) {
  113.                     to_next_charge = 1;
  114.                 } else {
  115.                     to_next_charge = 0;
  116.                 }
  117.                 number = rest;
  118.             }
  119.  
  120.             result.push_back(number);
  121.         }
  122.  
  123.         if (to_next_charge != 0) {
  124.             result.push_back(to_next_charge);
  125.         }
  126.  
  127.         return {result, true};
  128.     }
  129. }
  130.  
  131.  
  132.  
  133. BigInteger BigInteger::operator-(const BigInteger &other) const {
  134.  
  135.     // this is positive, other is negative
  136.     if (this->GetIsPositive() && !other.GetIsPositive()) {
  137.         BigInteger other_copy = other;                                      // (+a) - (-b)
  138.         other_copy.SetIsPositive();
  139.         return (*this + other_copy);
  140.  
  141.         // this is negative, other is positive
  142.     } else if (!this->GetIsPositive() && other.GetIsPositive()) {           // (-a) - (+b)
  143.         BigInteger this_copy = *this;                                       // MEMORY!
  144.         this_copy.SetIsPositive();
  145.         BigInteger other_copy = other;                                      // MEMORY!
  146.         other_copy.SetIsPositive();
  147.         return -(this_copy + other_copy);                                   // -((+a) + (+b))
  148.  
  149.         // this & other are negative
  150.     } else if (!this->GetIsPositive() && !other.GetIsPositive()) {          //(-a) - (-b)
  151.         BigInteger this_copy = *this;                                       // MEMORY!
  152.         this_copy.SetIsPositive();
  153.         BigInteger other_copy = other;                                      // MEMORY!
  154.         other_copy.SetIsPositive();
  155.         return (other_copy - this_copy);
  156.  
  157.         // both operands are positive
  158.     } else {                                                                //(+a) - (+b)
  159.         std::vector<int> result;
  160.         // из большего - меньшее
  161.         if ( *this > other ) {
  162.  
  163.             auto other_vector_of_numbers = other.GetVectorOfNumbers();
  164.  
  165.             size_t n = vector_of_numbers.size();
  166.             size_t m = other_vector_of_numbers.size();
  167.  
  168.  
  169.             bool take_base_from_high_charge = false;
  170.             for (size_t i = 0; i < n || i < m; ++i) {
  171.  
  172.                 int number = 0;
  173.  
  174.                 if (i >= m) {  // m < n
  175.                     number = vector_of_numbers[i];
  176.                 } else {
  177.                     number = vector_of_numbers[i] - other_vector_of_numbers[i];
  178.                 }
  179.  
  180.                 if (take_base_from_high_charge) {
  181.                     number -= 1;
  182.                 }
  183.  
  184.                 if (number < 0) {
  185.                     result.push_back(number + BASE);
  186.                     take_base_from_high_charge = true;
  187.                 } else {
  188.                     result.push_back(number);
  189.                 }
  190.             }
  191.             return {result, true};
  192.  
  193.             // сводим к вычитанию из большего меньшее
  194.         } else if (*this == other) {
  195.             return {result, true};
  196.         } else {
  197.             return -(other - *this);
  198.         }
  199.     }
  200. }
  201.  
  202.  
  203.  
  204. BigInteger BigInteger::operator-() {
  205.     this->is_positive = false;
  206.     return *this;
  207. }
  208.  
  209.  
  210.  
  211. BigInteger BigInteger::operator*(const BigInteger &other) const {
  212.  
  213.     size_t n = this->GetNumberOfDigits();
  214.     size_t m = other.GetNumberOfDigits();
  215.  
  216.     // умножение длинного на короткое
  217.     if ( n >= m ) {
  218.  
  219.         std::vector<int> this_vector = this->GetVectorOfNumbers();
  220.         std::vector<int> other_vector = other.GetVectorOfNumbers();
  221.  
  222.         bool result_sign = this->GetIsPositive() & other.GetIsPositive();
  223.  
  224.         std::vector<BigInteger> parts;
  225.         for (int j = 0; j < m; ++j) { // идем по короткому
  226.             int to_next_charge = 0;
  227.             std::vector<int> new_part(n + j);
  228.             for (int i = 0; i < n; ++i) { // по длинному
  229.  
  230.                 int value = (other_vector[j] * this_vector[i]) + to_next_charge;
  231.                 int rest = value % BASE;
  232.  
  233.                 if ( rest < value ) { //число больше 10
  234.                     to_next_charge = (value - rest)/BASE;
  235.                 } else {
  236.                     to_next_charge = 0;
  237.                 }
  238.                 new_part[i + j] = rest;
  239.  
  240.             }
  241.  
  242.             if ( to_next_charge > 0 ) {
  243.                 new_part.push_back(to_next_charge);
  244.             }
  245.  
  246.             parts.push_back({new_part,true});
  247.         }
  248.  
  249.         BigInteger result = parts[0];
  250.         for(int i = 1; i < parts.size(); ++i) {
  251.             result += parts[i];
  252.         }
  253.  
  254.         return {result.GetVectorOfNumbers(), result_sign};
  255.  
  256.     } else {
  257.         return (other * (*this));
  258.     }
  259.  
  260. }
  261.  
  262.  
  263.  
  264. BigInteger BigInteger::operator/(const BigInteger &other) const {
  265.     return BigInteger(std::vector<int>(), false);
  266. }
  267.  
  268.  
  269.  
  270. void BigInteger::operator+=(const BigInteger &other) {
  271.     *this = (*this + other);
  272. }
  273.  
  274.  
  275.  
  276. void BigInteger::operator-=(const BigInteger &other) {
  277.     *this = (*this - other);
  278. }
  279.  
  280. void BigInteger::operator*=(const BigInteger &other) {
  281.     *this = (*this * other);
  282. }
  283.  
  284.  
  285.  
  286. void BigInteger::operator/=(const BigInteger &other) {
  287.     *this = (*this / other);
  288. }
  289.  
  290.  
  291.  
  292. bool BigInteger::operator==(const BigInteger &other) const {
  293.     size_t  n = this->GetNumberOfDigits();
  294.     if ( n != other.GetNumberOfDigits() ) {
  295.         return  false;
  296.     } else {
  297.         if ( this->GetIsPositive() != other.GetIsPositive() ) {
  298.             return  false;
  299.         } else {
  300.             auto this_vector = this->GetVectorOfNumbers();
  301.             auto other_vector = other.GetVectorOfNumbers();
  302.             for (size_t i = 0; i < n; ++i) {
  303.                 if (this_vector[i] != other_vector[i]) { // there isn't matter where we are starting
  304.                     return false;
  305.                 }
  306.             }
  307.             return true;
  308.         }
  309.     }
  310. }
  311.  
  312.  
  313.  
  314. void BigInteger::Print() const {
  315.     std::vector<int> vector = GetVectorOfNumbers();
  316.     for (int i = vector.size() - 1; i >= 0; --i) {
  317.         std::cout<<vector[i];
  318.     }
  319.     std::cout<<'\n';
  320. }
  321.  
  322.  
  323.  
  324. size_t BigInteger::GetNumberOfDigits() const {
  325.     return vector_of_numbers.size();
  326. }
  327.  
  328. bool BigInteger::operator!=(const BigInteger &other) const {
  329.     return !( *this == other );
  330. }
  331.  
  332. bool BigInteger::operator<=(const BigInteger &other) const {
  333.     bool is_equal = ( *this == other );
  334.     bool is_less = (*this < other);
  335.     return is_equal || is_less;
  336. }
  337.  
  338.  
  339.  
  340. bool BigInteger::operator>=(const BigInteger &other) const {
  341.     bool is_equal = ( *this == other );
  342.     bool is_more = (*this > other);
  343.     return is_equal || is_more;
  344. }
  345.  
  346.  
  347.  
  348. bool BigInteger::CompareByAbs(const BigInteger &other) const { // is this < other by abs?
  349.     size_t  n = this->GetNumberOfDigits();
  350.     size_t m = other.GetNumberOfDigits();
  351.  
  352.     if ( n < m ) {
  353.         return  true;
  354.     } else if ( m == n ) {
  355.  
  356.         auto this_vector = this->GetVectorOfNumbers();
  357.         auto other_vector = other.GetVectorOfNumbers();
  358.         for (size_t i = n - 1; i >= 0; --i) {  // start from the end
  359.             if (this_vector[i] < other_vector[i]) {
  360.                 return true;
  361.             } else if (this_vector[i] > other_vector[i]) {
  362.                 return  false;
  363.             }
  364.         }
  365.         return  false;
  366.     } else {
  367.         return false;
  368.     }
  369. }
  370.  
  371. bool BigInteger::operator<(const BigInteger &other) const {
  372.     bool is_less = false;
  373.     // this - negative , other - positive
  374.     if ( !this->GetIsPositive() && other.GetIsPositive() ) {
  375.         is_less = true;
  376.  
  377.         // this - positive , other - negative
  378.     } else if ( this->GetIsPositive() && !other.GetIsPositive() ) {
  379.         is_less = false;
  380.  
  381.         // this & other have similar sign
  382.     } else {
  383.         // this & other negative
  384.         if ( !this->GetIsPositive() && !other.GetIsPositive() ) {
  385.             is_less = !(this->CompareByAbs(other));
  386.         }
  387.         // this & other positive
  388.         if ( this->GetIsPositive() && other.GetIsPositive() ){
  389.             is_less = this->CompareByAbs(other);
  390.         }
  391.     }
  392.     return  is_less && (*this != other);
  393. }
  394.  
  395.  
  396.  
  397. bool BigInteger::operator>(const BigInteger &other) const {
  398.     return (!(*this < other)) && (*this != other);
  399. }
  400.  
  401. void BigInteger::SetIsPositive() {
  402.     this->is_positive = true;
  403. }
  404.  
  405.  
  406.  
  407.  
  408. int main() {
  409.  
  410.     BigInteger a({1,2,3,4,5,6,7,8,9},true);
  411.     BigInteger b({9,8,7,6,5,4,3,2,1},true);
  412.  
  413. //  std::cout<<(a != b)<<std::endl;
  414. //  std::cout<<(a == b)<<std::endl;
  415. //  std::cout<<(a < b)<<std::endl;
  416. //  std::cout<<(a > b)<<std::endl;
  417. //  std::cout<<(a <= b)<<std::endl;
  418. //  std::cout<<(a >= b)<<std::endl;
  419.  
  420.     (a * b).Print();
  421.  
  422.     return  0;
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement