📄 program_4_4.cpp
字号:
// Program 4.4: Compute a simple arithmetic expression
#include <iostream>
using namespace std;
int main() {
// prompt and extract desired operation
cout << "Please enter a simple expression "
<< endl << "(number operator number): ";
int LeftOperand;
int RightOperand;
char Operator;
cin >> LeftOperand >> Operator >> RightOperand;
// validate and compute desired operation
int Result;
switch ( Operator ) {
case '+':
Result = LeftOperand + RightOperand;
break;
case '-':
Result = LeftOperand - RightOperand;
break;
case '*':
Result = LeftOperand * RightOperand;
break;
case '/':
if (RightOperand != 0 )
Result = LeftOperand / RightOperand;
else {
cout << LeftOperand << " / "
<< " denominator is 0." << endl;
return 1;
}
break;
default:
cout << Operator
<< " is unrecognized operation." << endl;
return 1;
}
// display result
cout << LeftOperand << " " << Operator << " "
<< RightOperand << " equals " << Result << endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -