CISC 2200 Project 3
Classes | Typedefs | Functions
proj3.cc File Reference
#include <iostream>
#include <stack>
#include <stdexcept>
#include <string>
Include dependency graph for proj3.cc:

Classes

class  IllegalExprException
 Exception class thrown by illegal infix expression. More...
 

Typedefs

using ValueType = int
 This program only does integer arithmetic (and it's pretty limited integer arithmetic, with only one-digit operands). More...
 

Functions

ValueType processExpression (const string &expr) throw (IllegalExprException)
 Determine the value of an infix expression. More...
 
bool isValidResponse (char response)
 Is a character a valid response? More...
 
bool isYesResponse (char response)
 Is a character a "yes" response? More...
 
int precedence (char op)
 Determine the precedence of an operator. More...
 
void provideHelpIfNecessary (void)
 Print a help message if the user wants one.
 
void execute (stack< ValueType > &valstack, stack< char > &opStack) throw (IllegalExprException)
 Do the binary operation using the top element of an operand stack and the top two elements an operator stack. More...
 
ValueType doOperation (ValueType operandL, char operation, ValueType operandR) throw (IllegalExprException)
 Do a binary operation. More...
 
int main (void)
 The usual main function.
 

Typedef Documentation

using ValueType = int

This program only does integer arithmetic (and it's pretty limited integer arithmetic, with only one-digit operands).

If we ever decide to do (say) floating point arithmetic, simply change the definition of ValueType. Of course, this would require us to work somewhat harder when dealing with operands.

Function Documentation

ValueType doOperation ( ValueType  operandL,
char  operation,
ValueType  operandR 
)
throw (IllegalExprException
)

Do a binary operation.

Parameters
operandLthe left operatand
operationthe operation to be performed
operandRthe right operatand
Exceptions
IllegalExprExceptionif the operation cannot be executed
Returns
the result of the operation
void execute ( stack< ValueType > &  valstack,
stack< char > &  opStack 
)
throw (IllegalExprException
)

Do the binary operation using the top element of an operand stack and the top two elements an operator stack.

Parameters
valstacka stack of operands
opStacka stack of operators
Exceptions
IllegalExprExceptionif the operation cannot be executed
Postcondition
the binary operation at the top of opStack is applied to the two operands at the top of valstack and this value is pushed to the top of valstack
bool isValidResponse ( char  response)

Is a character a valid response?

Parameters
responsethe first char in the user's response
Precondition
response is a valid character
Returns
true iff response is one of the following:
'Y', 'y', 'N', 'n', 'T', 't', 'F', 'f', '1', '0'
bool isYesResponse ( char  response)

Is a character a "yes" response?

Precondition
response is a valid response (in the sense of isValidResponse)
Returns
true iff Response is one of the following:
'Y', 'y', 'T', 't', '1'
int precedence ( char  op)

Determine the precedence of an operator.

Parameters
opthe operator in question
Precondition
op is one of '+', '-', '*', '/'
Returns
  • 3 for multiplicative operations
  • 2 for additive operations
  • 1 for illegal operation
  • 0 for a left parenthesis
ValueType processExpression ( const string &  expr)
throw (IllegalExprException
)

Determine the value of an infix expression.

Parameters
expra string, supposedly representing an infix expression
Returns
the value of expr
Exceptions
IllegalExprExceptionif expr is not a legal infix expression