【问题标题】:Number addition program of up to 100 digits. Odd console output最多 100 位数字的加法程序。奇怪的控制台输出
【发布时间】:2016-08-31 19:45:46
【问题描述】:

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

#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?

我真的很想在计算输出方面得到一些帮助,但任何建议都会很棒:)!

【问题讨论】:

  • 您想从输出中找出什么?请让您的问题更清楚,以便在阅读大量代码之前有人知道您遇到了什么问题。
  • 底部的输出。运行程序有两次尝试,但有 4 行有一个奇数组合,看起来像这样。 03-48-486532 它们应该是标准数字,带十位。
  • 只看这段代码,我就看到了很多需要帮助的格式。一旦你正确地格式化你的代码,它会更容易,解决方案可能会出现。我还建议在使用 if 语句时,添加花括号 {},即使它是单行。在我看来,您可能打算在最后一个函数中使用 else 语句,但您却放了一个分号并将所有内容都放在一行上,这很令人困惑。还有很多地方的间距不一致。
  • 对此感到抱歉。感谢您的指点。我现在就处理它
  • 够好吗?我可以一次移动所有东西吗?我只是在使用空格键。这个数量还不错,但我可能在一两个月内有更多的数量。编辑:我还注意到有一个未使用的变量。现在没了。

标签: c++ arrays digit


【解决方案1】:

一些具体问题:

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
%

【讨论】:

  • 太棒了! TYVM!.我如何让你的答案?我点击了向上箭头,但我对这个网站还是太陌生了。
  • @Joeybreh,寻找箭头和数字下方的灰色复选标记 - 当您单击它时,它应该变为绿色以表示接受。但请留意稍后出现的您可能更喜欢的新答案——您可以随时更改您的选择。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 2018-11-10
  • 1970-01-01
相关资源
最近更新 更多