【问题标题】:C++ switch case calculator with terminating character带有终止字符的 C++ 切换案例计算器
【发布时间】:2017-09-17 20:22:23
【问题描述】:

我正在尝试制作一个 switch case 计算器,它将要求一个表达式(例如 2+2)并打印答案,重复该过程直到用户输入“q”。

当用户输入“q”时,我不知道如何让程序结束。下面的程序成功地要求一个表达,给出答案,并要求另一个。但是,当您输入错误的表达式时,它将永远重复默认情况,包括您输入“q”时。

我知道问题与我如何输入变量有关,考虑到操作数是 double 类型,而且 while 循环也有问题,但我想不出任何替代方案,而且似乎找不到其他地方的解决方案。

int main() {

double operand1;
double operand2;
char operation;
double answer;

while (operation != 'q'){

cout << "Enter an expression:""\n";
cin >> operand1 >> operation >> operand2;


     switch(operation)
{
    case '+':
        answer = (operand1 + operand2);
        break;

    case '-':
        answer = (operand1 - operand2);
        break;

    case '*':
        answer = (operand1 * operand2);
        break;

    case '/':
        answer = (operand1 / operand2);
        break;


    default:
            cout << "Not an operation :/";
            return 0;

}

    cout <<operand1<<operation<<operand2<< "=" << answer<< endl;

}
 return 0;
}

【问题讨论】:

标签: c++ while-loop switch-statement calculator do-while


【解决方案1】:

由于您一次读取 3 个变量,当您在控制台中键入 q 时,它被分配给 operand1,而不是 operation,它是 while 循环的终止符 - 因此,无限循环开始。

基本问题在于应用程序的逻辑。该代码的最快解决方案如下:

int main() {

    double operand1;
    double operand2;
    char operation;
    char more = 'y';
    double answer;

    while (more != 'n') {

        cout << "Enter an expression:""\n";
        cin >> operand1 >> operation >> operand2;

        switch (operation)
        {
        case '+':
            answer = (operand1 + operand2);
            break;

        case '-':
            answer = (operand1 - operand2);
            break;

        case '*':
            answer = (operand1 * operand2);
            break;

        case '/':
            answer = (operand1 / operand2);
            break;


        default:
            cout << "Not an operation :/";
            return 0;

        }

        cout << operand1 << operation << operand2 << "=" << answer << endl;
        cout << "Continue? (y/n) ";
        cin >> more;
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    一个简单的改变将解决问题:

    int main()
    {
    
        double operand1;
        double operand2;
        char operation = '8';
        double answer;
    
    
        while (operation != 'q'){
    
            cout << "Enter an expression:""\n";
            cin >> operand1 >> operation >> operand2;    
            switch(operation)
            {
                case '+':
                    answer = (operand1 + operand2);
                    break;
    
                case '-':
                    answer = (operand1 - operand2);
                    break;
    
                case '*':
                    answer = (operand1 * operand2);
                    break;
    
                case '/':
                    answer = (operand1 / operand2);
                    break;
    
    
                default:
                        cout << "Not an operation :/";
                        return 0;
    
            }
    
            cout <<operand1<<operation<<operand2<< "=" << answer<< endl;
    
            cout << "Wants to quit? Enter (q)"\n";
            cin >> operation;
    
        }
     return 0;
    }
    

    【讨论】:

    • 那个代码不行,死循环还在
    • @DevDio 有效点。我现在意识到了。我想用一种不同的方式来表达它,就像你一样..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多