Here is a very simple calculator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// switch/infix-calc.cpp - Simple calculator.
// Illustrates switch and continue statements.
// Not robust (eg, doesn't check for division by 0).
// Fred Swartz 10 Aug 2003
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int left, right; // Operands
char oper; // Operator
int result; // Resulting value
while (cin >> left >> oper >> right) {
switch (oper) {
case '+': result = left + right;
break;
case '-': result = left - right;
break;
case '*': result = left * right;
break;
case '/': result = left / right;
break;
default : cout << "Bad operator '" << oper << "'" << endl;
continue; // Start next loop iteration.
}
cout << result << endl << endl;
}
return 0;
}
|