calculator (Linux) - library API 1.1.1
calculator - Libraries
Loading...
Searching...
No Matches
Static Public Member Functions | Static Private Member Functions | List of all members
Coruh::Calculator::ExpressionParser Class Reference

Parses and evaluates infix arithmetic expressions built from +, -, *, /, parentheses, and (possibly decimal) numbers. More...

#include <expressionParser.h>

Collaboration diagram for Coruh::Calculator::ExpressionParser:

Static Public Member Functions

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 Private Member Functions

static bool isOperator (char c)
 True for '+', '-', '*', '/'.
 
static int precedence (char c)
 Operator precedence: 2 for '*','/'; 1 for '+','-'; 0 otherwise.
 

Detailed Description

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.

Member Function Documentation

◆ evaluateInfix()

double ExpressionParser::evaluateInfix ( const std::string &  infix)
static

Convenience helper: parses and evaluates an infix expression in one call (toPostfix() + evaluatePostfix()).

Parameters
infixThe infix expression.
Returns
The numeric result.
Exceptions
std::invalid_argumentSee toPostfix() and evaluatePostfix().

◆ evaluatePostfix()

double ExpressionParser::evaluatePostfix ( const std::string &  postfix)
static

Evaluates a postfix (RPN) expression as produced by toPostfix().

Parameters
postfixThe postfix expression, tokens separated by whitespace.
Returns
The numeric result.
Exceptions
std::invalid_argumentIf 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
infixThe 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_argumentIf 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: