LCOV - code coverage report
Current view: top level - calculator\header\expressionParser.h - expressionParser.h (source / functions) Hit Total Coverage
Test: calculator library documentation coverage - genhtml (Windows) Lines: 7 7 100.0 %
Date: Wed Sep 30 03:07:43 2026 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       1           1 : /**
       2             :  * @file expressionParser.h
       3             :  *
       4             :  * @brief Converts an infix arithmetic expression to postfix (Reverse Polish
       5             :  *        Notation) and evaluates it, on top of the Calculator functions.
       6             :  */
       7             : 
       8             : #ifndef EXPRESSION_PARSER_H
       9             : #define EXPRESSION_PARSER_H
      10             : 
      11             : #include <string>
      12             : 
      13             : namespace Coruh
      14             : {
      15             :     namespace Calculator
      16             :     {
      17             :         /**
      18             :             @class ExpressionParser
      19             :             @brief Parses and evaluates infix arithmetic expressions built from
      20             :                    +, -, *, /, parentheses, and (possibly decimal) numbers.
      21             : 
      22             :             This class used to live inline inside the calculatorapp sample
      23             :             application; it was moved into the library so it can be unit
      24             :             tested with googletest independently of the interactive app.
      25             :         */
      26           1 :         class ExpressionParser
      27             :         {
      28             :         public:
      29             :             /**
      30             :              * @brief Converts an infix expression to postfix (RPN) notation
      31             :              *        using the shunting-yard algorithm.
      32             :              *
      33             :              * @param infix The infix expression, e.g. "2 + 3 * (4 - 1)".
      34             :              * @return The postfix form, tokens separated by single spaces,
      35             :              *         e.g. "2 3 4 1 - * +".
      36             :              * @throws std::invalid_argument If the expression is empty, has
      37             :              *         an unmatched '(' or ')', is missing an operand or an
      38             :              *         operator, has a number with more than one decimal
      39             :              *         point or a decimal point with no digits, or contains
      40             :              *         a character that is not a digit, '.', an operator, a
      41             :              *         parenthesis, or whitespace.
      42             :              */
      43           1 :             static std::string toPostfix(const std::string& infix);
      44             : 
      45             :             /**
      46             :              * @brief Evaluates a postfix (RPN) expression as produced by
      47             :              *        toPostfix().
      48             :              *
      49             :              * @param postfix The postfix expression, tokens separated by
      50             :              *        whitespace.
      51             :              * @return The numeric result.
      52             :              * @throws std::invalid_argument If there are not enough operands
      53             :              *         for an operator, the expression does not reduce to a
      54             :              *         single value, or a division by zero is attempted.
      55             :              */
      56           1 :             static double evaluatePostfix(const std::string& postfix);
      57             : 
      58             :             /**
      59             :              * @brief Convenience helper: parses and evaluates an infix
      60             :              *        expression in one call (toPostfix() + evaluatePostfix()).
      61             :              *
      62             :              * @param infix The infix expression.
      63             :              * @return The numeric result.
      64             :              * @throws std::invalid_argument See toPostfix() and
      65             :              *         evaluatePostfix().
      66             :              */
      67           1 :             static double evaluateInfix(const std::string& infix);
      68             : 
      69             :         private:
      70             :             /// True for '+', '-', '*', '/'.
      71           1 :             static bool isOperator(char c);
      72             : 
      73             :             /// Operator precedence: 2 for '*','/'; 1 for '+','-'; 0 otherwise.
      74           1 :             static int precedence(char c);
      75             :         };
      76             :     }
      77             : }
      78             : 
      79             : #endif // EXPRESSION_PARSER_H

Generated by: LCOV version 1.15.alpha0w