【发布时间】:2021-12-20 00:43:10
【问题描述】:
对于我的作业,我必须创建一个数组计算器,但我的老师要求我将其显示/输出为答案:2 + 1 + 3 = 6(以随机数为例)我想知道我需要输入什么变量代替“2”“1”和“3”点?为了让它像示例一样显示。也许我需要为每个变量创建一个变量?如果是这样,请给我一个例子。我将复制并粘贴我目前拥有的东西,顺便说一下我正在使用视觉工作室。
// Preprocessor Directives
#include <iostream>
#include <iomanip>
using namespace std;
/*
* Name: main()
* Parameters: None.
* Processes: Replace this text; see the instructions in the Template.
* Return Value: An integer representing an error code; if the program ends without error, zero
* will be returned to the calling program or operating system.
*/
int main()
{
// Constants and Variables
const int SIZE = 3;
double number[SIZE] = { 0.0, 0.0, 0.0 }, sum = 0.0, difference = 0.0, product = 0.0, quotient = 0.0,
average = 0.0, largest = 0.0, num = 0.0;
int i;
bool numsGotten = false;
char choice = '\0';
// Output - Format
cout << setprecision(3) << fixed;
// Process - Calculator Menu
do
{
cout << "\nCalculator Menu\n\n"
<< "(G)et three numbers.\n"
<< "(A)dd.\n"
<< "(S)ubtract.\n"
<< "(M)ultiply.\n"
<< "(D)ivide.\n"
<< "a(V)erage.\n"
<< "(L)argest.\n"
<< "(P)ositive, negative, or zero.\n"
<< "e(X)it.\n\n"
<< "Choice: ";
cin >> choice;
switch (choice)
{
// New Numbers
case 'G':
case 'g':
// Input - Numbers
cout << "\nEnter numbers: ";
for (int i = 0; i < SIZE; i++)
{
cin >> number[i];
//Validation
numsGotten = true;
}
break;
// Addition
case 'A':
case 'a':
// Validation
if (numsGotten)
{
for (i = 0; i < SIZE; i++)
{
sum += number[i];
}
// Output - Answer
cout << "\nAnswer: " << << " + " << i++ << " + " << i++ << " = " << sum << ".\n";
}
else
{
// Output - Error
cout << "\n\tError: Please select option G from the menu first!\n";
}
break;
// Exit
case 'X':
case 'x':
// Output - Thank You Message
cout << "\nThank you for using Brianna's calculator! Good-bye!\n";
break;
// Invalid Choice
default:
// Output - Error
cout << "\n\tError: Invalid choice!\n";
}
}
while (choice != 'x' && choice != 'X');
return 0;
}
【问题讨论】:
-
数字在您读入的数组中。这就是您输出的内容。无需创建变量,因为数组是保存您输入的信息的“变量”。
标签: c++ arrays visual-studio output