代码之家  ›  专栏  ›  技术社区  ›  b w

没有else的多个if语句

  •  -1
  • b w  · 技术社区  · 6 年前

    我不允许使用 else ,但我可以使用 if

    这是我目前掌握的代码:

    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int operand1;
    int operand2;
    float output;
    char action;
    
    int main()
    {
       cout << fixed << showpoint << setprecision(2);
    
       cout << "Enter the initial balance [1-1000]: " << endl;
       cin >> operand1;
    
       cout << "Enter an action (D, W, I or C):" << endl;
       cin >> action;
    
       cout << "Enter the second operand:" << endl;
       cin >> operand2;
    
       if ((action != 'D' && action != 'W' && action != 'I' && action != 'C') || (operand1 > 1000 || operand1 < 1) || 
           (action == 'I' && operand2 > 15 || operand2 < 1) || (action == 'C' && operand2 != 20 && operand2 != 10 && operand2 != 5) ||
           (operand2 > 1000 || operand2 < 1))
       {
           cout << "Input out of range" << endl;
           return 0;
       }
    
       if (action == 'D')
       {
          output = (operand1 + operand2);
          cout << "The new account balance is " << output << endl;
       }
    
       if (action == 'W')
       {
          output = (operand1 - operand2);
          if (output<0)
          {
              cout << "Input out of range" << endl;
              return 0;
          }
           cout << "The new account balance is " << output << endl;
       }
    
       if (action == 'I')
       {
    
           output = ((float)operand1 + (((float)operand2 / 100) * (float)operand1));
           cout << "The new account balance is " << output << endl;
       }
    
       if (action == 'C')
       {
           output = operand1 % operand2;
           cout << operand1 / operand2 << " bills dispensed plus " << output << endl;
       }
    
        cin.get();
        cin.get();
        return 0;
    }
    

    Enter the initial balance [1-1000]: 1030
    
    Enter an action (D, W, I or C): D
    
    Enter the second operand: 40
    
    Input out of range 
    

    Enter the initial balance [1-1000]:
    
    1030
    
    Input out of range
    
    Enter an action (D, W, I or C):
    
    D
    
    Enter the second operand:
    
    40
    
    The new account balance is 1070.00
    

    声明。

    3 回复  |  直到 6 年前
        1
  •  1
  •   dszakal    6 年前

    使用开关(操作):

    https://en.cppreference.com/w/cpp/language/switch

    在这种情况下,它可以有一个默认值。

    还有很多习俗禁止其他的,但不是禁止elseif-你确定elseif在你的情况下是禁止的吗?

    但即使允许elseif,switch也更适合阅读,是一个更优雅的解决方案。

        2
  •  0
  •   S.R.    6 年前

    您可以通过将所有命令作为不同的案例来使用switch。这是其他人已经说过的话。

        3
  •  0
  •   Joop Eggen    6 年前

    && 优先级高于 ||

    if (
        (action != 'D' && action != 'W' && action != 'I' && action != 'C') ||
        (operand1 > 1000 || operand1 < 1) ||
    //  (action == 'I' && operand2 > 15 || operand2 < 1) ||
        (action == 'I' && (operand2 > 15 || operand2 < 1)) ||
        (action == 'C' && operand2 != 20 && operand2 != 10 && operand2 != 5) ||
        (operand2 > 1000 || operand2 < 1))
    {
        cout << "Input out of range" << endl;
        return 0;
    }
    

    为了在代码的功能上有更多的可追溯性,值得付出努力:

    if (action != 'D' && action != 'W' && action != 'I' && action != 'C')
    {
        cout << "Input out of range; action " << action << endl;
        return 0;
    }
    if (operand1 > 1000 || operand1 < 1)
    {
        cout << "Input out of range; 1st operand: " << operand1 << endl;
        return 0;
    }
    ...