bigint() code example

Example: bigint

#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;
}

Tags:

Cpp Example