BigInt калькулятор выкладывает немного неправильные результаты - PullRequest
2 голосов
/ 23 сентября 2019

Так что для моего задания я должен создать калькулятор, который работает с большими целыми числами длиной до 256 символов.Текущая часть задания, над которым я работаю, - заставить его работать с умножением больших чисел.DIGITS - это предел цифр для класса Bigint, в настоящее время установленный для отладки, равный 20, но он возрастет до 256

. При выполнении вычислений, подобных 25 * 137, я получаю ответ 3285, когда он должен быть 3425.Когда я просматриваю койки, которые я установил для отладки, первая итерация цикла i работает отлично и добавляет 685 к сумме, которая равна 5 * 137, так что это работает отлично.Однако, когда он доходит до того места, где он должен выполнить вторую итерацию цикла i, где он равен 20 * 137, он получает неправильный ответ, и я не могу понять, почему.У меня есть предположение, что это связано с тем, что перенос состоит из двух цифр (14), но я все еще не могу понять, как я могу это исправить.

Основная реализация, которая явно что-то не так с этимнаходится в операторе * класса bigint.Я знаю, что это не относится к операторам << или >>, так как они отлично работают для сложения и вычитания.

Полный код класса bigint приведен ниже:

#include <iostream>
#include <string>
#include "Bigint.h"
#include <cmath>

using namespace std;

Bigint::Bigint()
{
    for (int i = DIGITS-1; i >= 0; --i) {
        digits_[i] = 0;
    }
}

ostream& operator<< (ostream& out, const Bigint& n)
{

    string s = "";
    bool found = false;
    for (int i = DIGITS - 1; i >= 0; --i) {
        if(n.digits_[i] > 0) {
            found = true;
        }
        if(n.digits_[i] != 0 || found == true) {
            s += char(n.digits_[i] + '0');
        }
    }
    if (s == "") {
        s = "0";
    }
    return out << s;
}

istream& operator>> (istream& in, Bigint& n)
{
    // Extracts full-length number (does not work for any other length).
    // All characters are assumed to be valid digits.
    //
    string s;
    if (in >> s) {
        for (int i = 0; i < DIGITS; ++i) {
            n.digits_[i] = i < s.length() ? s[s.length() - 1 - i] - '0' : 0;
        }
    }
    return in;
}

Bigint operator+ (const Bigint& n1, const Bigint& n2)
{
    Bigint ret;
    int cur_carry = 0;
    for(int i = 0; i < DIGITS; ++i) {
        int n1_digit = n1.get(i);
        int n2_digit = n2.get(i);
        if(n1_digit < 0 || n1_digit > 9) {
            n1_digit = 0;
        }
        if(n2_digit < 0 || n2_digit > 9) {
            n2_digit = 0;
        }
        //printf("n1 : %d\n", n1_digit);
        //printf("n2 : %d\n", n2_digit);
        int sum = n1_digit + n2_digit + cur_carry;
        //cout << "sum : " << sum << endl;
        cur_carry = Bigint::getCarry(sum);
        //cout << "new carry : " << cur_carry << endl;
        ret.set(i, Bigint::getDigitValue(sum));
        //cout << "Set : " << i << "," << Bigint::getDigitValue(sum) << endl;
    }
    return ret;
}

Bigint operator* (const Bigint& n1, const Bigint& n2)
{
    Bigint ret;
    //int borrowed = 0;
    Bigint sum;
    for(int i = 0; i < DIGITS ; i++){
        int n1_digit = n1.get(i);
        //cout << "n2: " << n2_digit << endl;
        Bigint temp;

        if(n1_digit < 0 || n1_digit > 9) {
            n1_digit = 0;
        }

        int carry = 0;

        for (int j = 0; j < DIGITS ; j++){
            int val = n1_digit * (pow(10, i)) * n2.get(j);
            cout << "n1: " << n1_digit << endl;
            cout << "n2: " << n2.get(j) << endl;

            if(carry != 0){
                temp.set(j, (Bigint::getDigitValue(val)) + carry);
                cout << "Carry was " << carry << ", now set 0" << endl;
                cout << "value to set: " << (Bigint::getDigitValue(val)) + carry << endl;
                carry = 0;
            }
            else if(carry == 0){
                temp.set(j, Bigint::getDigitValue(val));
                cout << "value to set: " << (Bigint::getDigitValue(val))<< endl;
            }

            carry = (Bigint::getCarry(val) + carry);
            cout << "carry: " << carry << endl;
        }
        cout << "Sum before adding temp: " << sum << endl;
        sum = sum + temp;
        cout << "Sum after adding temp: " << sum << endl;


    }

    ret = sum;
    return ret; // Only correct when n2 equals 1.
}

int Bigint::get(int pos) const {
    //Return address of digit for reading
    int ret = digits_[pos];
    return ret;
}

