Parses and evaluates infix arithmetic expressions built from +, -, *, /, parentheses, and (possibly decimal) numbers.
More...
#include <expressionParser.h>
|
| static std::string | toPostfix (const std::string &infix) |
| | Converts an infix expression to postfix (RPN) notation using the shunting-yard algorithm.
|
| |
| static double | evaluatePostfix (const std::string &postfix) |
| | Evaluates a postfix (RPN) expression as produced by toPostfix().
|
| |
| static double | evaluateInfix (const std::string &infix) |
| | Convenience helper: parses and evaluates an infix expression in one call (toPostfix() + evaluatePostfix()).
|
| |
|
| static bool | isOperator (char c) |
| | True for '+', '-', '*', '/'.
|
| |
| static int | precedence (char c) |
| | Operator precedence: 2 for '*','/'; 1 for '+','-'; 0 otherwise.
|
| |
Parses and evaluates infix arithmetic expressions built from +, -, *, /, parentheses, and (possibly decimal) numbers.
This class used to live inline inside the calculatorapp sample application; it was moved into the library so it can be unit tested with googletest independently of the interactive app.
◆ evaluateInfix()
| double ExpressionParser::evaluateInfix |
( |
const std::string & | infix | ) |
|
|
static |
Convenience helper: parses and evaluates an infix expression in one call (toPostfix() + evaluatePostfix()).
- Parameters
-
| infix | The infix expression. |
- Returns
- The numeric result.
- Exceptions
-
◆ evaluatePostfix()
| double ExpressionParser::evaluatePostfix |
( |
const std::string & | postfix | ) |
|
|
static |
Evaluates a postfix (RPN) expression as produced by toPostfix().
- Parameters
-
| postfix | The postfix expression, tokens separated by whitespace. |
- Returns
- The numeric result.
- Exceptions
-
| std::invalid_argument | If there are not enough operands for an operator, the expression does not reduce to a single value, or a division by zero is attempted. |
◆ isOperator()
| bool ExpressionParser::isOperator |
( |
char | c | ) |
|
|
staticprivate |
True for '+', '-', '*', '/'.
◆ precedence()
| int ExpressionParser::precedence |
( |
char | c | ) |
|
|
staticprivate |
Operator precedence: 2 for '*','/'; 1 for '+','-'; 0 otherwise.
◆ toPostfix()
| std::string ExpressionParser::toPostfix |
( |
const std::string & | infix | ) |
|
|
static |
Converts an infix expression to postfix (RPN) notation using the shunting-yard algorithm.
- Parameters
-
| infix | The infix expression, e.g. "2 + 3 * (4 - 1)". |
- Returns
- The postfix form, tokens separated by single spaces, e.g. "2 3 4 1 - * +".
- Exceptions
-
| std::invalid_argument | If the expression is empty, has an unmatched '(' or ')', is missing an operand or an operator, has a number with more than one decimal point or a decimal point with no digits, or contains a character that is not a digit, '.', an operator, a parenthesis, or whitespace. |
The documentation for this class was generated from the following files: