bigint in c++ code example
Example 1: bigint c++
#ifndef BIGINT_HPP
#define BIGINT_HPP
#define BASE 10
#include <iostream>
#include <list>
#include"Command.hpp"
class Bigint {
public:
Bigint() = default;
Bigint(const Bigint&) = default;
Bigint(Bigint&&) = default;
Bigint& operator=(const Bigint&) = default;
Bigint& operator=(Bigint&&) = default;
~Bigint();
Bigint(std::list<unsigned char>B);
bool is_zero()const;
bool is_negative() const;
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:
bool m_is_negative = false;
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++
#include <stdexcept>
#include"Bigint.hpp"
#include<algorithm>
Bigint operator*(const Bigint& a, const Bigint& b){
Bigint temp1 = a;
Bigint temp2 = b;
Bigint temp3;
temp3.m_digits ={'0'};
Bigint temp4;
temp4.m_digits = {'0'};
Bigint mult;
mult.m_digits = {'0'};
if(temp1.is_zero()||temp2.is_zero()){return mult.m_digits;}
char devide_by_ten{'0'};
char devide_by_modulo{'0'};
int size_of_in {0};
if(temp1.m_digits>temp2.m_digits){
size_of_in = temp2.m_digits.size();
}else{
size_of_in = temp1.m_digits.size();
}
int push_zeros = size_of_in ;
int counter = 0;
int minus_one = 0;
auto iterator1=temp1.m_digits.end();
auto iterator2=temp2.m_digits.end();
while(true)
{
temp3.m_digits.clear();
while(push_zeros+minus_one+counter != size_of_in){
++counter;
temp3.m_digits.emplace_front('0');
}
--minus_one;
counter =0;
--iterator2;
if(iterator2==temp2.m_digits.end())break;
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;
temp4= temp4+temp3;
iterator1=temp1.m_digits.end();
}
if(a.m_is_negative==true&& b.m_is_negative==true){return mult;}
if(a.m_is_negative==true||b.m_is_negative==true)mult.m_digits.emplace_front('-');
return mult;
}
Example 3: bigint c++
#include <stdexcept>
#include"Bigint.hpp"
#include<algorithm>
Bigint::Bigint(std::list<unsigned char>B)
:m_digits(B){}
Bigint::~Bigint(){}
bool Bigint::is_zero()const
{
if(m_digits.front()=='0'){
return true;
}
return false;
}
bool Bigint::is_negative() const{
if(m_is_negative == true){
return true ;
}else
return false;
}
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);
}
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();
while(i.m_digits.front()=='0'&&i.m_digits.size()!= 1){
i.m_digits.pop_front();
if(i.m_digits.size()== 1)
break;
}
return in;
}