bigint class c++ code example

Example 1: bigint c++

//Bigint.hpp class
#ifndef BIGINT_HPP
#define BIGINT_HPP

#define BASE 10

#include <iostream>
#include <list>
#include"Command.hpp"
class Bigint {
public:
  Bigint() = default;  // Construct expect no arg 
  Bigint(const Bigint&) = default; // Copy constructor 
  Bigint(Bigint&&) = default; // Move constructor
  Bigint& operator=(const Bigint&) = default; // Copy assignment 
  Bigint& operator=(Bigint&&) = default; // Move assignment 
  ~Bigint(); // Destructort 
  Bigint(std::list<unsigned char>B);
  // Accessors methods ########################################################################
  bool is_zero()const;

  bool is_negative() const;

  // Friends ####################################################################################
  friend Bigint operator+(const Bigint& a, const Bigint& b);
  friend Bigint operator-(const Bigint& a, const Bigint& b);
  friend Bigint operator*(const Bigint& a, const Bigint& b);

  friend std::ostream& operator<<(std::ostream& out, const Bigint& i);
  friend std::istream& operator>>(std::istream& in, Bigint& i);
  friend Bigint minus_operator_cases(const Bigint& a, const Bigint& b);
private:
  //! Determine whether the integer is negative.
  bool m_is_negative = false;

  //! A linked list of digits.
  std::list<unsigned char> m_digits;
};

Bigint operator+(const Bigint& a, const Bigint& b);
Bigint operator-(const Bigint& a, const Bigint& b);
Bigint operator*(const Bigint& a, const Bigint& b);
std::ostream& operator<<(std::ostream& out, const Bigint& i);
std::istream& operator>>(std::istream& in, Bigint& i);
#endif

Example 2: bigint c++

//bigint.cpp methods implementation
#include <stdexcept>
#include"Bigint.hpp"
#include<algorithm>
/**
//**************************** overloading operator multiplication ********************************************
********************************         Amir Ammar                 *******************************************/
Bigint operator*(const Bigint& a, const Bigint& b){
  Bigint temp1 = a;
  Bigint temp2 = b;
  Bigint temp3;  // using temp3 object as a temporary to store the chars each iteration 
  temp3.m_digits ={'0'};
  Bigint temp4;  // for the arithmetic operation + between (temp3+temp4) and next temp4 to store the value of temp3 
  temp4.m_digits = {'0'};
  Bigint mult;      // the final values will be stored here and we return mult 
  mult.m_digits = {'0'};
  if(temp1.is_zero()||temp2.is_zero()){return mult.m_digits;}  // if one of them is zero we return mult == '0';
  char devide_by_ten{'0'};     // initializing devided by ten variable as a char type
  char devide_by_modulo{'0'};  // initializing devided by modulo variable as a char type 
  int size_of_in {0};          // size of in is a variable that will decide next how many zeroes will be pushed inside temp3 each iteration
  if(temp1.m_digits>temp2.m_digits){  // if statement to in
    size_of_in = temp2.m_digits.size();// i decided to intilize it according to the size of the bigger Bigint.size()
  }else{
    size_of_in = temp1.m_digits.size();
  }
  int push_zeros = size_of_in ; // we need to loop and push zeroes so i need this variable first to have the same value as size_of_in
  int counter = 0; // to increment the counter each time a zero is pushed and finally to break the loop 
  int minus_one = 0; // to decrment the valuee by one each loop
  auto iterator1=temp1.m_digits.end(); 
  auto iterator2=temp2.m_digits.end();
  // auto iterator1_1=temp1.m_digits.end(); // those variables was a part of experiment that succeeded 
  // auto iterator2_2=temp2.m_digits.end();  // those variables was a part of experiment that succeeded 
   while(true)//iterator1_1!=temp1.begin()||iterator2_2!=temp2.begin()
  {
      temp3.m_digits.clear();                              // cleaning the temp3 each loop so we can store the new value 
        while(push_zeros+minus_one+counter != size_of_in){ // while loop to push zero for temp3 each iteration
          ++counter;
          temp3.m_digits.emplace_front('0');
        }
        --minus_one;
        counter =0;
        --iterator2;
         if(iterator2==temp2.m_digits.end())break;  // break when we finally reached the last iteration 
            while(iterator1!=temp1.m_digits.begin()){ 
              --iterator1;
              devide_by_modulo=((*iterator1-48)*(*iterator2-48)+(devide_by_ten-48))%10+'0';
              devide_by_ten = ((*iterator1-'0')*(*iterator2-'0')+(devide_by_ten-'0'))/10+'0';
              temp3.m_digits.emplace_front(devide_by_modulo);
            }
            if(devide_by_ten != '0')temp3.m_digits.emplace_front(devide_by_ten);
            devide_by_modulo = {'0'};
            devide_by_ten ={'0'};
    mult = temp3+temp4; // the arithmetic operation between temp3+temp4 stored inside mult 
    temp4= temp4+temp3;  // the result will be also stored inside temp4 so we can do another + operation but with different values inside temp3
    iterator1=temp1.m_digits.end();// initializing the iterator1 as the end so we could loop again with same values 
    // --iterator2_2; // experiment 
    // --iterator1_1; // experiment 
  }
  if(a.m_is_negative==true&& b.m_is_negative==true){return mult;} // if both negative we return mult with none negative sign 
  if(a.m_is_negative==true||b.m_is_negative==true)mult.m_digits.emplace_front('-'); // if one of them is negative we push - 
  return mult;  // and finally we return mult 
}

