控制台输出是我主要关注的问题。这是整个计划。我将在最底部发布控制台输出。输出显示我输入程序的内容。您可以立即看到数字变为奇数。他们得到负数或连字符。奇数位于底部附近的“+”符号的上方和下方。我想添加两个输入并正确显示它们。请提供您可以提供的任何帮助。即使是批评我的发布方法,因为我是新手。
#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
}
}
CONSOLE OUTPUT
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?
我真的希望得到一些帮助来确定输出,但任何建议都会很棒:)!
答案 0 :(得分: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
%