/*****************************************************************************
 **    File:  SimpCalc.cpp                                                  **
 **    CIS 110A - 501  :  An interpreter for a simple calculator            **
 *****************************************************************************/

#include <iostream.h>

#define    ADD         '+'             // define the four
#define    SUBTRACT    '-'             // functions that we will
#define    TIMES       '*'             // support using the usual
#define    DIVIDE      '/'             // symbols


/*****************************************************************************
 **     Calculator <- fValue1 cOperation fValue2;  e.g.,  # <- a + b        **
 *****************************************************************************/

float Calculator (float fValue1, char cOperation, float fValue2)
{
    float    fResult;

    switch (cOperation)
    {
       case ADD:
            fResult = fValue1 + fValue2;
            break;
       case SUBTRACT:
            fResult = fValue1 - fValue2;
            break;
       case TIMES:
            fResult = fValue1 * fValue2;
            break;
       case DIVIDE:
            if (fValue2 != 0.0)                // disallow division by zero
                fResult = fValue1 / fValue2;
            else
                fResult = 0.0;                 // define  #/0  to be zero
            break;
    }
    return fResult;
}


/*****************************************************************************
 **     Display - utility to "prettyprint" the Calculator's results         **
 *****************************************************************************/

void Display (float fAnswer, float fNumber1, char cOperation, float fNumber2)
{
    cout << fAnswer << " <- " << fNumber1 << cOperation << fNumber2 << endl;
}


/*****************************************************************************
 **     main - run the Calculator and Display the results...                **
 *****************************************************************************/

int main (void)
{
    char    cOperate;
    float    fAnswer, fNumber1, fNumber2;

    cout << "First number: ";
    cin  >> fNumber1;
    cout << "Second number: ";
    cin  >> fNumber2;
    cout << "Operation (+ - * /): ";
    cin  >> cOperate;

    fAnswer = Calculator (fNumber1, cOperate, fNumber2);

    Display (fAnswer, fNumber1, cOperate, fNumber2);

    return 0;
}

/*****************************************************************************

 Here're two sample executions...

 First number: 5                     First number: 4
 Second number: 7                    Second number: 9
 Operation (+ - * /): *              Operation ( + - * /): /
 35 <- 5*7                           0.444444 <- 4/9

/*****************************************************************************/

