Created
February 28, 2025 08:08
-
-
Save izabera/ff958699fd910814da30daf95142af8c to your computer and use it in GitHub Desktop.
basic rpn calculator with assignments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -f | |
IFS= | |
rpn () { | |
for token do | |
case $token in | |
+|-|\*|/|%|\*\*) (( stack[-2] = stack[-2] $token stack[-1] )); unset stack[-1] ;; | |
=) (( ${stack[-2]} = stack[-1] )); unset stack[-1] stack[-1] ;; | |
*) stack+=($token) ;; | |
esac | |
done | |
echo $* ...... ${stack[-1]} | |
} | |
rpn 3 4 2 * 1 5 - 2 3 ** ** / + | |
rpn 3 4 2 * + | |
rpn 1 5 - | |
rpn 3 2 ** | |
rpn 2 2 + a 3 2 ** = a + a + | |
declare -p a |
Author
izabera
commented
Feb 28, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment