最多100位数字的加号程序。奇怪的控制台输出

Number addition program of up to 100 digits. Odd console output

控制台输出是我最关心的问题。这是整个程序。我将 post 控制台输出放在最底部。输出显示我输入到程序中的内容。您可以立即看到数字变成奇数。他们得到负数或连字符。奇数位于靠近底部的“+”号上方和下方。我想添加两个输入并让它们正确显示。请提供力所能及的帮助。即使是批评我的 posting 方法,因为我是新手。

#include <iostream>
#include <string>
using namespace std;

void readBig(int[]);
void printBig(int[]);
void addBig(int[], int[], int[]);

const int MAX_DIGITS = 100;

int main()
{
    int number1[MAX_DIGITS]={}, number2[MAX_DIGITS]={}, sum[MAX_DIGITS]={};
    bool finished = false;
    char response;
    while (! finished)
    {
        cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
        readBig(number1);
        cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
        readBig(number2);
        addBig(number1, number2, sum);
        printBig(number1);
        cout << "\n+\n";
        printBig(number2);
        cout << "\n=\n";
        printBig(sum);
        cout << "\n";
        cout << "test again?";
        cin >> response;
        cin.ignore(900,'\n');
        finished = toupper(response) != 'Y';
    }
    return 0;
}

void readBig(int number[MAX_DIGITS]) // This function below will read an input number //as a string then input that into an array.
{
    string read;
    cin >> read;

    for (int i = 0; i < MAX_DIGITS && i <= read.length() + 1; i++) {

        number[i] = int (read[i] - '0');
    }
}

// This function below will display the number.
void printBig(int number[MAX_DIGITS])
{
    int digit = 0; // first val counter

    for (int i=0; i<=MAX_DIGITS; i++) 

    if (number[i] == 0 && digit >= 1) cout << ""; // clears leading zeros 
    // and checks for placeholder zeros

    else cout << number[i]; digit++; // else print val and check 
    // for beginning of number.
}

void addBig(int number1[MAX_DIGITS], int number2[MAX_DIGITS], int sum[MAX_DIGITS])
    // The code below sums the arrays.
{
    for (int i = 0; i < MAX_DIGITS; i++) {
        sum[i] = number1[i] + number2[i];
    }
    for (int j = 0; j < MAX_DIGITS; j++){
        if (sum[j] / 10 > 0) { sum[j + 1] += 1; }
        if (sum[j] / 10 > 0) { sum[j] = sum[j] / 10; } // EDIT:I would like both to happen
    }
}

控制台输出

    Please enter a number up to 100 digits: 987654321
    Please enter a number up to 100 digits: 987654321
    987654321-48-483
+
    987654321-48-489
=
    11111-9-99
    test again?

    Please enter a number up to 100 digits: 444444444
    Please enter a number up to 100 digits: 444666644
    444444444-48-483
+
    444666644-48-484
= 
    01111-9-94
    test again?

我真的很想得到一些帮助来计算输出,但是任何建议都很好 :)!

一些具体问题:

1) 此代码:

else  cout << number[i]; digit++;

你的意思是:

else
{
    cout << number[i];
    digit++;
}

你写的是:

else
{
    cout << number[i];
}
digit++;

如果您是 C/C++ 的新手,总是 使用花括号,主要是为了避免此类问题,同时也让您的代码更清晰其他

2) 你的表述是反的。通过将高位数字放在数字数组中的第一位,您就没有为最高位进位留出空间。但是,如果反过来,将最低位放在前面,则可以将数组中未使用的数字进位。

3) 此声明:

int number1[MAX_DIGITS]={}, number2[MAX_DIGITS]={}, sum[MAX_DIGITS]={};

需要将所有数字初始化为零,您的算法(不跟踪数字长度)才能正常工作。所以它需要在循环中,这样第二次添加就不会继承第一次的数字。

我已经通过上述更改和许多样式更改重新编写了您的代码:

#include <iostream>
#include <string>
using namespace std;

void readBig(int[]);
void printBig(int[]);
void addBig(int[], int[], int[]);

const int MAX_DIGITS = 100;

int main()
{
    bool finished = false;     

    while (! finished)
    {
        int number1[MAX_DIGITS]={0}, number2[MAX_DIGITS]={0}, sum[MAX_DIGITS]={0};
        char response;

        cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
        readBig(number1);
        cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
        readBig(number2);
        addBig(number1, number2, sum);
        printBig(number1);
        cout << "\n+\n";
        printBig(number2);
        cout << "\n=\n";
        printBig(sum);
        cout << "\n";
        cout << "test again? ";
        cin >> response;
        cin.ignore(900, '\n');
        finished = toupper(response) != 'Y';
    }

    return 0;
}

void readBig(int number[MAX_DIGITS])
{
    // This function below will read an input number as a string then input that into an array.

    string read;
    cin >> read; 

    int length = read.length(); 

    for (int i = length - 1, j = 0; i >= 0 && j < MAX_DIGITS - 1; i--)
    {
        number[i] = read[i] - '0';
    }
}

void printBig(int number[MAX_DIGITS])
{
    // This function below will display the number.

    bool first_digit = false; // first value flag

    for (int i = MAX_DIGITS - 1; i >= 0; i--)
    {
        if (number[i] != 0 || first_digit)
        {
            // clears leading zeros and checks for placeholder zeros
            cout << number[i];
            first_digit = true; // else print val and check for beginning of number.
        }
    }
}

void addBig(int number1[MAX_DIGITS], int number2[MAX_DIGITS], int sum[MAX_DIGITS])
{
    // The code below sums the arrays.

    for (int i = MAX_DIGITS - 1; i >= 0; i--)
    {
        sum[i] = number1[i] + number2[i];

        if (sum[i] > 9 && i < MAX_DIGITS - 1)
        { 
            sum[i + 1] += 1;
            sum[i] -= 10;
        }
    }
}

输出

% ./a.out
Please enter a number up to 100 digits: 444444444
Please enter a number up to 100 digits: 444666644
444444444
+
446666444
=
891110888
test again? y
Please enter a number up to 100 digits: 9999
Please enter a number up to 100 digits: 9999
9999
+
9999
=
19998
test again? n
%