Example 3: bigint c++

// bigint methods implementation
#include <stdexcept>
#include"Bigint.hpp"
#include<algorithm>
//+++++++++++++++++++++++++++    overloaded pluse operator    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++        Amir Ammar             +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Bigint operator+(const Bigint& a, const Bigint& b){
  Bigint temp1 = a;
  Bigint temp2 = b;
  Bigint temp3;
  if(temp1.is_zero()&&temp2.is_zero()){temp3.m_digits={'0'};return temp3;}
  if(!temp1.is_negative() &&temp2.is_negative()){  // if  (+temp1) (+) (-temp2)  done 
    temp2.m_is_negative = false;
    temp3 = temp1-temp2;
    return temp3;
  }
  if(temp1.is_negative()&&!temp2.is_negative()){ // if  (-temp1) (+)  (+temp2)   done 
    temp1.m_is_negative = false;
    temp3 = temp2-temp1;
    return temp3;
  }
  int carry {0}; int sum{0};auto it1 = temp1.m_digits.rbegin(); auto it2 = temp2.m_digits.rbegin();
  while(it1 != temp1.m_digits.rend() && it2 != temp2.m_digits.rend() ){
    ((a.m_digits.size()>b.m_digits.size()))?temp2.m_digits.push_front('0'):temp1.m_digits.push_front('0');
    sum=((*it1-'0')+(*it2-'0')+carry);
    temp3.m_digits.emplace_front(sum%10+'0');
    carry=sum/10;
    ++it1; ++it2;
  }
  if(carry != 0)temp3.m_digits.emplace_front(carry+'0');
  if(temp1.is_negative()&&temp2.is_negative()) // if (-temp1) + (-temp2) done
  { 
    temp3.m_digits.emplace_front('-'); // we push eventually (-)
  }
  return temp3;
}
//-------------------------------- overloaded  minus operator ---------------------------------------- 
//--------------------------------------   Amir Ammar   ----------------------------------------
Bigint operator-(const Bigint& a,const Bigint& b)
{ 
  Bigint temp1(a);
  Bigint temp2(b);
  Bigint sub;
  if(temp1.is_zero()&&temp2.is_zero()){sub.m_digits={'0'};return sub;}
  if(!temp1.is_negative()&& temp2.is_negative()){  // (+temp1) - (-temp2) 
     temp2.m_is_negative = false;
     return temp1+temp2;    
  }
  if(temp1.is_negative() && !temp2.is_negative()){  // (-temp1) - (+temp2) 
     temp2.m_is_negative = true;
     return temp1+temp2;    
  }
  if(temp1.is_negative()&&temp2.is_negative()){
    temp1.m_is_negative = false;
    temp2.m_is_negative = false;
    return temp2 - temp1 ;
  }
  auto it_1 = temp1.m_digits.end(); 
  auto it_2 = temp2.m_digits.end(); 
  int one_less{0};
  int length = temp1.m_digits.size()-temp2.m_digits.size();
  if(length <0){
    for(int i = length ; i<0; ++i) // 
    ((temp1.m_digits.size()>temp2.m_digits.size())?temp2.m_digits.emplace_front('0'):temp1.m_digits.emplace_front('0'));
  }else
  {
    for(int i = length; i>0; --i)
      ((temp1.m_digits.size()>temp2.m_digits.size())?temp2.m_digits.emplace_front('0'):temp1.m_digits.emplace_front('0'));
  }
  auto it1 = temp1.m_digits.end(); auto it2 = temp2.m_digits.end();
  while(it1 != temp1.m_digits.begin() && it2 != temp2.m_digits.begin()){
      --it1; --it2; 
      int it1_int = *it1%48;
      int it2_int = *it2%48;
    if(it1_int-it2_int-one_less<0){
      it1_int+= 10;
      sub.m_digits.emplace_front(it1_int-it2_int-one_less+'0');
      one_less = 1;
    }else{
      sub.m_digits.emplace_front(it1_int-it2_int-one_less+'0');
      one_less = 0;
    }
  }
  if(one_less==1){
    sub=temp2-temp1 ;
    sub.m_digits.emplace_front('-');
  }
  while(sub.m_digits.front()=='0'&&sub.m_digits.size()!= 1){
    sub.m_digits.pop_front();
    if(sub.m_digits.size()== 1)
      break;
  }
  return sub.m_digits;
}

