输入两个操作数和一个操作运算符,分别用if语句和switch语句实现(c➕➕)程序该怎么写

输入两个操作数和一个操作运算符,分别用if语句和switch语句实现

使用if语句实现:

#include <iostream>
using namespace std;

int main() {
    float num1, num2;
    char op;

    cout << "请输入两个操作数:";
    cin >> num1 >> num2;
    cout << "请输入操作运算符:";
    cin >> op;

    if (op == '+') {
        cout << num1 + num2 << endl;
    }
    else if (op == '-') {
        cout << num1 - num2 << endl;
    }
    else if (op == '*') {
        cout << num1 * num2 << endl;
    }
    else if (op == '/') {
        cout << num1 / num2 << endl;
    }
    else {
        cout << "无效的操作符" << endl;
    }

    return 0;
}
使用switch语句实现:

#include <iostream>
using namespace std;

int main() {
    float num1, num2;
    char op;

    cout << "请输入两个操作数:";
    cin >> num1 >> num2;
    cout << "请输入操作运算符:";
    cin >> op;

    switch (op) {
        case '+':
            cout << num1 + num2 << endl;
            break;
        case '-':
            cout << num1 - num2 << endl;
            break;
        case '*':
            cout << num1 * num2 << endl;
            break;
        case '/':
            cout << num1 / num2 << endl;
            break;
        default:
            cout << "无效的操作符" << endl;
            break;
    }

    return 0;
}

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7519978
  • 这篇博客也不错, 你可以看下if语句和switch语句
  • 除此之外, 这篇博客: if与switch的对比中的 5. if和switch的出场时机 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • if会挨个进行比较然后再跳转,而switch对于一种特殊的情况会做一个特别的优化,也就是当多个案例是连续的时候,这时候的switch就会建立一个表,所谓的表其实也就是连续的1,2,3,4…并没有在编译的时候会做一个表,然后进行查询。主要查询过程是这样的:首先会对比该值有没有超过最大的值,如果没超过就看相差几,然后就只需要进行几个跳转就行,具体示例讲解请看下一章

    if和switch在编译的时候的区别.png

  • 您还可以看一下 Abel小智老师的嵌入式开发系统学习路线 从基础到项目 精品教程 工程师必备课程 物联网课程中的 什么时候使用if&switch?小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    如果要用 C++ 程序实现输入两个操作数和一个操作运算符,并分别使用 if 语句和 switch 语句进行运算,可以按照以下步骤进行:

    1.提示用户输入两个操作数和一个操作符。可以用 cout 显示信息,然后通过 cin 获取用户的输入。示例代码如下:

    #include <iostream>
    using namespace std;
    
    int main() {
      double operand1, operand2;
      char op;
    
      cout << "Please enter the first operand: ";
      cin >> operand1;
    
      cout << "Please enter the second operand: ";
      cin >> operand2;
    
      cout << "Please enter the operator (+, -, *, /): ";
      cin >> op;
      // 处理运算
      ...
      return 0;
    }
    
    1. 在 if 语句中进行运算。根据取得的运算符,用 if 语句处理不同的情况。示例代码如下:
    if (op == '+') {
      cout << "The result is: " << operand1 + operand2 << endl;
    } else if (op == '-') {
      cout << "The result is: " << operand1 - operand2 << endl;
    } else if (op == '*') {
      cout << "The result is: " << operand1 * operand2 << endl;
    } else if (op == '/') {
      if (operand2 == 0) {
        cout << "Divide by zero error" << endl;
      } else {
        cout << "The result is: " << operand1 / operand2 << endl;
      }
    } else {
      cout << "Invalid operator" << endl;
    }
    
    1. 用 switch 语句进行运算。根据取得的运算符,用 switch 语句处理不同的情况。示例代码如下:
    switch (op) {
      case '+':
        cout << "The result is: " << operand1 + operand2 << endl;
        break;
      case '-':
        cout << "The result is: " << operand1 - operand2 << endl;
        break;
      case '*':
        cout << "The result is: " << operand1 * operand2 << endl;
        break;
      case '/':
        if (operand2 == 0) {
          cout << "Divide by zero error" << endl;
        } else {
          cout << "The result is: " << operand1 / operand2 << endl;
        }
        break;
      default:
        cout << "Invalid operator" << endl;
        break;
    }
    

    完整代码如下:

    #include <iostream>
    
    using namespace std;
    
    int main() {
      double operand1, operand2;
      char op;
    
      cout << "Please enter the first operand: ";
      cin >> operand1;
    
      cout << "Please enter the second operand: ";
      cin >> operand2;
    
      cout << "Please enter the operator (+, -, *, /): ";
      cin >> op;
    
      // if 语句处理运算
      if (op == '+') {
        cout << "The result is: " << operand1 + operand2 << endl;
      } else if (op == '-') {
        cout << "The result is: " << operand1 - operand2 << endl;
      } else if (op == '*') {
        cout << "The result is: " << operand1 * operand2 << endl;
      } else if (op == '/') {
        if (operand2 == 0) {
          cout << "Divide by zero error" << endl;
        } else {
          cout << "The result is: " << operand1 / operand2 << endl;
        }
      } else {
        cout << "Invalid operator" << endl;
      }
    
      // switch 语句处理运算
      switch (op) {
        case '+':
          cout << "The result is: " << operand1 + operand2 << endl;
          break;
        case '-':
          cout << "The result is: " << operand1 - operand2 << endl;
          break;
        case '*':
          cout << "The result is: " << operand1 * operand2 << endl;
          break;
        case '/':
          if (operand2 == 0) {
            cout << "Divide by zero error" << endl;
          } else {
            cout << "The result is: " << operand1 / operand2 << endl;
          }
          break;
        default:
          cout << "Invalid operator" << endl;
          break;
      }
    
      return 0;
    }