From 342052a0a480deaad9f43f63b910ba942d0bfced Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sun, 24 Mar 2024 19:19:42 +0900 Subject: [PATCH] Add exponentiation operator to toRPN and calculateRPN functions --- src/ts/process/infunctions.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ts/process/infunctions.ts b/src/ts/process/infunctions.ts index 59ddd7a5..606da561 100644 --- a/src/ts/process/infunctions.ts +++ b/src/ts/process/infunctions.ts @@ -6,7 +6,8 @@ function toRPN(expression:string) { '+': {precedence: 2, associativity: 'Left'}, '-': {precedence: 2, associativity: 'Left'}, '*': {precedence: 3, associativity: 'Left'}, - '/': {precedence: 3, associativity: 'Left'} + '/': {precedence: 3, associativity: 'Left'}, + '^': {precedence: 4, associativity: 'Right'}, }; expression = expression.replace(/\s+/g, ''); @@ -15,7 +16,7 @@ function toRPN(expression:string) { expression2.forEach(token => { if (parseFloat(token) || token === '0') { outputQueue += token + ' '; - } else if ('+-*/'.includes(token)) { + } else if ('+-*/^'.includes(token)) { while (operatorStack.length > 0 && ((operators[token].associativity === 'Left' && operators[token].precedence <= operators[operatorStack[operatorStack.length - 1]].precedence) || @@ -48,6 +49,7 @@ function calculateRPN(expression:string) { case '-': stack.push(a - b); break; case '*': stack.push(a * b); break; case '/': stack.push(a / b); break; + case '^': stack.push(a ** b); break; } } });