program_4_4.cpp

来自「清华关于C++ 的程序讲义 值得一看 关于算法」· C++ 代码 · 共 43 行

CPP
43
字号
// 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 + =
减小字号Ctrl + -
显示快捷键?