void Bigint::set(int pos, int val) {
    this->digits_[pos] = val;
}

int Bigint::getCarry(int val) {
    //Integer division, always floors
    return val/10;
}

int Bigint::getDigitValue(int val) {
    return val % 10;
}


Заголовочный файл:


#ifndef BIGINT_H_
#define BIGINT_H_

#define DIGITS 20

class Bigint
{
  public:

    /**
     * Creates a Bigint initialised to 0.
     */
    Bigint();

    /**
     * Inserts n into stream or extracts n from stream.
     */
    friend std::ostream& operator<< (std::ostream &out, const Bigint& n);
    friend std::istream& operator>> (std::istream &in, Bigint& n);

    /**
     * Returns the sum, difference, product, or quotient of n1 and n2.
     */
    friend Bigint operator* (const Bigint& n1, const Bigint& n2);
    friend Bigint operator+ (const Bigint& n1, const Bigint& n2);

    int get(int pos) const;
    void set(int pos, int val);

    static int getCarry(int val);
    static int getDigitValue(int val);

  private:
    int digits_[DIGITS];
};

#endif // BIGINT_H_

Основной:


#include <iostream>
#include "Bigint.h"

using namespace std;

int main(int argc, char *argv[]) 
{
    Bigint n1, n2;
    char op;

    while (cin >> n1 >> op >> n2) {
        switch (op) {
        case '+' :
            cout << n1 + n2 << endl;
            break;
        case '*' :
            cout << n1 * n2 << endl;
            break;
        }
    }



    return 0;
}
}

Ответы [ 2 ]

0 голосов
/ 23 сентября 2019

вам не следует использовать эту строку int val = n1_digit * (pow(10, i)) * n2.get(j); , потому что она даст целочисленное переполнение, поскольку вы работаете с bigintger, вместо этого используйте цифры в множителе и добавьте нули за результатом.

количество добавляемых нулейбудет зависеть от позиции цифры множителя, которую вы можете найти в переменной i из этого цикла for(int i = 0; i < DIGITS ; i++) в перегруженной * функции

0 голосов
/ 23 сентября 2019

Есть несколько потенциальных проблем

        for (int j = 0; j < DIGITS ; j++){
            int val = n1_digit * (pow(10, i)) * n2.get(j); // val % 10 == 0 for i > 0
            // You should also be adding the carry to val
            cout << "n1: " << n1_digit << endl;
            cout << "n2: " << n2.get(j) << endl;

            if(carry != 0){
                temp.set(j, (Bigint::getDigitValue(val)) + carry);
                // This can set temp[j] to values above 9 depending on the carry
                cout << "Carry was " << carry << ", now set 0" << endl;
                cout << "value to set: " << (Bigint::getDigitValue(val)) + carry << endl;
                carry = 0;
            }
            else if(carry == 0){
                temp.set(j, Bigint::getDigitValue(val));
                cout << "value to set: " << (Bigint::getDigitValue(val))<< endl;
            }

            carry = (Bigint::getCarry(val) + carry);
            cout << "carry: " << carry << endl;
        }

Поскольку вы умножаете на степень 10, getDigitValue и getCarry действуют не так, как вы ожидаете.Возможно, было бы лучше сместить индекс для temp.set на i вместо умножения на pow (10, i).

Я бы также порекомендовал очистить случаи.И if, и else if на самом деле выполняют одну и ту же работу в этом случае, и сброс переноса ничего не делает.Таким образом, это будет иметь точно такое же поведение:

        for (int j = 0; j < DIGITS ; j++){
            int val = n1_digit * (pow(10, i)) * n2.get(j);

            temp.set(j, (Bigint::getDigitValue(val)) + carry);
            // If the carry is 0, the addition doesn't do anything
            // You don't need to reset the carry to 0, since it's assigned here anyway
            carry = (Bigint::getCarry(val) + carry);
        }

Без случаев и без операторов print гораздо проще читать, но здесь использование методов (getCarry и getDigitValue) все еще затрудняет просмотрпроблема путем перемещения соответствующих операций в нижней части класса.Внесение изменений дает:

        for (int j = 0; j < DIGITS ; j++){
            int val = n1_digit * n2.get(j) + carry;

            temp.set(i + j, Bigint::getDigitValue(val));
            carry = Bigint::getCarry(val);
            // The carry just gets added to the value at the beginning
            // Everything else just works that way
        }

Вам также нужно изменить функции get и set, чтобы они не выходили за пределы:

int Bigint::get(int pos) const {
    //Return address of digit for reading
    if (pos >= DIGITS)
        return 0;
    int ret = digits_[pos];
    return ret;
}

void Bigint::set(int pos, int val) {
    if (pos >= DIGITS)
        return ;
    this->digits_[pos] = val;
}

С этим кодом я получаю3425 за 25 * 137.

...