【问题标题】:C++ array calculatorC++ 数组计算器
【发布时间】: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


【解决方案1】:

对于这个问题,我写了一个小例子,方便大家理解。在这个示例中,我创建了一个包含 3 个数字的数组,其中 a[0] 的值为 1,a[1] 的值为 2,a[2] 的值为 3。您可以使用此方法调用数组中的数字。

int main()
{
    int a[3] = { 1,2,3 };
    for (int i = 0; i < 3; i++)
    {
        cout << a[i] << " ";
    }
    return 0;
}

【讨论】:

  • @helpmehpls 感谢您对帮助感到满意。如果您的案例已经解决,请帮忙标记答案。
猜你喜欢
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 2017-03-29
相关资源
最近更新 更多