Example 4: bigint c++

//bigint.cpp methods implementation
#include <stdexcept>
#include"Bigint.hpp"
#include<algorithm>
// CONSTRUCTOR OVERLOAD 
Bigint::Bigint(std::list<unsigned char>B) 
   :m_digits(B){}
Bigint::~Bigint(){}
//##################################### is_zero #########>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Amir Ammar
bool Bigint::is_zero()const
{
     if(m_digits.front()=='0'){
       return true;
     }
     return false;
}
//##################################### is_negative #########################################################
bool Bigint::is_negative() const{
   if(m_is_negative == true){
     return  true ;
   }else 
     return false;
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<insertion operator overloading<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Amir Ammar
std::ostream& operator<<(std::ostream& out, const Bigint& i){
  for(auto b = i.m_digits.begin(); b != i.m_digits.end(); ++b){
    out<<(*b);
  }
  return (out);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>extraction operator overloading>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Efi Fogel
std::istream& operator>>(std::istream& in, Bigint& i) {
  char c;
  in.get(c);
  if (c == '-') i.m_is_negative = true;
  else {
    if (! std::isdigit(c)) throw std::runtime_error("Invalid input");
    i.m_digits.emplace_front(c);
  }
  while (in.get(c) && (c != 0xa)) {
    if (! std::isdigit(c)) throw std::runtime_error("Invalid input");
    i.m_digits.emplace_front(c);
  }
  i.m_digits.reverse(); // additional method to return the reversed value (the real input)
  while(i.m_digits.front()=='0'&&i.m_digits.size()!= 1){ // while loop to earse additional zeroes 
    i.m_digits.pop_front();
    if(i.m_digits.size()== 1)
      break;
  }
  return in;
}

Example 5: bigint c++

//+++++++++++++++++++++++++++    overloaded pluse operator    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++        Amir Ammar             +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Bigint operator+(const Bigint& a, const Bigint& b){
  Bigint temp1 = a;
  Bigint temp2 = b;
  Bigint temp3;
  if(temp1.is_zero()&&temp2.is_zero()){temp3.m_digits={'0'};return temp3;}
  if(!temp1.is_negative() &&temp2.is_negative()){  // if  (+temp1) (+) (-temp2)  done 
    temp2.m_is_negative = false;
    temp3 = temp1-temp2;
    return temp3;
  }
  if(temp1.is_negative()&&!temp2.is_negative()){ // if  (-temp1) (+)  (+temp2)   done 
    temp1.m_is_negative = false;
    temp3 = temp2-temp1;
    return temp3;
  }
  int carry {0}; int sum{0};auto it1 = temp1.m_digits.rbegin(); auto it2 = temp2.m_digits.rbegin();
  while(it1 != temp1.m_digits.rend() && it2 != temp2.m_digits.rend() ){
    ((a.m_digits.size()>b.m_digits.size()))?temp2.m_digits.push_front('0'):temp1.m_digits.push_front('0');
    sum=((*it1-'0')+(*it2-'0')+carry);
    temp3.m_digits.emplace_front(sum%10+'0');
    carry=sum/10;
    ++it1; ++it2;
  }
  if(carry != 0)temp3.m_digits.emplace_front(carry+'0');
  if(temp1.is_negative()&&temp2.is_negative()) // if (-temp1) + (-temp2) done
  { 
    temp3.m_digits.emplace_front('-'); // we push eventually (-)
  }
  return temp3;
}
//-------------------------------- overloaded  minus operator ---------------------------------------- 
//--------------------------------------   Amir Ammar   ----------------------------------------
Bigint operator-(const Bigint& a,const Bigint& b)
{ 
  Bigint temp1(a);
  Bigint temp2(b);
  Bigint sub;
  if(temp1.is_zero()&&temp2.is_zero()){sub.m_digits={'0'};return sub;}
  if(!temp1.is_negative()&& temp2.is_negative()){  // (+temp1) - (-temp2) 
     temp2.m_is_negative = false;
     return temp1+temp2;    
  }
  if(temp1.is_negative() && !temp2.is_negative()){  // (-temp1) - (+temp2) 
     temp2.m_is_negative = true;
     return temp1+temp2;    
  }
  if(temp1.is_negative()&&temp2.is_negative()){
    temp1.m_is_negative = false;
    temp2.m_is_negative = false;
    return temp2 - temp1 ;
  }
  auto it_1 = temp1.m_digits.end(); 
  auto it_2 = temp2.m_digits.end(); 
  int one_less{0};
  int length = temp1.m_digits.size()-temp2.m_digits.size();
  if(length <0){
    for(int i = length ; i<0; ++i) // 
    ((temp1.m_digits.size()>temp2.m_digits.size())?temp2.m_digits.emplace_front('0'):temp1.m_digits.emplace_front('0'));
  }else
  {
    for(int i = length; i>0; --i)
      ((temp1.m_digits.size()>temp2.m_digits.size())?temp2.m_digits.emplace_front('0'):temp1.m_digits.emplace_front('0'));
  }
  auto it1 = temp1.m_digits.end(); auto it2 = temp2.m_digits.end();
  while(it1 != temp1.m_digits.begin() && it2 != temp2.m_digits.begin()){
      --it1; --it2; 
      int it1_int = *it1%48;
      int it2_int = *it2%48;
    if(it1_int-it2_int-one_less<0){
      it1_int+= 10;
      sub.m_digits.emplace_front(it1_int-it2_int-one_less+'0');
      one_less = 1;
    }else{
      sub.m_digits.emplace_front(it1_int-it2_int-one_less+'0');
      one_less = 0;
    }
  }
  if(one_less==1){
    sub=temp2-temp1 ;
    sub.m_digits.emplace_front('-');
  }
  while(sub.m_digits.front()=='0'&&sub.m_digits.size()!= 1){
    sub.m_digits.pop_front();
    if(sub.m_digits.size()== 1)
      break;
  }
  return sub.m_digits;
}

Tags:

Cpp Example