循环计算器

Looping for calculators

本文关键字:计算器 循环      更新时间:2023-10-16

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

所以,我不确定是否使用开关语句正确地执行此操作?有更好的方法吗?也许,在循环时使用do?我真的很困惑。

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

尝试以下代码:

#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循环中。当用户希望的情况下,使用中断语句用于退出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;
        }
    }
}

这是代码上的一个可能版本:

   #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,则以上代码在首先立即返回两个输入(退出程序(。如果操作是平方根,因为它是一个单一操作,而不是第二操作数。继续阅读第三操作数和操作员,直到要求停止。