简体   繁体   中英

using tostring within a class. C++

using tostring within a class.

#include <iostream>
#include <iomanip>
#include <string>
#include <cassert>

using namespace std;

Here is my class LargeInteger. Everything is correct in here Im quite sure.

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();
    string tostring();
};

Constructor for class LargeInteger with a parameter int value.

LargeInteger::LargeInteger(int value)
{
    // set this instance id
    id = nextLargeIntegerId++;

    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 of least significant digit
        value = value / 10;
    }
}

Destructor

LargeInteger::~LargeInteger()
{
    cout << " destructor entered, freeing my digits" << endl
         << " id = " << id << endl
         //<< " value=" << tostring() // uncomment this after you implement tostring()
         << endl;
    delete[] this->digits;
}

This is where I'm confused. What do I put here? I tried this but I dont know what to set intValue to in order to get the value I want.

string LargeInteger::tostring()
{
    string intValue;
    //intValue = ??
    return intValue;
}

Main function

int main(int argc, char** argv)
{
    // test constructors, destructors and tostring()
    cout << "Testing Constructors, tostring() and destructor:" << endl;
    cout << "-------------------------------------------------------------" << endl;
    LargeInteger li1(3483);
    cout << "li1 = " << li1.tostring() << endl;

    return 0
}

Looking at the constructor, it appears, that the array of digits in your data structure is a sequence of decimal digits encoded as binary (value 0..9) in the reverse order.

So 1992 would be encoded as 2,9,9,1.

In order to make a digit printable, you need to add '0' to to it. You then need to iterate from end to begin and concatenate the printable version. Something like:

string LargeInteger::tostring()
{
    string intValue;
    for (int i=numDigits-1; i>=0; i--) 
        intValue +=  digits[i] + '0';
    return intValue;
}

Online demo

Suggestion 1

Instead of storing the digits as an array of integers, you can very well use a string, since a string can contain any binary data including '\\0'. This would avoid the memory allocation hassle.

If you go that way, you can also use iterators and algorithms and write your the same function as:

string LargeInteger::tostring()
{
    string intValue(digits.size(),' ');
    transform (digits.rbegin(), digits.rend(), intValue.begin(), 
                                             [](auto &x){ return x+'0'; }) ; 
    return intValue;
}

Online demo

Suggestion 2

Note that your constructor doesn't work with a negative number, since a log10() of a negative raises an exception.

This can be corrected with an absolute number:

 numDigits = (int)log10(abs((double)value)) + 1;

However using modulo on a negative number gives a negative digit. This means that our tostring() would need to get changed to use the absolute value of each digit, and if any digit is negative, adding a negative sign at the beginning of the number (see online demo here ).

A more convenient approach would be to have a sign flag at the class level (to say if the number is overall positive or negative), and change the constructor so to ensure that the digits are always positive.

You stored the digits array as reversed order of a integer number. Now in toString, just convert each digit to character in the reversed order and push it into a string result. Here is my solution

string LargeInteger::tostring()
{
    string intValue;
    //intValue = ??

    for (int index = numDigits-1; index >= 0; index--)
    {
        intValue.push_back(digits[index]+'0');
    }
    return intValue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM