创建一个大整数c ++

时间:2016-02-09 22:37:07

标签: c++ visual-c++

在我的作业中我得到了

BigInteger::BigInteger(int val) {

BigInteger::BigInteger(string str) {

我应该实现两个构造函数来从int值初始化BigInteger对象和存储整数值的字符串。我真正需要的只是朝着正确方向迈出的一步,我只是不知道如何开始这个。

它还提到这对它有用

void BigInteger::setDigit(int pos, char digit)
{
    if (pos >= size)
    { // not enough space
        int newSize = 1;
        while (newSize <= pos)
            newSize *= 2; // compute newSize as a power of 2      that is bigger than pos

        char* temp = digits; // store link to current digits

        digits = new char[newSize]; // allocate a new array
        memset(digits, 0, newSize); // and fill zeros

        if (temp != NULL) // if there are some current digits
            memcpy(digits, temp, nDigits); // copy them
        size = newSize;
    }

    digits[pos] = digit; // put the given digit at position pos

    if (pos >= nDigits) // update the number of digits!
        nDigits = pos + 1;
}

这是类定义

class BigInteger
{
private:
    char* digits; // the array storing digits
    int size; // the current size of digits array
    int nDigits; // the current number of digits

    void init(int size);
    void copy(const BigInteger &num);

public:
    BigInteger(); // Default constructor
    ~BigInteger(); // Default destructor

    BigInteger(int val); // Initialize a BigInteger with an integer value
    BigInteger(string str); // Initialize a BigInteger with a string storing a number

    BigInteger(const BigInteger &num); // Copy constructor
    BigInteger& operator=(const BigInteger &num); // Copy assignment operator

    int numberOfDigits() const
    {
        return nDigits;
    }
    char getDigit(int pos) const; // get the digit at position pos
    void setDigit(int pos, char digit); // set the digit at position pos

    void print(bool debug = true);
};

作业的最终目标是能够计算999! (1 * 2 * 3 * ... 999)

就像我说的那样,正确的方向迈出了一步。

以下是整个代码:

#include"BigInteger.h"

BigInteger::BigInteger()
{
    digits = NULL; // Default constructor: storing nothing!
    size = 0;
    nDigits = 0;
}

BigInteger::~BigInteger()
{
    if (digits != NULL)
        delete[] digits;
    digits = NULL;
    size = 0;
    nDigits = 0;
}

void BigInteger::init(int size)
{
// Task 1. Allocate memory for digits and fill them as zeros
    digits = new char[size];
    for (int i = 0; i < size; i++)
    {
        digits[i] = 0;
    }

}

void BigInteger::copy(const BigInteger &num)
{
    size = num.size; // copy digits array size
    nDigits = num.nDigits; // copy number of digits
    digits = new char[size]; // allocate a new digits array with        the same size in num
    memcpy(digits, num.digits, size); // copy digits array
}

BigInteger::BigInteger(const BigInteger &num)
{ // Copy constructor
    copy(num);
}

BigInteger& BigInteger::operator=(const BigInteger &num)
{
    if (this != &num)
    { // not assign to the same object
        if (digits != NULL)
            delete[] digits; // release current digits array
        copy(num);
    }
    return *this;
}

BigInteger::BigInteger(int val)
{
// Task 2a. Construct a BigInteger from an int value

}

BigInteger::BigInteger(string str)
{
// Task 2b. Construct a BigInteger from a string.

}

char BigInteger::getDigit(int pos) const
{
    if (pos >= nDigits)
        return 0;
    else
        return digits[pos];
}

void BigInteger::setDigit(int pos, char digit)
{
    if (pos >= size)
    { // not enough space
        int newSize = 1;
        while (newSize <= pos)
            newSize *= 2; // compute newSize as a power of 2      that is bigger than pos

        char* temp = digits; // store link to current digits

        digits = new char[newSize]; // allocate a new array
        memset(digits, 0, newSize); // and fill zeros

        if (temp != NULL) // if there are some current digits
            memcpy(digits, temp, nDigits); // copy them
        size = newSize;
    }

    digits[pos] = digit; // put the given digit at position pos

    if (pos >= nDigits) // update the number of digits!
        nDigits = pos + 1;
}

BigInteger multiply(BigInteger &x, int y, int pos = 0)
{
    int nx = x.numberOfDigits();
    BigInteger z;
    int carry = 0;
    for (int i = 0; i < nx; i++)
    {
        carry += x.getDigit(i) * y;
        z.setDigit(pos++, carry % 10);
        carry /= 10;
    }
    while (carry > 0)
    {
        z.setDigit(pos++, carry % 10);
        carry /= 10;
    }
    return z;
}

void BigInteger::print(bool debug)
{
    if (digits == NULL)
        cout << '0';
    else
        for (int i = nDigits; --i >= 0;)
            cout << (int) digits[i];
    if (debug)
        printf(" [digits = %x, size = %d, nDigits = %d] ",
               digits,
               size,
               nDigits);
}

ostream& operator<<(ostream& out, BigInteger num)
{
//Task 3. Overload operattor << to write a BigInteger object to screen
    num.print();
    return out;
}

1 个答案:

答案 0 :(得分:0)

你已经有了一个代码来存储BigInteger中的整数值 multiply功能。保存carry其余部分(最后)的代码将其保存到BigInteger中。以此为例。

对于字符串1,您只需要将字符串中的数字反向放入BigInteger对象。