The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 398 Weeks Ago, 5 Days Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
#ifndef BIGINTEGER_H
#define BIGINTEGER_H
#include <iostream>
using namespace::std;
const int NUMDIGITS = 50;
// A non-negative integer with NUMDIGITS
// Leading zeores are stored in the number
// if + or * result in overflow, i.e. and answer with more that NUMDIGIST, the overflow flag is set
class BigInteger
{
  friend ostream & operator <<( ostream &, const BigInteger &);
public:
  BigInteger(); // return NULL
  BigInteger(const BigInteger &); // copy constructor
  BigInteger( int );
  BigInteger( const char *);
  ~BigInteger();
  void setToZero();
  bool operator==(const BigInteger &) const;
  bool operator<(const BigInteger &) const;
  bool operator<=(const BigInteger &) const;
  bool operator>(const BigInteger &) const;
  bool operator>=(const BigInteger &) const;
  bool operator!=(const BigInteger &) const;
  const BigInteger & operator=(const BigInteger &) ;
  BigInteger operator+(const BigInteger &) const;
  BigInteger operator*(const BigInteger &) const;
  bool hasOverFlowed(void) const;
  static int getNumberOfDigits(void)
  {
     return NUMDIGITS;
  }
//BigInteger.cpp
#include "BigInteger.h"
ostream & operator <<( ostream & out, const BigInteger & r )
{
int digitsStart = 0;
// STUDENTS
// write a for or while loop to place the subscript of the first high order digit that is not 0 in digitsStart
 Â
for(int i =0;i <NUMDIGITS; i++)
{
  if(r.digits[i] !=0)
  {
     digitsStart= i ;
     break;
  }
   Â
   Â
  }
 Â
//out<< r.digits[i];
if ( digitsStart == NUMDIGITS ) // the number is all 0's
{
  out << '0';
  return out;
}
for(int i = digitsStart; i < NUMDIGITS; i++) // this code prevents leading 0's from being printed
  out << r.digits[i];
return out;
}
BigInteger::BigInteger()
{
  setToZero();
}
BigInteger::BigInteger(const BigInteger & r)
{
  this->overflow = r.overflow;
  for ( int i = 0; i < NUMDIGITS; i++)
     this->digits[i] = r.digits[i];
}
BigInteger::BigInteger( int r)
{
  this->convertIntToBigInteger(r);
}
BigInteger::BigInteger( const char * s)
{
  int len = strlen(s);
  setToZero();
  if ( len > NUMDIGITS )
  {
     overflow = true;
     return;
  }
  overflow = false;
  // STUDENTS
  // write a for or while loop to place the correct number in the digits array from the C++ string
  // work right to left and move the digits one by one WILL NEED TO CONVERT A CHAR DIGIT TO A NUMBER DIGIT
  for(int i=0; i< strlen(s); i++)
  return;
}
BigInteger::~BigInteger()
{
  return;
}
void BigInteger::setToZero()
{
  overflow = false;
  for( int i = 0; i < NUMDIGITS;i++)
     digits[i] = 0;
}
bool BigInteger::operator==(const BigInteger &r) const
{
  for ( int i = 0; i < NUMDIGITS; i++)
     if ( digits[i] != r.digits[i] ) return false;
  return true;
}
bool BigInteger::operator<(const BigInteger & r) const
{
  // STUDENTS
  // Write a for or a while loop to compare digits left to right
  // if a smaller digit is found in the invoking instance return true
  // if a larger digit is found in the invoking instance return false
  //for(int i=0; i<NUMDIGITS; i++)
  for(int i=0; i<NUMDIGITS; i++)
     if(digits[i] < r.digits[i])
        {return true;
  }
     else{
  // for loop has been completed here ==> all digits match, so return false
  return false; // all digits match, both BigIntegers are equal
     }
}
bool BigInteger::operator<=(const BigInteger & r) const
{
  // STUDENTS
  // code needed
  for(int i = 0; i < NUMDIGITS; i++)
     if(digits[i] >= NUMDIGITS) return false;
  return true;
}
bool BigInteger::operator>(const BigInteger & r) const
{
  // STUDENTS
  // code needed
  return true;
}
bool BigInteger::operator>=(const BigInteger & r) const
{
  // STUDENTS
  // code needed
  return !(*this < r);
// return true;
}
bool BigInteger::operator!=(const BigInteger & r) const
{
  return !( *this == r );
}
const BigInteger & BigInteger::operator=(const BigInteger & r)
{
  if ( this == & r ) return *this;
  overflow = r.overflow;
  for ( int i = 0; i < NUMDIGITS; i++)
     digits[i] = r.digits[i];
  return *this;
}
BigInteger BigInteger::operator+(const BigInteger & r) const
{
  BigInteger answer;
  int carry = 0;
  // STUDENTS
  // using a for or a while loop add the digits right to left using the carry
  // review your notes from class
  // loop exit, set the overflow flag
  answer.overflow = ( carry > 0 );
  return answer;
}
BigInteger BigInteger::operator*(const BigInteger & r) const
{
  BigInteger i(0), total(0), one(1);
  // STUDENTS
  // modify the code so that the small BigInteger is always used to control the for loop
  for(int i ; i < *this; i = i + one ) // runs faster if *this < r  Â
  {
     total = total + r;
     if ( total.hasOverFlowed() ) return total; // stop when overflow
  }
  return total;
}
bool BigInteger::hasOverFlowed(void) const
{
  return this->overflow;
}
void BigInteger::convertIntToBigInteger(int r) // place the int parameter into the BigInteger format for the invoking instance
{
this->setToZero();
  if ( r <= 0 ) return;
  int placer = NUMDIGITS - 1; // rightmost digit, work right to left
  int value = r;
  while ( value > 0 && placer >= 0 )
  {
     // Students
     // use % operator to select the right most digit and place in the array
     // change the variable value by removing the right digit using / operator
// move to the left for placing digits in the array
     //if(placer ==0)
     //{
        digits[placer] = value % 10;
        value = value/10;
        placer --;
 Â
     //}
  //else{
     //  digits[placer] = value /10;
     //  value = value/10;
     //  placer --;
  //  }
  }
  return;
}
//DriverBigInteger.cpp
#include <iostream>
using namespace::std;
#include "BigInteger.h"
int main()
{
  {
     BigInteger a(123456);
     cout<<" a is " << a << endl;
     return 0;
  }
  // STUDENTS
  // test the code piece by piece by adding and removing comments
  // REMEMBER that you can use break points and the watch window to help debugging
  // Your final output should match the output at the end of the program
cout << "Number of digits in our numbers is " << BigInteger::getNumberOfDigits() << endl;
// test default constructor and << operator
BigInteger a;
cout << a << endl;
// test int argument constructor and << operator
BigInteger b(123456789), c(999);
// test <<
cout << b << ' ' << c << endl;
// test copy constructor
BigInteger d(b);
cout << "d is " << d << endl;
// test = operator
d = c;
cout << "d is now " << d << endl;
// test == and + operators
BigInteger e(123456788), f(667788), g(667788);
cout << b << ' ' << e << ' ' << " b == e is " << ( b == e ) << endl;
cout << f << ' ' << g << ' ' << " f == g is " << ( f == g ) << endl;
cout << e << " + " << f << " is " << ( e + f ) << endl;
// test *
BigInteger h(20), k(60);
cout << h << " * " << k << " is " << ( h * k ) << endl;
// Calculate powers of 2 until overflow
BigInteger s(1), value(0), stop(20), one(1), two(2),powerOf2(1), save(5);
while ( !powerOf2.hasOverFlowed())
{
  powerOf2 = two * powerOf2; // multiple by 2 each time
  cout << " 2 to the power of " << s << " is " << powerOf2 << endl;
  s = s + one;
}
// test C++ string argument constructor
BigInteger w("12345678901234567890");
cout << "w is " << w << endl;
BigInteger w1,w2,oneMillion("1000000");
w1 = w + w;
w2 = oneMillion * w;
cout << w1 << " " << w2 << endl;
return;
}
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll