September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,40 @@
// version 1.1.2
fun rpnCalculate(expr: String) {
if (expr.isEmpty()) throw IllegalArgumentException("Expresssion cannot be empty")
println("For expression = $expr\n")
println("Token Action Stack")
val tokens = expr.split(' ').filter { it != "" }
val stack = mutableListOf<Double>()
for (token in tokens) {
val d = token.toDoubleOrNull()
if (d != null) {
stack.add(d)
println(" $d Push num onto top of stack $stack")
}
else if ((token.length > 1) || (token !in "+-*/^")) {
throw IllegalArgumentException("$token is not a valid token")
}
else if (stack.size < 2) {
throw IllegalArgumentException("Stack contains too few operands")
}
else {
val d1 = stack.removeAt(stack.lastIndex)
val d2 = stack.removeAt(stack.lastIndex)
stack.add(when (token) {
"+" -> d2 + d1
"-" -> d2 - d1
"*" -> d2 * d1
"/" -> d2 / d1
else -> Math.pow(d2, d1)
})
println(" $token Apply op to top of stack $stack")
}
}
println("\nThe final value is ${stack[0]}")
}
fun main(args: Array<String>) {
val expr = "3 4 2 * 1 5 - 2 3 ^ ^ / +"
rpnCalculate(expr)
}

View file

@ -0,0 +1,52 @@
local stack = {}
function push( a ) table.insert( stack, 1, a ) end
function pop()
if #stack == 0 then return nil end
return table.remove( stack, 1 )
end
function writeStack()
for i = #stack, 1, -1 do
io.write( stack[i], " " )
end
print()
end
function operate( a )
local s
if a == "+" then
push( pop() + pop() )
io.write( a .. "\tadd\t" ); writeStack()
elseif a == "-" then
s = pop(); push( pop() - s )
io.write( a .. "\tsub\t" ); writeStack()
elseif a == "*" then
push( pop() * pop() )
io.write( a .. "\tmul\t" ); writeStack()
elseif a == "/" then
s = pop(); push( pop() / s )
io.write( a .. "\tdiv\t" ); writeStack()
elseif a == "^" then
s = pop(); push( pop() ^ s )
io.write( a .. "\tpow\t" ); writeStack()
elseif a == "%" then
s = pop(); push( pop() % s )
io.write( a .. "\tmod\t" ); writeStack()
else
push( tonumber( a ) )
io.write( a .. "\tpush\t" ); writeStack()
end
end
function calc( s )
local t, a = "", ""
print( "\nINPUT", "OP", "STACK" )
for i = 1, #s do
a = s:sub( i, i )
if a == " " then operate( t ); t = ""
else t = t .. a
end
end
if a ~= "" then operate( a ) end
print( string.format( "\nresult: %.13f", pop() ) )
end
--[[ entry point ]]--
calc( "3 4 2 * 1 5 - 2 3 ^ ^ / +" )
calc( "22 11 *" )

View file

@ -0,0 +1,32 @@
/* ooRexx *************************************************************
* 10.11.2012 Walter Pachl translated from PL/I via REXX
**********************************************************************/
fid='rpl.txt'
ex=linein(fid)
Say 'Input:' ex
/* ex=' 3 4 2 * 1 5 - 2 3 ^ ^ / +' */
Numeric Digits 15
expr=''
st=.circularqueue~new(100)
Say 'Stack contents:'
do While ex<>''
Parse Var ex ch +1 ex
expr=expr||ch;
if ch<>' ' then do
If pos(ch,'0123456789')>0 Then /* a digit goes onto stack */
st~push(ch)
Else Do /* an operator */
op=st~pull /* get top element */
select /* and modify the (now) top el*/
when ch='+' Then st~push(st~pull + op)
when ch='-' Then st~push(st~pull - op)
when ch='*' Then st~push(st~pull * op)
when ch='/' Then st~push(st~pull / op)
when ch='^' Then st~push(st~pull ** op)
end;
Say st~string(' ','L') /* show stack in LIFO order */
end
end
end
Say 'The reverse polish expression = 'expr
Say 'The evaluated expression = 'st~pull

View file

@ -1,4 +1,4 @@
var proggie = '3 4 2 * 1 5 - 2 3 ^ ^ / +';
var proggie = '3 4 2 * 1 5 - 2 3 ^ ^ / +'
class RPN(arr=[]) {
@ -10,10 +10,10 @@ class RPN(arr=[]) {
method run(p) {
p.each_word { |w|
say "#{w} (#{arr})";
say "#{w} (#{arr})"
given (w) {
when (/\d/) {
arr << w.to_f
arr << Num(w)
}
when (<+ - * />) {
self.binop(w)
@ -30,4 +30,4 @@ class RPN(arr=[]) {
}
}
RPN.new.run(proggie);
RPN.new.run(proggie)

View file

@ -0,0 +1,28 @@
10 DIM S(5)
20 LET P=1
30 INPUT E$
40 LET I=0
50 LET I=I+1
60 IF E$(I)=" " THEN GOTO 110
70 IF I<LEN E$ THEN GOTO 50
80 LET W$=E$
90 GOSUB 150
100 STOP
110 LET W$=E$( TO I-1)
120 LET E$=E$(I+1 TO )
130 GOSUB 150
140 GOTO 40
150 IF W$="+" OR W$="-" OR W$="*" OR W$="/" OR W$="**" THEN GOTO 250
160 LET S(P)=VAL W$
170 LET P=P+1
180 PRINT W$;
190 PRINT ":";
200 FOR I=P-1 TO 1 STEP -1
210 PRINT " ";S(I);
220 NEXT I
230 PRINT
240 RETURN
250 IF W$="**" THEN LET S(P-2)=ABS S(P-2)
260 LET S(P-2)=VAL (STR$ S(P-2)+W$+STR$ S(P-1))
270 LET P=P-1
280 GOTO 180

View file

@ -0,0 +1,56 @@
Function RPN(expr As String) As String
Dim tokenArray() As String
Dim stack() As String
Dim Wert1 As Double
Dim Wert2 As Double
'Initialize array (removed later)
ReDim tokenArray(1)
ReDim stack(1)
tokenArray = Split(expr, " ")
Dim i As integer
i = 0
While i <= tokenArray.Ubound
If tokenArray(i) = "+" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1+Wert2))
ElseIf tokenArray(i) = "-" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1-Wert2))
ElseIf tokenArray(i) = "*" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1*Wert2))
ElseIf tokenArray(i) = "/" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1/Wert2))
ElseIf tokenArray(i) = "^" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(pow(Wert1,Wert2)))
Else
stack.Append(tokenArray(i))
End If
i = i +1
Wend
Return stack(2)
End Function

View file

@ -0,0 +1,18 @@
var ops=D("^",True, "*",'*, "/",'/, "+",'+, "-",'-);
fcn parseRPN(e){
println("\npostfix: ", e);
stack:=L();
foreach tok in (e.split()){
op:=ops.find(tok);
if(op){
y := stack.pop(); x := stack.pop();
if(True==op) x=x.pow(y);
else x=op(x,y);
stack.append(x);
}
else stack.append(tok.toFloat());
println(tok," --> ",stack);
}
println("result: ", stack[0])
}

View file

@ -0,0 +1,2 @@
tests:=T("3 4 2 * 1 5 - 2 3 ^ ^ / +");
foreach t in (tests) { parseRPN(t) }