Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Venix Cador
- //Cs23001
- //02/10/2013
- #include"bigint.h"
- bigint::bigint()
- {
- zero();
- }
- void bigint::zero()
- {
- for(int i=0; i <MaxValue; i++)// to search each value in the integer to zero
- {
- big[i]= 0;
- }
- size = 0;
- }
- bigint::bigint(int d)
- {
- zero();
- for (int i = 0; d != 0; i++, ++size) // To set the values in the integer array
- {
- big[i] = d % 10;
- d /=10;
- }
- }
- bigint::bigint(const char d[]) //declaring the character array here.
- {
- zero();
- int y = 0;
- do //to initialize each character array value y to the integer value
- {
- ++y;
- }while (d[y] != '\0');
- --y;
- for (int i = 0; d[i] != '\0'; i++, y--, ++size) //setting each array value = to the integer array value.
- {
- big[y] = (int(d[i]) - int('0')); //Casting the big(bigint array) array to an integer value when assigning it to the character array.
- }
- }
- temp = ((big[i] * x) + carry) % 10;
- carry = ((big[i] * x) + carry) / 10;
- big[i]= temp;
- }
- bool bigint::operator ==(const bigint& y) // using y to compare two bigints.
- {
- for(int i = 0; i < MaxValue; i++)
- {
- if(y.big[i] != big[i])
- {
- return false;
- }
- }
- return true;
- }
- bool bigint::operator ==(int d)
- {
- for (int i = 0; d != 0; ++i)
- {
- if (big[i] != d % 10)
- {
- return false;
- }
- d /= 10;
- }
- return true;
- }
- bool bigint::operator ==(const char d[])
- {
- int y = 0;
- do
- {
- ++y;
- }while (d[y] != '\0');
- --y;
- for (int i = 0; d[i] != '\0'; i++, y--)
- if (big[y] != (int(d[i]) - int('0'))) return false;
- return true;
- }
- void bigint::output(std::ostream& out) const
- {
- int a = MaxValue;
- int outi = 0;
- do
- {
- --a;
- }while (big[a] == 0);
- do
- {
- if(a & 80)
- {
- out << big[a];
- }
- else
- out << std::endl << big[a];
- --a;
- ++outi;
- }while (a >= 0);
- }
- bigint bigint::operator +(bigint rhs)const
- {
- int i = 0;
- int remainder = 0;
- int temp = 0; //Need temp to hold the remainder values.
- while (i < MaxValue)
- {
- remainder = rhs.big[i] + big[i] +remainder;
- temp = remainder % 10;
- remainder /= 10;
- rhs.big[i] = temp;
- ++i;
- }
- return rhs;
- }
- int bigint::operator [](int i)
- {
- if ((i < 0)||(i >= size))
- return 0;
- return big[i];
- }
- int bigint::operator [](int i) const
- {
- if((i < 0) || (i >= size))
- return 0;
- return big[i];
- }
- std::istream& operator >>(std::istream& iput, bigint& rhs)
- {
- int tempi = 0;
- char intry;
- char temp[MaxValue];
- iput >> intry;
- while(intry != ';' && !iput.eof())//.eof means end o file
- // to set the char array temp to the user's input
- {
- temp[tempi] = intry;
- iput >> intry;
- ++tempi;
- }
- temp[tempi] = 0; //To set the final value of the temp array to 0.
- rhs = bigint(temp);
- return iput;
- }
- std::ostream& operator <<(std::ostream& out, const bigint& rhs) //calling rhs from std::istream&.
- //This time it will display the input.
- {
- rhs.output(out);
- return out;
- }
- //Multiply bigint by powers of 10
- void bigint::times10(const int x){ //Shift all elements in the array x number of spaces
- for(int i = ( MaxValue - 1); i >= 0; i--){
- big[i]= big[i - x];
- }
- for(int i = (x -1); i >= 0; --i){ //when previous loop ends, fill remaining x elements with 0.
- big[i] = 0;
- }
- }
- void bigint::times_single_digit(int x){ //multiply a bigint and a single digit
- int temp = 0;
- int carry = 0;
- for(int i = 0; i < MaxValue; i++){
- temp = ((big[i] * x) + carry) % 10;
- carry = ((big[i] * x) + carry) / 10;
- big[i]= temp;
- }
- }
- bigint bigint::operator*(const bigint& rhs)
- {
- bigint part, result;
- for( int i = 0; i <MaxValue; i++){
- part = * this; //copy the value of the left handside to part
- part.times_single_digit(rhs.big[i]); //Multiply left handside by the spot in rhs
- part.times10(i);// shift the left by i
- result = result + part;
- }
- return result;
- }
- bigint bigint::factorial() const{
- bigint result(1);
- if (bigint(0) == *this) {
- return result;
- }
- for (bigint i = 1; i != *this + 1; i = i + 1){
- result = result * i;
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment