循环计算器

时间:2019-06-26 03:59:46

标签: c++

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

因此,我不确定是否通过使用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;
}

2 个答案:

答案 0 :(得分:2)

尝试以下代码:

#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;
        }
    }
}

答案 1 :(得分:2)

以下是代码中的一种可能版本:

   #include <iostream>
   #include <iomanip>
   #include <cmath>
   using namespace std; 
int select_operation()
{
    int choice;
    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;
    cin >> choice;
    cout << endl;
    return choice;
}
int main()
{
    float secondOperand;
    float firstOperand;
    float thirdOperand;
    float resultOne;
    int choice;

    cout << "Enter a number: ";
    cin >> firstOperand;
    cout << endl;
    choice = select_operation();
    if (choice == 8)
            return 0;
    else if(choice == 7)

 secondOperand = .5;
    else{
            cout << "Enter the second number: ";
            cin >> secondOperand;
    }
    cout << endl;

    while(1)
    {
            switch (choice) {
                    case 1:
                            resultOne = firstOperand + secondOperand;
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
                    case 2:
                            resultOne = firstOperand * secondOperand;
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
                    case 3:
                            resultOne = pow(firstOperand,secondOperand);
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
                    case 4:
                            resultOne = 0;
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
                    case 5:
                            resultOne = firstOperand - secondOperand;
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
                    case 6:
                           if(secondOperand){
                                  resultOne = firstOperand / secondOperand;
                                  cout << "The result of the calculation is " << resultOne << endl;
                            }
                            break;
                    case 7:
                            resultOne = pow(firstOperand,secondOperand);
                            cout << "The result of the calculation is " << resultOne << endl;
                            break;
            }

            choice = select_operation();
            if (choice == 8)
                    return 0;
            else if(choice == 7)
                    secondOperand = .5;
            else{
                    cout << "Enter another number ";
                    cin >> thirdOperand;
                    secondOperand = thirdOperand;
            }
    }
    cout << endl;
    firstOperand =  resultOne;
    return 0;

}

上面的代码首先接受两个输入,如果是选项8,则立即返回(退出程序)。如果运算是平方根,因为它是不接受第二个运算数的一元运算。 继续读取第三个操作数和运算符,直到要求停止。