在类中使用tostring。C++

在类中使用tostring。C++,c++,data-structures,C++,Data Structures,在类中使用tostring #include <iostream> #include <iomanip> #include <string> #include <cassert> using namespace std; 具有参数int值的类LargeInteger的构造函数 LargeInteger::LargeInteger(int value) { // set this instance id id = nextLarg

在类中使用tostring

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

using namespace std;
具有参数int值的类LargeInteger的构造函数

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;
    }
}
主要功能

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
}

查看构造函数,数据结构中的数字数组似乎是一个十进制数字序列,按相反顺序编码为二进制值0..9

所以1992年将被编码为2,9,9,1

为了使数字可打印,您需要在其中添加“0”。然后,您需要从头迭代,并连接可打印版本。比如:

string LargeInteger::tostring()
{
    string intValue;
    for (int i=numDigits-1; i>=0; i--) 
        intValue +=  digits[i] + '0';
    return intValue;
}
建议1 您可以很好地使用字符串,而不是将数字存储为整数数组,因为字符串可以包含任何二进制数据,包括“\0”。这将避免内存分配的麻烦

如果这样做,还可以使用迭代器和算法编写与以下相同的函数:

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

建议2 请注意,您的构造函数不能使用负数,因为负数的值会引发异常

这可以用一个绝对数来纠正:

 numDigits = (int)log10(abs((double)value)) + 1;
然而,在负数上使用模会给出一个负数。这意味着我们的tostring需要更改以使用每个数字的绝对值,如果任何数字为负数,请在数字的开头添加负号,请参见


一种更方便的方法是在类级别使用一个符号标志来表示数字是整体正还是负,并更改构造函数以确保数字始终为正

您将数字数组存储为整数的倒序。现在在toString中,只需将每个数字按相反顺序转换为字符,并将其推入字符串结果。这是我的解决办法

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

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

整数*位数;你为什么需要这个?数字可以很好地表示为简单的字符值。我相信你处理大整数的方法是有缺陷的。您可能想查看一些已经正确处理了这类内容的库。可能重复的代码是错误的。for应在循环中递减。i++应替换为i-@LocTran ided!谢谢你指出这一点;我在测试时才意识到:-
string LargeInteger::tostring()
{
    string intValue(digits.size(),' ');
    transform (digits.rbegin(), digits.rend(), intValue.begin(), 
                                             [](auto &x){ return x+'0'; }) ; 
    return intValue;
}
 numDigits = (int)log10(abs((double)value)) + 1;
string LargeInteger::tostring()
{
    string intValue;
    //intValue = ??

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