#include <iostream>
#include <stack>
#include <stdexcept>
#include <string>
|
| using | ValueType = int |
| | This program only does integer arithmetic (and it's pretty limited integer arithmetic, with only one-digit operands). More...
|
| |
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.
Do a binary operation.
- Parameters
-
| operandL | the left operatand |
| operation | the operation to be performed |
| operandR | the right operatand |
- Exceptions
-
- Returns
- the result of the operation
Do the binary operation using the top element of an operand stack and the top two elements an operator stack.
- Parameters
-
| valstack | a stack of operands |
| opStack | a stack of operators |
- Exceptions
-
- 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
-
| response | the 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
-
| op | the 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
Determine the value of an infix expression.
- Parameters
-
| expr | a string, supposedly representing an infix expression |
- Returns
- the value of
expr
- Exceptions
-