Question
1) You need to create a new class called BigInt so you can handle numbers greater that 2^31-1 (which is the largest positive integer.). BigInt
1) You need to create a new class called BigInt so you can handle numbers greater that 2^31-1 (which is the largest positive integer.). BigInt needs to handle addition and subtraction, plus constructor, copy constructor, assignment operator, destructor, and other functions as needed. You will use the STL vector to store the digits or characters of your BigInt.
2) You need to modify the GoldRabbits function to be more efficient. You should keep track of the each result you acquire so you will not have to recalculate it later. You should use an STL map structure for this. Also, you should declare your map structure as static so that it retains its values after the function exits.
3)You also need to learn how to do Exception Handling. If you dont have access to BigInt, your world is limited to the largest integer value which is 2^31 1. If you were to calculate the factorial of 13, you would have integer overflow very bad for the Arian 5. Heres a reference on how to protect from integer overflow:
http//:www.cplusplus.com/articles/DE18T05o/ Your job is to read and understand this article and then build a function called fact(int) that raise an exception when integer overflow is detected.
4) On demo day, you will print out the results of GoldRabbits(0) through GoldRabbits(1000), and you will have the function fact(int) that raises an exception on overflow. It should take about 10 seconds depending on your computer speed. Your main function should be simple, something like this:
int main()
{
for (BigInt n = 0; n <= 1000; n++)
cout << " The GoldRabbit of ("<
getchar(); //pause
for(int I = 0; i<100; i++){
try{
cout < } Catch(){ cout < } } getchar(); } Source Code for Program #5 BigInt and Exceptions #include #include #include #include using namespace std; class BigInt { private: vector // I needed these next 2 functions but you may or may not need them depending how you implement your class void borrow(BigInt &, int); // I used this in the subtraction function void stripLeadingZero(BigInt &); // you need to make sure you don't have a number like this: '0123"; public: BigInt(); BigInt(int); BigInt(string); BigInt operator++(int); bool operator<=(BigInt); bool operator==(int); bool operator==(BigInt); BigInt operator-(BigInt); BigInt operator-(int); BigInt operator+(BigInt); bool operator<( const BigInt &) const; // this is required for the map friend ostream & operator<<(ostream &, const BigInt &); }; // you must modify this function so that it runs in a reasonable time for input of 1000 // you must use a map BigInt GoldRabbits(BigInt bigN) { static map fiboMap[BigInt(0)] = BigInt(1); fiboMap[BigInt(1)] = BigInt(1); if (bigN == 0 || bigN == 1) return BigInt(1); else return GoldRabbits(n 1) + GoldRabbits(n 2); } // you must modify this function so it throws an exception if the result overflows int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } // here is the main function DO NOT CHANGE ANYTHING in this program. int main() { BigInt bigX(28675), bigY(46368), bigResult; bigResult = bigX + bigY; cout << bigX << "+" << bigY << "=" << bigResult; getchar(); // pause for (BigInt n = 0; n <= 1000; n++) { cout << (n<950?" ":" ")<<"The GoldRabbit of ("< if (n == 30) // pause at 30 getchar(); } getchar(); // pause after the GoldRabbits for (int i=0; i<20; i++) { try { cout << "Fact("< } catch(...) { cout << "Fact("< } } getchar(); } BigInt::BigInt() { storage.push_back(0); } // Freebie void BigInt::stripLeadingZero(BigInt & bigN) { // implement this} BigInt::BigInt(int n) { // implement this } BigInt::BigInt(string bigS) { // implement this } BigInt BigInt::operator++(int dumy) { // implement this } bool BigInt::operator<(const BigInt & bigN) const { // implement this } bool BigInt::operator<=(BigInt bigN) { // implement this } bool BigInt::operator==(BigInt bigN) { // implement this } void BigInt::borrow(BigInt &bigN, int n) { // implement this } BigInt BigInt::operator-(int n) { return *this - BigInt(n); } // Freebie BigInt BigInt::operator-(BigInt bigN) { // implement this } { // implement this } BigInt BigInt::operator+(BigInt bigN) { // implement this } { // implement this } ostream & operator<<(ostream & out, const BigInt & bigN) { // implement this }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started