bigint c 2b 2b

Solutions on MaxInterview for bigint c 2b 2b by the best coders in the world

showing results for - "bigint c 2b 2b"
Élie
16 May 2017
1//Bigint.hpp class
2#ifndef BIGINT_HPP
3#define BIGINT_HPP
4
5#define BASE 10
6
7#include <iostream>
8#include <list>
9#include"Command.hpp"
10class Bigint {
11public:
12  Bigint() = default;  // Construct expect no arg 
13  Bigint(const Bigint&) = default; // Copy constructor 
14  Bigint(Bigint&&) = default; // Move constructor
15  Bigint& operator=(const Bigint&) = default; // Copy assignment 
16  Bigint& operator=(Bigint&&) = default; // Move assignment 
17  ~Bigint(); // Destructort 
18  Bigint(std::list<unsigned char>B);
19  // Accessors methods ########################################################################
20  bool is_zero()const;
21
22  bool is_negative() const;
23
24  // Friends ####################################################################################
25  friend Bigint operator+(const Bigint& a, const Bigint& b);
26  friend Bigint operator-(const Bigint& a, const Bigint& b);
27  friend Bigint operator*(const Bigint& a, const Bigint& b);
28
29  friend std::ostream& operator<<(std::ostream& out, const Bigint& i);
30  friend std::istream& operator>>(std::istream& in, Bigint& i);
31  friend Bigint minus_operator_cases(const Bigint& a, const Bigint& b);
32private:
33  //! Determine whether the integer is negative.
34  bool m_is_negative = false;
35
36  //! A linked list of digits.
37  std::list<unsigned char> m_digits;
38};
39
40Bigint operator+(const Bigint& a, const Bigint& b);
41Bigint operator-(const Bigint& a, const Bigint& b);
42Bigint operator*(const Bigint& a, const Bigint& b);
43std::ostream& operator<<(std::ostream& out, const Bigint& i);
44std::istream& operator>>(std::istream& in, Bigint& i);
45#endif
46
Axel
29 Oct 2020
1//bigint.cpp methods implementation
2#include <stdexcept>
3#include"Bigint.hpp"
4#include<algorithm>
5/**
6//**************************** overloading operator multiplication ********************************************
7********************************         Amir Ammar                 *******************************************/
8Bigint operator*(const Bigint& a, const Bigint& b){
9  Bigint temp1 = a;
10  Bigint temp2 = b;
11  Bigint temp3;  // using temp3 object as a temporary to store the chars each iteration 
12  temp3.m_digits ={'0'};
13  Bigint temp4;  // for the arithmetic operation + between (temp3+temp4) and next temp4 to store the value of temp3 
14  temp4.m_digits = {'0'};
15  Bigint mult;      // the final values will be stored here and we return mult 
16  mult.m_digits = {'0'};
17  if(temp1.is_zero()||temp2.is_zero()){return mult.m_digits;}  // if one of them is zero we return mult == '0';
18  char devide_by_ten{'0'};     // initializing devided by ten variable as a char type
19  char devide_by_modulo{'0'};  // initializing devided by modulo variable as a char type 
20  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
21  if(temp1.m_digits>temp2.m_digits){  // if statement to in
22    size_of_in = temp2.m_digits.size();// i decided to intilize it according to the size of the bigger Bigint.size()
23  }else{
24    size_of_in = temp1.m_digits.size();
25  }
26  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
27  int counter = 0; // to increment the counter each time a zero is pushed and finally to break the loop 
28  int minus_one = 0; // to decrment the valuee by one each loop
29  auto iterator1=temp1.m_digits.end(); 
30  auto iterator2=temp2.m_digits.end();
31  // auto iterator1_1=temp1.m_digits.end(); // those variables was a part of experiment that succeeded 
32  // auto iterator2_2=temp2.m_digits.end();  // those variables was a part of experiment that succeeded 
33   while(true)//iterator1_1!=temp1.begin()||iterator2_2!=temp2.begin()
34  {
35      temp3.m_digits.clear();                              // cleaning the temp3 each loop so we can store the new value 
36        while(push_zeros+minus_one+counter != size_of_in){ // while loop to push zero for temp3 each iteration
37          ++counter;
38          temp3.m_digits.emplace_front('0');
39        }
40        --minus_one;
41        counter =0;
42        --iterator2;
43         if(iterator2==temp2.m_digits.end())break;  // break when we finally reached the last iteration 
44            while(iterator1!=temp1.m_digits.begin()){ 
45              --iterator1;
46              devide_by_modulo=((*iterator1-48)*(*iterator2-48)+(devide_by_ten-48))%10+'0';
47              devide_by_ten = ((*iterator1-'0')*(*iterator2-'0')+(devide_by_ten-'0'))/10+'0';
48              temp3.m_digits.emplace_front(devide_by_modulo);
49            }
50            if(devide_by_ten != '0')temp3.m_digits.emplace_front(devide_by_ten);
51            devide_by_modulo = {'0'};
52            devide_by_ten ={'0'};
53    mult = temp3+temp4; // the arithmetic operation between temp3+temp4 stored inside mult 
54    temp4= temp4+temp3;  // the result will be also stored inside temp4 so we can do another + operation but with different values inside temp3
55    iterator1=temp1.m_digits.end();// initializing the iterator1 as the end so we could loop again with same values 
56    // --iterator2_2; // experiment 
57    // --iterator1_1; // experiment 
58  }
59  if(a.m_is_negative==true&& b.m_is_negative==true){return mult;} // if both negative we return mult with none negative sign 
60  if(a.m_is_negative==true||b.m_is_negative==true)mult.m_digits.emplace_front('-'); // if one of them is negative we push - 
61  return mult;  // and finally we return mult 
62}
Rafael
23 Nov 2018
1// bigint methods implementation
2#include <stdexcept>
3#include"Bigint.hpp"
4#include<algorithm>
5//+++++++++++++++++++++++++++    overloaded pluse operator    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6// ++++++++++++++++++++++++++++        Amir Ammar             +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7Bigint operator+(const Bigint& a, const Bigint& b){
8  Bigint temp1 = a;
9  Bigint temp2 = b;
10  Bigint temp3;
11  if(temp1.is_zero()&&temp2.is_zero()){temp3.m_digits={'0'};return temp3;}
12  if(!temp1.is_negative() &&temp2.is_negative()){  // if  (+temp1) (+) (-temp2)  done 
13    temp2.m_is_negative = false;
14    temp3 = temp1-temp2;
15    return temp3;
16  }
17  if(temp1.is_negative()&&!temp2.is_negative()){ // if  (-temp1) (+)  (+temp2)   done 
18    temp1.m_is_negative = false;
19    temp3 = temp2-temp1;
20    return temp3;
21  }
22  int carry {0}; int sum{0};auto it1 = temp1.m_digits.rbegin(); auto it2 = temp2.m_digits.rbegin();
23  while(it1 != temp1.m_digits.rend() && it2 != temp2.m_digits.rend() ){
24    ((a.m_digits.size()>b.m_digits.size()))?temp2.m_digits.push_front('0'):temp1.m_digits.push_front('0');
25    sum=((*it1-'0')+(*it2-'0')+carry);
26    temp3.m_digits.emplace_front(sum%10+'0');
27    carry=sum/10;
28    ++it1; ++it2;
29  }
30  if(carry != 0)temp3.m_digits.emplace_front(carry+'0');
31  if(temp1.is_negative()&&temp2.is_negative()) // if (-temp1) + (-temp2) done
32  { 
33    temp3.m_digits.emplace_front('-'); // we push eventually (-)
34  }
35  return temp3;
36}
37//-------------------------------- overloaded  minus operator ---------------------------------------- 
38//--------------------------------------   Amir Ammar   ----------------------------------------
39Bigint operator-(const Bigint& a,const Bigint& b)
40{ 
41  Bigint temp1(a);
42  Bigint temp2(b);
43  Bigint sub;
44  if(temp1.is_zero()&&temp2.is_zero()){sub.m_digits={'0'};return sub;}
45  if(!temp1.is_negative()&& temp2.is_negative()){  // (+temp1) - (-temp2) 
46     temp2.m_is_negative = false;
47     return temp1+temp2;    
48  }
49  if(temp1.is_negative() && !temp2.is_negative()){  // (-temp1) - (+temp2) 
50     temp2.m_is_negative = true;
51     return temp1+temp2;    
52  }
53  if(temp1.is_negative()&&temp2.is_negative()){
54    temp1.m_is_negative = false;
55    temp2.m_is_negative = false;
56    return temp2 - temp1 ;
57  }
58  auto it_1 = temp1.m_digits.end(); 
59  auto it_2 = temp2.m_digits.end(); 
60  int one_less{0};
61  int length = temp1.m_digits.size()-temp2.m_digits.size();
62  if(length <0){
63    for(int i = length ; i<0; ++i) // 
64    ((temp1.m_digits.size()>temp2.m_digits.size())?temp2.m_digits.emplace_front('0'):temp1.m_digits.emplace_front('0'));
65  }else
66  {
67    for(int i = length; i>0; --i)
68      ((temp1.m_digits.size()>temp2.m_digits.size())?temp2.m_digits.emplace_front('0'):temp1.m_digits.emplace_front('0'));
69  }
70  auto it1 = temp1.m_digits.end(); auto it2 = temp2.m_digits.end();
71  while(it1 != temp1.m_digits.begin() && it2 != temp2.m_digits.begin()){
72      --it1; --it2; 
73      int it1_int = *it1%48;
74      int it2_int = *it2%48;
75    if(it1_int-it2_int-one_less<0){
76      it1_int+= 10;
77      sub.m_digits.emplace_front(it1_int-it2_int-one_less+'0');
78      one_less = 1;
79    }else{
80      sub.m_digits.emplace_front(it1_int-it2_int-one_less+'0');
81      one_less = 0;
82    }
83  }
84  if(one_less==1){
85    sub=temp2-temp1 ;
86    sub.m_digits.emplace_front('-');
87  }
88  while(sub.m_digits.front()=='0'&&sub.m_digits.size()!= 1){
89    sub.m_digits.pop_front();
90    if(sub.m_digits.size()== 1)
91      break;
92  }
93  return sub.m_digits;
94}
Johanna
26 Mar 2020
1//bigint.cpp methods implementation
2#include <stdexcept>
3#include"Bigint.hpp"
4#include<algorithm>
5// CONSTRUCTOR OVERLOAD 
6Bigint::Bigint(std::list<unsigned char>B) 
7   :m_digits(B){}
8Bigint::~Bigint(){}
9//##################################### is_zero #########>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Amir Ammar
10bool Bigint::is_zero()const
11{
12     if(m_digits.front()=='0'){
13       return true;
14     }
15     return false;
16}
17//##################################### is_negative #########################################################
18bool Bigint::is_negative() const{
19   if(m_is_negative == true){
20     return  true ;
21   }else 
22     return false;
23}
24//<<<<<<<<<<<<<<<<<<<<<<<<<<<insertion operator overloading<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Amir Ammar
25std::ostream& operator<<(std::ostream& out, const Bigint& i){
26  for(auto b = i.m_digits.begin(); b != i.m_digits.end(); ++b){
27    out<<(*b);
28  }
29  return (out);
30}
31//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>extraction operator overloading>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Efi Fogel
32std::istream& operator>>(std::istream& in, Bigint& i) {
33  char c;
34  in.get(c);
35  if (c == '-') i.m_is_negative = true;
36  else {
37    if (! std::isdigit(c)) throw std::runtime_error("Invalid input");
38    i.m_digits.emplace_front(c);
39  }
40  while (in.get(c) && (c != 0xa)) {
41    if (! std::isdigit(c)) throw std::runtime_error("Invalid input");
42    i.m_digits.emplace_front(c);
43  }
44  i.m_digits.reverse(); // additional method to return the reversed value (the real input)
45  while(i.m_digits.front()=='0'&&i.m_digits.size()!= 1){ // while loop to earse additional zeroes 
46    i.m_digits.pop_front();
47    if(i.m_digits.size()== 1)
48      break;
49  }
50  return in;
51}
Gael
11 Mar 2020
1
2//+++++++++++++++++++++++++++    overloaded pluse operator    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3// ++++++++++++++++++++++++++++        Amir Ammar             +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4Bigint operator+(const Bigint& a, const Bigint& b){
5  Bigint temp1 = a;
6  Bigint temp2 = b;
7  Bigint temp3;
8  if(temp1.is_zero()&&temp2.is_zero()){temp3.m_digits={'0'};return temp3;}
9  if(!temp1.is_negative() &&temp2.is_negative()){  // if  (+temp1) (+) (-temp2)  done 
10    temp2.m_is_negative = false;
11    temp3 = temp1-temp2;
12    return temp3;
13  }
14  if(temp1.is_negative()&&!temp2.is_negative()){ // if  (-temp1) (+)  (+temp2)   done 
15    temp1.m_is_negative = false;
16    temp3 = temp2-temp1;
17    return temp3;
18  }
19  int carry {0}; int sum{0};auto it1 = temp1.m_digits.rbegin(); auto it2 = temp2.m_digits.rbegin();
20  while(it1 != temp1.m_digits.rend() && it2 != temp2.m_digits.rend() ){
21    ((a.m_digits.size()>b.m_digits.size()))?temp2.m_digits.push_front('0'):temp1.m_digits.push_front('0');
22    sum=((*it1-'0')+(*it2-'0')+carry);
23    temp3.m_digits.emplace_front(sum%10+'0');
24    carry=sum/10;
25    ++it1; ++it2;
26  }
27  if(carry != 0)temp3.m_digits.emplace_front(carry+'0');
28  if(temp1.is_negative()&&temp2.is_negative()) // if (-temp1) + (-temp2) done
29  { 
30    temp3.m_digits.emplace_front('-'); // we push eventually (-)
31  }
32  return temp3;
33}
34//-------------------------------- overloaded  minus operator ---------------------------------------- 
35//--------------------------------------   Amir Ammar   ----------------------------------------
36Bigint operator-(const Bigint& a,const Bigint& b)
37{ 
38  Bigint temp1(a);
39  Bigint temp2(b);
40  Bigint sub;
41  if(temp1.is_zero()&&temp2.is_zero()){sub.m_digits={'0'};return sub;}
42  if(!temp1.is_negative()&& temp2.is_negative()){  // (+temp1) - (-temp2) 
43     temp2.m_is_negative = false;
44     return temp1+temp2;    
45  }
46  if(temp1.is_negative() && !temp2.is_negative()){  // (-temp1) - (+temp2) 
47     temp2.m_is_negative = true;
48     return temp1+temp2;    
49  }
50  if(temp1.is_negative()&&temp2.is_negative()){
51    temp1.m_is_negative = false;
52    temp2.m_is_negative = false;
53    return temp2 - temp1 ;
54  }
55  auto it_1 = temp1.m_digits.end(); 
56  auto it_2 = temp2.m_digits.end(); 
57  int one_less{0};
58  int length = temp1.m_digits.size()-temp2.m_digits.size();
59  if(length <0){
60    for(int i = length ; i<0; ++i) // 
61    ((temp1.m_digits.size()>temp2.m_digits.size())?temp2.m_digits.emplace_front('0'):temp1.m_digits.emplace_front('0'));
62  }else
63  {
64    for(int i = length; i>0; --i)
65      ((temp1.m_digits.size()>temp2.m_digits.size())?temp2.m_digits.emplace_front('0'):temp1.m_digits.emplace_front('0'));
66  }
67  auto it1 = temp1.m_digits.end(); auto it2 = temp2.m_digits.end();
68  while(it1 != temp1.m_digits.begin() && it2 != temp2.m_digits.begin()){
69      --it1; --it2; 
70      int it1_int = *it1%48;
71      int it2_int = *it2%48;
72    if(it1_int-it2_int-one_less<0){
73      it1_int+= 10;
74      sub.m_digits.emplace_front(it1_int-it2_int-one_less+'0');
75      one_less = 1;
76    }else{
77      sub.m_digits.emplace_front(it1_int-it2_int-one_less+'0');
78      one_less = 0;
79    }
80  }
81  if(one_less==1){
82    sub=temp2-temp1 ;
83    sub.m_digits.emplace_front('-');
84  }
85  while(sub.m_digits.front()=='0'&&sub.m_digits.size()!= 1){
86    sub.m_digits.pop_front();
87    if(sub.m_digits.size()== 1)
88      break;
89  }
90  return sub.m_digits;
91}