首页 > 解决方案 > 循环计算器

问题描述

该程序将从用户那里获取浮点数和运算符,并执行所需的计算,并打印出结果。每次计算的结果将作为下一次计算的操作数。

所以,我不确定我是否通过使用 switch 语句正确地做到了这一点?有没有更好的方法呢?也许,通过使用 do-while 循环?我真的很困惑。

#include <iostream>
#include <iomanip>

using namespace std; 

int main()
{
float firstOperand;
cout << "Enter a number: ";
cin >> firstOperand;
cout << endl;

cout << "Choose an instruction code: " << endl << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;

int choice;
cin >> choice;
cout << endl;

float secondOperand;
cout << "Enter the second number: ";
cin >> secondOperand;
cout << endl;



switch (choice)
{
case 1:


    {
        float resultOne = firstOperand + secondOperand;
        cout << "The result of the calculation is " << resultOne << endl;

    }


case 2:
{

    float thirdOperand;
    cout << "Enter another number ";
    cin >> thirdOperand;
    cout << endl;

    cout << "Choose an instruction code: " << endl << endl;
    cout << "1) (+) for addition. " << endl;
    cout << "2) (*) for multiplication. " << endl;
    cout << "3) (p) for power. " << endl;
    cout << "4) (c) to clear the current result. " << endl;
    cout << "5) (-) for subtraction. " << endl;
    cout << "6) (/) for divison. " << endl;
    cout << "7) (s) for square root. " << endl;
    cout << "8) (q) to quit the program. " << endl << endl;

    float resultTwo = resultOne + thirdOperand;
    cout << "The result of the calculation is " << resultTwo << endl;
}



    break;

}


system("pause");
return 0;
}

标签: c++

解决方案


试试这个代码:

#include <iostream>
#include <math.h>

using namespace std; 

int main(){
    float firstOperand, secondOperand, result;
    int choice, quit = 0;

    cout << "Enter a number: ";
    cin >> firstOperand;
    cout << endl;

    while (1){
        cout << "The first operand is " << firstOperand << endl;

        cout << "\nChoose an instruction code: " << endl;
        cout << "1) (+) for addition. " << endl;
        cout << "2) (*) for multiplication. " << endl;
        cout << "3) (p) for power. " << endl;
        cout << "4) (c) to clear the current result. " << endl;
        cout << "5) (-) for subtraction. " << endl;
        cout << "6) (/) for divison. " << endl;
        cout << "7) (s) for square root. " << endl;
        cout << "8) (q) to quit the program. " << endl << endl;

        cin >> choice;
        cout << endl;

        if (choice == 8){
            cout << "Quitting the program..." << endl;
            break;
        }

        cout << "Enter the second number: ";
        cin >> secondOperand;
        cout << endl;

        switch(choice){
            case 1: result = firstOperand + secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;


            case 2: result = firstOperand * secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case 3: result = pow(firstOperand, secondOperand);
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case 4: result = 0;
                    cout << "The result has been cleared to " << result << endl;
                    cout << "Enter the first operand: ";
                    cin >> firstOperand;
                    cout << endl;
                    break;

            case 5: result = firstOperand - secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case 6: if(secondOperand){
                        result = firstOperand / secondOperand;
                        cout << "The result of the calculation is " << result << endl;
                        firstOperand = result;
                        break;
                    }
                    else{
                        cout << "Second operand is " << secondOperand << "Choose again!" << endl;
                    }
            case 7: result = sqrt(secondOperand);
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            default:cout << "Invalid input. Enter again!" << endl;
                    break;
        }
    }
}

主代码包含在带有退出条件的无限while循环中。break 语句用于while在用户希望时退出循环。注意:运算符的输入是数字而不是符号。char如果您想使用符号作为输入,您必须将选择变量更改为。

更新:以下代码用于字符输入。

#include <iostream>
#include <math.h>

using namespace std; 

int main(){
    float firstOperand, secondOperand, result;
    int quit = 0;
    char choice;

    cout << "Enter a number: ";
    cin >> firstOperand;
    cout << endl;

    while (1){
        cout << "The first operand is " << firstOperand << endl;

        cout << "\nChoose an instruction code: " << endl;
        cout << "1) (+) for addition. " << endl;
        cout << "2) (*) for multiplication. " << endl;
        cout << "3) (p) for power. " << endl;
        cout << "4) (c) to clear the current result. " << endl;
        cout << "5) (-) for subtraction. " << endl;
        cout << "6) (/) for divison. " << endl;
        cout << "7) (s) for square root. " << endl;
        cout << "8) (q) to quit the program. " << endl << endl;

        cin >> choice;
        cout << endl;

        if (choice == 'q'){
            cout << "Quitting the program..." << endl;
            break;
        }

        cout << "Enter the second number: ";
        cin >> secondOperand;
        cout << endl;

        switch(choice){
            case '+': result = firstOperand + secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;


            case '*': result = firstOperand * secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case 'p': result = pow(firstOperand, secondOperand);
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case 'c': result = 0;
                    cout << "The result has been cleared to " << result << endl;
                    cout << "Enter the first operand: ";
                    cin >> firstOperand;
                    cout << endl;
                    break;

            case '-': result = firstOperand - secondOperand;
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            case '/': if(secondOperand){
                        result = firstOperand / secondOperand;
                        cout << "The result of the calculation is " << result << endl;
                        firstOperand = result;
                        break;
                    }
                    else{
                        cout << "Second operand is " << secondOperand << "Choose again!" << endl;
                    }
            case 's': result = sqrt(secondOperand);
                    cout << "The result of the calculation is " << result << endl;
                    firstOperand = result;
                    break;

            default: cout << "Invalid input. Enter again!" << endl;
                    break;
        }
    }
}

推荐阅读