bigint size in c++ code example
Example 1: 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 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;
if(temp1.is_zero()&&temp2.is_zero()){temp3.m_digits={'0'};return temp3;}
if(!temp1.is_negative() &&temp2.is_negative()){
temp2.m_is_negative = false;
temp3 = temp1-temp2;
return temp3;
}
if(temp1.is_negative()&&!temp2.is_negative()){
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())
{
temp3.m_digits.emplace_front('-');
}
return temp3;
}
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()){
temp2.m_is_negative = false;
return temp1+temp2;
}
if(temp1.is_negative() && !temp2.is_negative()){
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;
}