Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<string>, operators: Array<string>): void {
const secondNumber = Number(output.pop());
const firstNumber = Number(output.pop());
Expand All @@ -23,12 +44,32 @@ function solveSimplifiedExpr(output: Array<string>, operators: Array<string>): 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<string> = ["+", "-", "*", "/"];
const expr: Array<string> = [];
Expand Down Expand Up @@ -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,
Expand Down