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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <fstream>
void print_result(int, int, char, int);
int main() {
int first = 0, second = 0;
char mathOperation;
std::ifstream calc;
calc.open ("calc.txt");
while( calc >> first >> second >> mathOperation)
{
switch (mathOperation){
case '*':
print_result(first, second, mathOperation, first * second);
break;
case '/':
if( second == 0 ){
std::cout
<< first << ' ' << mathOperation << ' ' << second
<< " *** Division by zero\n";
}
else
print_result(first, second, mathOperation, first / second);
break;
case '+':
print_result(first, second, mathOperation, first + second);
break;
case '-':
print_result(first, second, mathOperation, first - second);
break;
default:
std::cout
<< first << ' ' << mathOperation << ' ' << second
<< " *** Operator unknown\n";
}
}
calc.close();
return 0;
}
void print_result(int aFirst, int aSecond, char aOperation, int aAnswer)
{
std::cout
<< aFirst << ' ' << aOperation << ' ' << aSecond << " = " << aAnswer << '\n';
}
|