diff --git a/main.ts b/main.ts index 5c2c672..8e179d3 100644 --- a/main.ts +++ b/main.ts @@ -1,3 +1,14 @@ +/** + * Performs a basic arithmetic operation on two numbers. + * + * This function applies the operator ("+", "-", "*", or "/") to the given operands and + * returns the computed result. + * + * @param firstNumber - The first operand. + * @param secondNumber - The second operand. + * @param operator - The arithmetic operator to use; valid values are "+", "-", "*", or "/". + * @returns The result of performing the arithmetic operation. + */ function solve(firstNumber: number, secondNumber: number, operator: string): number { const operations: { [index: string]: Function } = { "+": () => firstNumber + secondNumber, @@ -10,6 +21,16 @@ function solve(firstNumber: number, secondNumber: number, operator: string): num } +/** + * Evaluates a simple sub-expression using the two most recent numeric operands and the last operator. + * + * This function removes the last two elements from the output array, converts them to numbers, retrieves the last operator from the operators array, and calculates the result using the `solve` function. The computed result is pushed back onto the output array as a string. + * + * @param output - An array of strings representing numeric operands, with the last two entries serving as operands. + * @param operators - An array of strings representing operators, where the last entry is used for the calculation. + * + * @throws {string} "Operation Not Supported." if no operator is available. + */ function solveSimplifiedExpr(output: Array, operators: Array): void { const secondNumber = Number(output.pop()); const firstNumber = Number(output.pop()); @@ -23,12 +44,32 @@ function solveSimplifiedExpr(output: Array, operators: Array): v output.push(solve(firstNumber, secondNumber, operator).toString()); } +/** + * Checks if the provided character is a digit (0-9). + * + * @param character The character to evaluate. + * @returns True if the character is a digit, false otherwise. + */ function isDigit(character: string) { return character.charCodeAt(0) >= "0".charCodeAt(0) && character.charCodeAt(0) <= "9".charCodeAt(0); } +/** + * Parses a mathematical expression string into an array of tokens. + * + * The function tokenizes the expression by removing all spaces and then separating numbers, + * decimal points, arithmetic operators, and parentheses. It supports negative numbers + * and validates proper decimal formatting, operator positioning, and balanced parentheses. + * If the expression is malformed, it throws an error with a descriptive message. + * + * @param expression - The mathematical expression to parse. + * @returns An array of tokens representing numbers, operators, and parentheses. + * + * @throws {string} When a decimal number is malformed, an operator is misplaced, + * a parenthesis is unmatched, or an invalid character is encountered. + */ function parseExpression(expression: string) { const operators: Array = ["+", "-", "*", "/"]; const expr: Array = []; @@ -96,6 +137,18 @@ function parseExpression(expression: string) { return expr; } +/** + * Evaluates a mathematical expression string and returns its numerical result. + * + * The function tokenizes the input using `parseExpression` and then evaluates the expression + * using a stack-based algorithm that respects operator precedence and handles parentheses. + * Intermediate results are computed by repeatedly applying operations via `solveSimplifiedExpr`. + * + * @param expression - The mathematical expression to evaluate. + * @returns The numerical result of the evaluated expression. + * + * @throws Error if the expression is malformed, such as having unmatched parentheses or invalid tokens. + */ function solveExpression(expression: string) { const priorities: { [index: string]: number } = { "+": 1,