//简易计算器
#include <iostream>
#include <cmath>
#include <string>
int main()
{
using namespace std;
double num1, num2;
char op;
double result;
string caption1("Error!Divided by 0!\n");
string caption2("Invalid opereator!\n");
while (1)
{
cout << "Please input the expression\n";
cin >> num1 >> op >> num2;
switch (op) //op不要加单引号!!!
{
case '+':
result = num1 + num2;
cout << num1 << op << num2 << "=" << result << endl; break;
case '-':
result = num1 - num2;
cout << num1 << op << num2 << "=" << result << endl; break;
case '*':
result = num1 * num2;
cout << num1 << op << num2 << "=" << result << endl; break;
case '/':
if (fabs(num2)<1.0e-8)
{
cout << caption1 << endl;
}
else
{
result = num1 / num2;
cout << num1 << op << num2 << "=" << result << endl; break;
}
default: cout << caption2 << endl;
}
}
return 0;
}
这个语句主要利用了开关语句来判断对应的关系,注意switch (op) op不要加单引号!!!