C++ 计算器循环

C++ 计算器循环,c++,C++,该程序将从用户处接收浮点数和运算符,并执行所需的计算,打印出结果。每次计算的结果将作为下一次计算的操作数 因此,我不确定使用switch语句是否正确执行了此操作?有更好的方法吗?也许,通过使用do-while循环?我真的很困惑 #include <iostream> #include <iomanip> using namespace std; int main() { float firstOperand; cout << "Enter a numbe

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

因此,我不确定使用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;
}
#包括
#包括
使用名称空间std;
int main()
{
浮点第一操作数;
cout>第一个操作数;
请尝试以下代码:

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

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

   #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;
#包括
#包括
#包括
使用名称空间std;
int选择_操作()
{
智力选择;

您是否确实需要一个循环和一些地方来存储上次计算的结果。通常,您会将核心计算封装在(;){
循环中,然后使用ctrl-c(在您的情况下加上“q”)来打破循环。我认为对于一般编程反馈,您应该查看或
   #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;