From e895c2296cfccf4df4184548aa1594e5507922b1 Mon Sep 17 00:00:00 2001 From: bangonicdd <157843588+bangonicdd2@users.noreply.github.com> Date: Mon, 10 Mar 2025 14:26:50 +0900 Subject: [PATCH] fix: treating unary minus in calcString --- src/ts/process/infunctions.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ts/process/infunctions.ts b/src/ts/process/infunctions.ts index e0ab4f40..70f39659 100644 --- a/src/ts/process/infunctions.ts +++ b/src/ts/process/infunctions.ts @@ -27,7 +27,11 @@ function toRPN(expression:string) { let lastToken = '' for(let i = 0; i < expression.length; i++) { - if(operatorsKeys.includes(expression[i])) { + const char = expression[i] + if (char === '-' && (i === 0 || operatorsKeys.includes(expression[i - 1]) || expression[i - 1] === '(')) { + lastToken += char + } + else if (operatorsKeys.includes(char)) { if(lastToken !== '') { expression2.push(lastToken) } @@ -35,10 +39,10 @@ function toRPN(expression:string) { expression2.push('0') } lastToken = '' - expression2.push(expression[i]) + expression2.push(char) } else{ - lastToken += expression[i] + lastToken += char } }