OR could do like this (my favorite)
Code:
typedef void (OpFncPtr)(double, double, double&);
void OpAdd(double x, double y, double& z)
{
z = x + y;
cout << "The sum of " << x << " + " << y << " = " << z << endl;
}
void OpSub(double x, double y, double& z)
{
z = x - y;
cout << "The difference of " << x << " - " << y << " = " << z << endl;
}
void OpMul(double x, double y, double& z)
{
z = x * y;
cout << "The product of " << x << " * " << y << " = " << z << endl;
}
void OpDiv(double x, double y, double& z)
{
z = x / y;
cout << "The result of " << x << " / " << y << " = " << z << endl;
}
void OpEqual(double x, double y, double&)
{
if (x == y)
cout << x << " is Equal to " << y << endl;
else
cout << x << " is Not Equal to " << y << endl;
}
void OpNEqual(double x, double y, double&)
{
if (x != y)
cout << x << " is Not Equal to " << y << endl;
else
cout << x << " is Equal to " << y << endl;
}
void OpLesserThan(double x, double y, double&)
{
if (x < y)
cout << x << " is Lesser Than " << y << endl;
else
cout << x << " is Not Lesser Than " << y << endl;
}
void OpGreaterThan(double x, double y, double&)
{
if (x < y)
cout << x << " is Greater Than " << y << endl;
else
cout << x << " is Not Greater Than " << y << endl;
}
void OpLessEqualThan(double x, double y, double&)
{
if (x <= y)
cout << x << " is Less Than or Equal to " << y << endl;
else
cout << x << " is Not Less Than or Equal to " << y << endl;
}
void OpGreatEqualThan(double x, double y, double&)
{
if (x >= y)
cout << x << " is Greater Than or Equal to " << y << endl;
else
cout << x << " is Not Greater Than or Equal to " << y << endl;
}
int main()
{
double x, y, z;
string operation;
string sx, sy;
int oper = 0;
// Create and fill function table
const int TableSize = ('>' + ('=' << 8));
OpFncPtr* FunctionTable[TableSize];
ZeroMemory(&FunctionTable, sizeof(FunctionTable));
FunctionTable['+'] = &OpAdd;
FunctionTable['-'] = &OpSub;
FunctionTable['*'] = &OpMul;
FunctionTable['/'] = &OpDiv;
FunctionTable['=' + ('=' << 8)] = &OpEqual;
FunctionTable['!' + ('=' << 8)] = &OpNEqual;
FunctionTable['<'] = &OpLesserThan;
FunctionTable['<'] = &OpGreaterThan;
FunctionTable['<' + ('=' << 8)] = &OpLessEqualThan;
FunctionTable['>' + ('=' << 8)] = &OpGreatEqualThan;
cout << "\nThis program performs aritnmetic and relational operations"
<< "\non two fractions that you enter.";
const string MENU = "\nThe operations that you can use are:"
"\n Addition -------------------> use '+'"
"\n Subtraction ----------------> use '-'"
"\n Multiplication -------------> use '*'"
"\n Division -------------------> use '/'"
"\n Equal To -------------------> use '=='"
"\n Not Equal To ---------------> use '!='"
"\n Less Than ------------------> use '<'"
"\n Greater Than ---------------> use '>'"
"\n Less Than or Equal To ------> use '<='"
"\n Greater Than or Equal To ---> use '>='"
"\n Or to QUIT -----------------> enter 'Q'"
"\nEnter the required symbol when prompted.";
do
{
cout << MENU;
cout << "\nEnter the first fraction in the form a/b: ";
cin >> sx;
cout << "\nEnter the second fraction in the form a/b: ";
cin >> sy;
cout << "\nEnter the operation symbol: ";
cin >> operation;
cout << endl;
if (atoi( sx.c_str() ) == 0 && sx.c_str()[0] != '0' || atoi( sy.c_str() ) == 0 && sy.c_str()[0] != '0')
{
cerr << "Invalid number entered. Please try again.\n\n";
continue;
}
x = strtod(sx.c_str(), NULL);
y = strtod(sy.c_str(), NULL);
const char* temp = operation.c_str();
oper = (int)temp[0] + (temp[1] << 8);
if (FunctionTable[oper] == NULL)
{
cerr << "\nInvalid operation choice, please try again!";
continue;
}
try
{
FunctionTable[oper](x, y, z);
}
catch(...)
{
cerr << "\nInvalid operation choice, please try again!";
continue;
}
}
while (oper != 'Q' || oper != 'q');
return 0;
}