Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
|
|
@ -0,0 +1,26 @@
|
|||
def rpn_calc (tokens)
|
||||
stack = [] of Float64
|
||||
tokens.each do |token|
|
||||
if token.is_a?(Float64)
|
||||
stack.push token
|
||||
else
|
||||
x = stack.pop
|
||||
y = stack.pop
|
||||
case token
|
||||
when "+" then stack.push(y + x)
|
||||
when "-" then stack.push(y - x)
|
||||
when "*" then stack.push(y * x)
|
||||
when "/" then stack.push(y / x)
|
||||
when "^" then stack.push(y ** x)
|
||||
else raise "unknown operator: #{token}"
|
||||
end
|
||||
end
|
||||
puts "%4s %s" % {token, stack}
|
||||
end
|
||||
end
|
||||
|
||||
def tokenize (s)
|
||||
s.split.map {|t| t.to_f rescue t }
|
||||
end
|
||||
|
||||
rpn_calc tokenize "3 4 2 * 1 5 - 2 3 ^ ^ / +"
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
require "queue"
|
||||
local fmt = require "fmt"
|
||||
|
||||
local function rpn_calculate(expr)
|
||||
assert(#expr > 0, "expression cannot be empty")
|
||||
print($"For expression = {expr}\n")
|
||||
print("Token Action Stack")
|
||||
local tokens = expr:split(" "):filter(|t| -> #t > 0):reorder()
|
||||
local stk = new stack()
|
||||
for tokens as token do
|
||||
local d = tonumber(token)
|
||||
if d then
|
||||
stk:push(d)
|
||||
fmt.print(" %d Push num onto top of stack %s", d, fmt.swrite(stk:toarray()))
|
||||
elseif #token > 1 or !(token in "+-*/^") then
|
||||
error($"{token} is not a valid token.")
|
||||
elseif stk:size() < 2 then
|
||||
error("Stack contains too few operands.")
|
||||
else
|
||||
local d1 = stk:pop()
|
||||
local d2 = stk:pop()
|
||||
stk:push(token == "+" ? d2 + d1 :
|
||||
token == "-" ? d2 - d1 :
|
||||
token == "*" ? d2 * d1 :
|
||||
token == "/" ? d2 / d1 : d2 ^ d1)
|
||||
fmt.print(" %s Apply op to top of stack %s", token, fmt.swrite(stk:toarray()))
|
||||
end
|
||||
end
|
||||
fmt.print("\nThe final value is %0.14g", stk:pop())
|
||||
end
|
||||
|
||||
local expr = "3 4 2 * 1 5 - 2 3 ^ ^ / +"
|
||||
rpn_calculate(expr)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
Red [ "RPN Eval - Hinjo, July 25, 2025" ]
|
||||
rpn-exec: func [exp [string!]] [
|
||||
stack: copy []
|
||||
blk: load exp ; convert into block
|
||||
foreach tok blk [
|
||||
print [mold stack tok]
|
||||
case [
|
||||
number? tok [append stack tok]
|
||||
find [+ - * / ^] tok [
|
||||
if (length? stack) < 2 [
|
||||
print "Error: Two operands required!" exit ]
|
||||
if tok = '^ [tok: '**]
|
||||
b: take/last stack
|
||||
a: take/last stack
|
||||
append stack do compose [a (tok) b]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
; in Red, "^" is an escape char, so, it must be written as ^^
|
||||
rpn-exec "3 4 2 * 1 5 - 2 3 ^^ ^^ / +"
|
||||
Loading…
Add table
Add a link
Reference in a new issue