输入两个操作数和一个操作运算符,分别用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;
}
if会挨个进行比较然后再跳转,而switch对于一种特殊的情况会做一个特别的优化,也就是当多个案例是连续的时候,这时候的switch就会建立一个表,所谓的表其实也就是连续的1,2,3,4…并没有在编译的时候会做一个表,然后进行查询。主要查询过程是这样的:首先会对比该值有没有超过最大的值,如果没超过就看相差几,然后就只需要进行几个跳转就行,具体示例讲解请看下一章
如果要用 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;
}
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 (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;
}