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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <iostream>
using namespace std;
#include <stdlib.h>
void readInt(double*number1) {
cout << "Please enter a number and press ENTER: ";
cin >> *number1;
}
double add(double number1, double number2) { return number1 + number2; }
double subtract(double number1, double number2) { return number1 - number2; }
double multiply(double number1, double number2) { return number1 * number2; }
double divide(double number1, double number2) { return number1 / number2; }
void main ()
{
bool IsOpValid;
double number1;
double number2;
int answer;
char Op;
s:
number1 = 0;
number2 = 0;
answer = 0;
do {
p:
readInt(&number1);
cout << "Please enter an operator, 'c' to clear, or 'x' to turn off and press ENTER: ";
cin >> Op;
IsOpValid = false;
double *readInt(&number2);
switch (Op)
{
case '+':
answer = add(number1, number2);
break;
case '-':
answer = subtract(number1, number2);
break;
case '*':
answer = multiply(number1,number2);
break;
case '/':
if (number2 == 0) {
cout << "Cannot divide by 0" << endl;
continue;
} else {
answer = divide(number1,number2);
}
break;
case 'c':
case 'C':
cout << "Clearing Calculator." << endl;
continue;
case 'x':
case 'X':
IsOpValid = true;
cout << "Turning Off Calculator." << endl;
exit (0);
break;
default:
IsOpValid = true;
cout << "You have entered an invalid operator." << endl;
cout << "Please enter +, -, *, /, 'c' or 'x' as operator. " << endl;
break;
}
cout << "The result is: " << answer << endl;
} while (!IsOpValid);
}
|