C++编写简易计算器
时间: 2023-11-14 13:06:59 浏览: 90
c++编写的简易计算器
下面是一个简单的 C++ 计算器代码,可以进行加减乘除运算:
```c++
#include <iostream>
using namespace std;
int main() {
char op;
double num1, num2;
cout << "请输入运算符 (+, -, *, /): ";
cin >> op;
cout << "请输入两个数字: ";
cin >> num1 >> num2;
switch(op) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// 如果运算符不是 +, -, *, 或 /,提示错误信息
cout << "错误!您输入的运算符不正确";
break;
}
return 0;
}
```
阅读全文