Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ please. Implement a tostring() memb er function. This function will return a string representation of the LargeInteger . You should probably used string streams

C++ please.

Implement a tostring() memb er function. This function will return a string representation of the LargeInteger . You should probably used string streams to implement this function. Note that the digits of the large integer are stored in reverse of their display order, e.g. the 1's place (10 0 ) is in index 0 of digits, the 10's place (10 1 ) is in index 1, etc.

2. Implement a second constructor for the LargeInteger . This construc- tor will b e used to construct a large integer from an array of digits. This constructor takes an int, which is the size of the array of digits, and an integer array as the second parameter. You will need to dy- namically allo cate the space for the digitis in your constructor (see the given constructor for an example).

3. Implement a memb er function named maxDigits() . This function will take a reference to a LargeInteger as its only parameter. This function should return the numb er of digits in the larger LargeInteger comparing the passed in ob ject to this ob ject.

4. Implement a memb er function named digitAtPlace() . This function returns the digit of this large integer ob ject at a particular digit in- dex. So this function takes an integer index as its input parameter, and returns an integer as its result. Again, if you ask for the digi- tAtPlace() index 0, this should return the 1's place 10 0 digit of the integer. If asked for the digit at place 1, this should return the 10's place 10 1 digit. If you ask for a digit place that is larger than the numb er of digits in the integer the memb er function should return 0. For example, if the integer represents 345, and you ask for the digit at place 3, then the function will return a 0. Likewise, if a negative index is given to this function, it should also return 0.

5. Implement a memb er function named app endDigit() . This functions takes an int digit as its only parameter. This will b e a void function as it do es not return any value result from calling it. The function is not 2 something a normal user of the LargeInteger typ e is likely to need to do to an integer, but it will b e very useful for our add function. This function app ends the digit to b ecome the new most signicant digit of the LargeInteger ob ject. For example, if the large integer ob ject represents the value 345 and we ask it to app endDigit(7) , then the new value of the ob ject will b e 7345. This function needs to manage memory to p erform its task. You need to p erform the following steps 1) allo cate a new array of digits of numDigits+1 (e.g. enough space to hold one more digit). 2) copy the old digits to the new array of digits you created. 3) app end the new passed in digit to the end of the array. Then 4) free up the old original array of digits, and make sure you class now uses the new array of allo cated digits. Also, you might want to have this function ignore the app end of a '0' digit (e.g. do not grow the large integer if trying to app end a 0, as this do es not need to b e represented by the LargeInteger ).

6. And nally, implement the add() memb er function. This function takes a reference to another LargeInteger ob ject, and adds the digits of the other ob ject to this ob ject together.

Lets discuss the algorithm for the add() memb er function in more detail. This function will simply take a reference parameter of typ e LargeInteger as its one and only parameter. You need to p erform the following steps in your add() memb er function:

1. Dynamically allo cate a new array to hold the resulting sum of this and the other LargeInteger . Use the maxDigits() memb er function to determine the size needed for this new array. The size of the resulting sum will either b e equal to the numb er of digits of the larger of the 2 numb ers, or this value + 1 if there is a carry on the most signicat sum. We handle needing to grow the result in the last step of the algorithm. For example if this large ineteger has the value 4537 (4 digits) and the other has the value of 23 (2 digits), the result will t in 4 digits, which is what maxDigits() should return. Only if we have a carry would we need an extra digit. For example if this is 4537 and the other is 7242 the result of adding them together would b e 11779, which needs 5 digits. But as mentioned, we will grow to accomo date a nal carry in the last step of the algorithm.

2. Perform the addition, from least signicant digit to most signicant digit, handling carry as needed. Use the digitAtPlace() memb er 3 function here, as this will determine if each numb er has a digit, or will return a 0 if you are b eyond the size of each numb er. The resulting digits should b e stored in the new array you allo cated in step 1. Also make sure you keep track of the carry, and at the end, you should know if a 0 or 1 was carried from the addition of the last most signicant digits.

3. Dynamically allo cate a new LargeInteger() ob ject, using the con- structor you created that takes an array as its parameter. You will pass in the array of new digits you summed up in step 2 and its size that you determined and dynamically allo cated in step 1.

4. If there was a carry on the last most signicant digit, use the app end- Digit() memb er function to add the carry value as the new most sig- nicant digit to the new LargeInteger() you created in step 3. As mentioned ab ove, you could also just have your app endDigit() func- tion ignore a request to app end a '0' digit, thus in your add() you could always just call app endDigit() with the nal carray, and the app end would app end or ignore as appropriate.

5. Finally the add() function should return a reference to the new LargeInteger() that contains the calculated sum you just computed and assigned to it.

The Program is below:

#ifndef _LARGEINTEGER_H_ #define _LARGEINTEGER_H_

#include #include #include #include

using namespace std;

/** LargeInteger class. * * @param id Private member integer variable, this is not strictly needed * for this class, but we assign a unique id to each instance of * LargeInteger that is created, so that you can more easily see how * the destructor works and is being called. You should set the * id and increment it in any constructors you create for this class. * @param numDigits Private member integer variable, contains the number * of digits currently in the LargeInteger, or equivalently, the size * of they digits array of integers. * @param digits A dynamically allocated array of integers. This array * holds the digits of the large integer this object represents. The * digits in the array are orderd such that the 1's place (10^0) is in * index 0 of the array, the 10's place (10^1) is in the index 1, and so * on. */ class LargeInteger { private: int id; int numDigits; // number of digits in LargeInt / size of alloc array int* digits; // the digits of the LargeInt we are representing

public: LargeInteger(int value); ~LargeInteger(); };

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions