Answers for "biginteger c++ implementation"

C++
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
Posted by: Guest on February-25-2021
0

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;
}
Posted by: Guest on February-25-2021

Browse Popular Code Answers by Language