Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- const int BASE = 10;
- class BigInteger {
- public:
- BigInteger(const std::vector<int>& _numbers, bool _is_positive);
- void Print() const;
- bool GetIsPositive() const;
- void SetIsPositive();
- BigInteger operator + (const BigInteger& other) const;
- BigInteger operator - (const BigInteger& other) const;
- BigInteger operator - ();
- BigInteger operator * (const BigInteger& other) const;
- BigInteger operator / (const BigInteger& other) const;
- void operator += (const BigInteger& other);
- void operator -= (const BigInteger& other);
- void operator *= (const BigInteger& other);
- void operator /= (const BigInteger& other);
- BigInteger operator%(const BigInteger& other) const;
- BigInteger operator++() const; // prefix-form
- BigInteger operator++(int notused) const; // postfix-form
- BigInteger operator--() const; // prefix-form
- BigInteger operator--(int notused) const; // postfix-form
- bool operator==(const BigInteger& other) const;
- bool operator!=(const BigInteger& other) const;
- bool operator<=(const BigInteger& other) const;
- bool operator>=(const BigInteger& other) const;
- bool operator<(const BigInteger& other) const;
- bool operator>(const BigInteger& other) const;
- std::string toString();
- private:
- std::vector<int> vector_of_numbers ;
- bool is_positive;
- std::vector<int> GetVectorOfNumbers () const;
- size_t GetNumberOfDigits() const;
- bool CompareByAbs(const BigInteger& other) const;
- };
- BigInteger::BigInteger(const std::vector<int>& _numbers, bool _is_positive) {
- vector_of_numbers = _numbers;
- is_positive = _is_positive;
- }
- std::vector<int> BigInteger::GetVectorOfNumbers() const {
- return vector_of_numbers;
- }
- bool BigInteger::GetIsPositive() const {
- return is_positive;
- }
- BigInteger BigInteger::operator+(const BigInteger &other) const {
- // this is positive, other is negative
- if (this->GetIsPositive() && !other.GetIsPositive()) {
- BigInteger other_copy = other; // MEMORY!
- other_copy.SetIsPositive();
- return (*this - other_copy);
- // this is negative, other is positive
- } else if (!this->GetIsPositive() && other.GetIsPositive()) {
- BigInteger this_copy = *this; // MEMORY!
- this_copy.SetIsPositive();
- return (other - this_copy);
- // this & other are negative
- } else if (!this->GetIsPositive() && !other.GetIsPositive()) {
- BigInteger this_copy = *this; // MEMORY!
- this_copy.SetIsPositive();
- BigInteger other_copy = other; // MEMORY!
- other_copy.SetIsPositive();
- return -(this_copy + other_copy);
- // both operands are positive
- } else {
- auto other_vector_of_numbers = other.GetVectorOfNumbers();
- std::vector<int> result;
- size_t n = vector_of_numbers.size();
- size_t m = other_vector_of_numbers.size();
- int to_next_charge = 0;
- for (size_t i = 0; i < n || i < m; ++i) {
- int rest = 0;
- int number = 0;
- if (i >= n) {
- number = other_vector_of_numbers[i] + to_next_charge;
- to_next_charge = 0;
- } else if (i >= m) {
- number = vector_of_numbers[i] + to_next_charge;
- to_next_charge = 0;
- } else {
- number = other_vector_of_numbers[i] + vector_of_numbers[i] + to_next_charge;
- rest = number%BASE;
- if ( number >= 10) {
- to_next_charge = 1;
- } else {
- to_next_charge = 0;
- }
- number = rest;
- }
- result.push_back(number);
- }
- if (to_next_charge != 0) {
- result.push_back(to_next_charge);
- }
- return {result, true};
- }
- }
- BigInteger BigInteger::operator-(const BigInteger &other) const {
- // this is positive, other is negative
- if (this->GetIsPositive() && !other.GetIsPositive()) {
- BigInteger other_copy = other; // (+a) - (-b)
- other_copy.SetIsPositive();
- return (*this + other_copy);
- // this is negative, other is positive
- } else if (!this->GetIsPositive() && other.GetIsPositive()) { // (-a) - (+b)
- BigInteger this_copy = *this; // MEMORY!
- this_copy.SetIsPositive();
- BigInteger other_copy = other; // MEMORY!
- other_copy.SetIsPositive();
- return -(this_copy + other_copy); // -((+a) + (+b))
- // this & other are negative
- } else if (!this->GetIsPositive() && !other.GetIsPositive()) { //(-a) - (-b)
- BigInteger this_copy = *this; // MEMORY!
- this_copy.SetIsPositive();
- BigInteger other_copy = other; // MEMORY!
- other_copy.SetIsPositive();
- return (other_copy - this_copy);
- // both operands are positive
- } else { //(+a) - (+b)
- std::vector<int> result;
- // из большего - меньшее
- if ( *this > other ) {
- auto other_vector_of_numbers = other.GetVectorOfNumbers();
- size_t n = vector_of_numbers.size();
- size_t m = other_vector_of_numbers.size();
- bool take_base_from_high_charge = false;
- for (size_t i = 0; i < n || i < m; ++i) {
- int number = 0;
- if (i >= m) { // m < n
- number = vector_of_numbers[i];
- } else {
- number = vector_of_numbers[i] - other_vector_of_numbers[i];
- }
- if (take_base_from_high_charge) {
- number -= 1;
- }
- if (number < 0) {
- result.push_back(number + BASE);
- take_base_from_high_charge = true;
- } else {
- result.push_back(number);
- }
- }
- return {result, true};
- // сводим к вычитанию из большего меньшее
- } else if (*this == other) {
- return {result, true};
- } else {
- return -(other - *this);
- }
- }
- }
- BigInteger BigInteger::operator-() {
- this->is_positive = false;
- return *this;
- }
- BigInteger BigInteger::operator*(const BigInteger &other) const {
- size_t n = this->GetNumberOfDigits();
- size_t m = other.GetNumberOfDigits();
- // умножение длинного на короткое
- if ( n >= m ) {
- std::vector<int> this_vector = this->GetVectorOfNumbers();
- std::vector<int> other_vector = other.GetVectorOfNumbers();
- bool result_sign = this->GetIsPositive() & other.GetIsPositive();
- std::vector<BigInteger> parts;
- for (int j = 0; j < m; ++j) { // идем по короткому
- int to_next_charge = 0;
- std::vector<int> new_part(n + j);
- for (int i = 0; i < n; ++i) { // по длинному
- int value = (other_vector[j] * this_vector[i]) + to_next_charge;
- int rest = value % BASE;
- if ( rest < value ) { //число больше 10
- to_next_charge = (value - rest)/BASE;
- } else {
- to_next_charge = 0;
- }
- new_part[i + j] = rest;
- }
- if ( to_next_charge > 0 ) {
- new_part.push_back(to_next_charge);
- }
- parts.push_back({new_part,true});
- }
- BigInteger result = parts[0];
- for(int i = 1; i < parts.size(); ++i) {
- result += parts[i];
- }
- return {result.GetVectorOfNumbers(), result_sign};
- } else {
- return (other * (*this));
- }
- }
- BigInteger BigInteger::operator/(const BigInteger &other) const {
- return BigInteger(std::vector<int>(), false);
- }
- void BigInteger::operator+=(const BigInteger &other) {
- *this = (*this + other);
- }
- void BigInteger::operator-=(const BigInteger &other) {
- *this = (*this - other);
- }
- void BigInteger::operator*=(const BigInteger &other) {
- *this = (*this * other);
- }
- void BigInteger::operator/=(const BigInteger &other) {
- *this = (*this / other);
- }
- bool BigInteger::operator==(const BigInteger &other) const {
- size_t n = this->GetNumberOfDigits();
- if ( n != other.GetNumberOfDigits() ) {
- return false;
- } else {
- if ( this->GetIsPositive() != other.GetIsPositive() ) {
- return false;
- } else {
- auto this_vector = this->GetVectorOfNumbers();
- auto other_vector = other.GetVectorOfNumbers();
- for (size_t i = 0; i < n; ++i) {
- if (this_vector[i] != other_vector[i]) { // there isn't matter where we are starting
- return false;
- }
- }
- return true;
- }
- }
- }
- void BigInteger::Print() const {
- std::vector<int> vector = GetVectorOfNumbers();
- for (int i = vector.size() - 1; i >= 0; --i) {
- std::cout<<vector[i];
- }
- std::cout<<'\n';
- }
- size_t BigInteger::GetNumberOfDigits() const {
- return vector_of_numbers.size();
- }
- bool BigInteger::operator!=(const BigInteger &other) const {
- return !( *this == other );
- }
- bool BigInteger::operator<=(const BigInteger &other) const {
- bool is_equal = ( *this == other );
- bool is_less = (*this < other);
- return is_equal || is_less;
- }
- bool BigInteger::operator>=(const BigInteger &other) const {
- bool is_equal = ( *this == other );
- bool is_more = (*this > other);
- return is_equal || is_more;
- }
- bool BigInteger::CompareByAbs(const BigInteger &other) const { // is this < other by abs?
- size_t n = this->GetNumberOfDigits();
- size_t m = other.GetNumberOfDigits();
- if ( n < m ) {
- return true;
- } else if ( m == n ) {
- auto this_vector = this->GetVectorOfNumbers();
- auto other_vector = other.GetVectorOfNumbers();
- for (size_t i = n - 1; i >= 0; --i) { // start from the end
- if (this_vector[i] < other_vector[i]) {
- return true;
- } else if (this_vector[i] > other_vector[i]) {
- return false;
- }
- }
- return false;
- } else {
- return false;
- }
- }
- bool BigInteger::operator<(const BigInteger &other) const {
- bool is_less = false;
- // this - negative , other - positive
- if ( !this->GetIsPositive() && other.GetIsPositive() ) {
- is_less = true;
- // this - positive , other - negative
- } else if ( this->GetIsPositive() && !other.GetIsPositive() ) {
- is_less = false;
- // this & other have similar sign
- } else {
- // this & other negative
- if ( !this->GetIsPositive() && !other.GetIsPositive() ) {
- is_less = !(this->CompareByAbs(other));
- }
- // this & other positive
- if ( this->GetIsPositive() && other.GetIsPositive() ){
- is_less = this->CompareByAbs(other);
- }
- }
- return is_less && (*this != other);
- }
- bool BigInteger::operator>(const BigInteger &other) const {
- return (!(*this < other)) && (*this != other);
- }
- void BigInteger::SetIsPositive() {
- this->is_positive = true;
- }
- int main() {
- BigInteger a({1,2,3,4,5,6,7,8,9},true);
- BigInteger b({9,8,7,6,5,4,3,2,1},true);
- // std::cout<<(a != b)<<std::endl;
- // std::cout<<(a == b)<<std::endl;
- // std::cout<<(a < b)<<std::endl;
- // std::cout<<(a > b)<<std::endl;
- // std::cout<<(a <= b)<<std::endl;
- // std::cout<<(a >= b)<<std::endl;
- (a * b).Print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement