Data structure and algorithm

profileAsutosh
LargeInteger.cpp

/** @file LargeInteger.cpp * @brief Implementation file for Assignment 03 practice with classes and * dynamic memory allocation. * * @author Jane Programmer * @note cwid : 123 45 678 * @note class: COSC 2336, Summer 2020 * @note ide : Atom Text Editor 1.46.0 / build package / GNU gcc tools * @note assg : Assignment 03 * @date June 1, 2020 * * LargeInteger class. This is the class implementation file. It contains the * implementation of all of the member functions declared in the header file. * Since implementation is separated from the declaration of each member * function, you must indicate each function is a member of the LargeIneger * class by prepending member function name with LargeInteger:: */ #include "LargeInteger.hpp" using namespace std; // integer to create unique id for new LargeInteger instances // please set and use this in the same way in the constructor you // create for this assignment static int nextLargeIntegerId = 1; /** LargeInteger constructor * Default constructor for LargeInteger class. By default we construct * with a value of 0 being represented. */ LargeInteger::LargeInteger() { // set this instance id id = nextLargeIntegerId++; // only a single digit numDigits = 1; // allocate an array of the right size digits = new int[numDigits]; // initialize the digit to 0 digits[0] = 0; } /** LargeInteger constructor * Constructor for LargeInteger class that takes a simple built-in * integer to be used as the initial value of the large integer. * * @param value A regular (32-bit) integer that we will use as the * initial value of this LargeInteger data type. */ LargeInteger::LargeInteger(int value) { // set this instance id id = nextLargeIntegerId++; // first determine number of digits, so we know what size of array // to construct dynamically // https://stackoverflow.com/questions/1489830/efficient-way-to-determine-number-of-digits-in-an-integer numDigits = (int) log10((double) value) + 1; // allocate an array of the right size digits = new int[numDigits]; // iterate through the digits in value, putting them into our // array of digits. int digit; for (int digitIndex = 0; digitIndex < numDigits; digitIndex++) { // least significant digit digit = value % 10; digits[digitIndex] = digit; // integer division to chop off least significant digit value = value / 10; } } /** LargeInteger constructor * Constructor for LargeInteger class that takes an array of digits * and initializes this large integers digits to the given values. * * @param numDigits The number of digits being passed in to be * initialized. * @param digits An array of integers representing digits of a * large integer. */ // Your implementation of the constructor from an array of int digits // should go here /** LargeInteger destructor * Destructor for the LargeInteger class. Make sure we are good * managers of memory by freeing up our digits when this object * goes out of scope. */ LargeInteger::~LargeInteger() { // uncomment following output statement to debug/follow destruction of // LargeInteger instances //cout << "<LargeInteger::~LargeInteger> destructor entered, freeing my digits" << endl // << " id = " << id << endl // << " value=" << tostring() << endl; delete [] this->digits; } /** LargeInteger tostring * Represent this large integer as a string value * * @returns string The large integer as a string */ // your implementation of the tostring() member function should go here /** LargeInteger max digits * Return the larger of the number of digits (numDigits) between * this object and the other LargeInteger object. This might * not be so useful to users of this data type, but the first * step in addition and subtraction is to determine the size * we need for the new result array, which will either be the * larger numDigits of the two objects being added, or that value * plus 1 if there is cary from the addition. * * @param other Another LargeInteger object that we are to compare * this object numDigits to * * @returns int The larger (max) of the numDigits of the two * referenced objects. */ // your implementation of the maxDigits() member function should go here /** LargeInteger digit at index * Given an index, return the digit at the given index of * this LargeInteger. The digitIndex passed in refers to * the place or power of the digit needed. For example * 0 means we want the 10^0 or the 1's place, 1 means we * need the 10^1 or the 10s place digit, etc. If the * requested digit index is bigger than the number of places * in the LargeInteger then 0 is returned. For example, if this * LargeInteger represents the number 123, and we ask for the * 4th place (10^4 or the 1000s place), this function will * return 0. * * @param digitIndex The index, interpreted as the place or power, * of the specific digit to be accessed and returned. * * @returns int The digit in the 10^digitIndex of this * LargeInteger object. */ // your implementation of the digitAtPlace() member function should go here /** LargeInteger append digit * Append the indicated digit to the most significant place of this * digit. This function is not so useful to users of LargeInteger. * However, for arithemetic operations, if there is carry over from * the last operations (like carry from adding the most significant * place of two integers), then the LargeInteger has to grow in size. * * @param digit The digit to append to the most significant place * of this object. */ // your implementation of the appendDigit() member function should go here /** LargeInteger add * Add this LargeInteger to the other LargeInteger passed in as * a parameter. * * @param other Another LargeInteger data type. * * @returns LargeInteger& Returns a reference to a newly created * LargeInteger which contains the value of the this added * with other. */ // your implementation of the add() member function should go here