Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,18 @@
var e = '3 4 2 * 1 5 - 2 3 ^ ^ / +'
var s=[], e=e.split(' ')
for (var i in e) {
var t=e[i], n=+t
if (n == t)
s.push(n)
else {
var o2=s.pop(), o1=s.pop()
switch (t) {
case '+': s.push(o1+o2); break;
case '-': s.push(o1-o2); break;
case '*': s.push(o1*o2); break;
case '/': s.push(o1/o2); break;
case '^': s.push(Math.pow(o1,o2)); break;
}
}
document.write(t, ': ', s, '<br>')
}

View file

@ -0,0 +1,33 @@
var e = '3 4 2 * 1 5 - 2 3 ^ ^ / +'
eval: {
document.write(e, '<br>')
var s=[], e=e.split(' ')
for (var i in e) {
var t=e[i], n=+t
if (!t) continue
if (n == t)
s.push(n)
else {
if ('+-*/^'.indexOf(t) == -1) {
document.write(t, ': ', s, '<br>', 'Unknown operator!<br>')
break eval
}
if (s.length<2) {
document.write(t, ': ', s, '<br>', 'Insufficient operands!<br>')
break eval
}
var o2=s.pop(), o1=s.pop()
switch (t) {
case '+': s.push(o1+o2); break
case '-': s.push(o1-o2); break
case '*': s.push(o1*o2); break
case '/': s.push(o1/o2); break
case '^': s.push(Math.pow(o1,o2))
}
}
document.write(t, ': ', s, '<br>')
}
if (s.length>1) {
document.write('Insufficient operators!<br>')
}
}

View file

@ -0,0 +1,12 @@
calc[rpn_] :=
Module[{tokens = StringSplit[rpn], steps},
steps = FoldList[
Switch[#2, _?DigitQ, Append[#, FromDigits[#2]], "^",
Append[#[[;; -3]], #[[-2]]^#[[-1]]], "*",
Append[#[[;; -3]], #[[-2]] #[[-1]]], "/",
Append[#[[;; -3]], #[[-2]]/#[[-1]]], "+",
Append[#[[;; -3]], #[[-2]] + #[[-1]]], "-",
Append[#[[;; -3]], #[[-2]] - #[[-1]]]] &, {}, tokens][[2 ;;]];
Grid[Transpose[{# <> ":" & /@ tokens,
StringRiffle[ToString[#, InputForm] & /@ #] & /@ steps}]]];
Print[calc["3 4 2 * 1 5 - 2 3 ^ ^ / +"]];

View file

@ -2,7 +2,7 @@ my $proggie = '3 4 2 * 1 5 - 2 3 ^ ^ / +';
class RPN is Array {
method binop(&infix:<op>) { self.push: self.pop Rop self.pop }
method binop(&op) { self.push: self.pop R[&op] self.pop }
method run($p) {
for $p.words {

View file

@ -0,0 +1,40 @@
object RPN {
val PRINT_STACK_CONTENTS: Boolean = true
def main(args: Array[String]): Unit = {
val result = evaluate("3 4 2 * 1 5 - 2 3 ^ ^ / +".split(" ").toList)
println("Answer: " + result)
}
def evaluate(tokens: List[String]): Double = {
import scala.collection.mutable.Stack
val stack: Stack[Double] = new Stack[Double]
for (token <- tokens) {
if (isOperator(token)) token match {
case "+" => stack.push(stack.pop + stack.pop)
case "-" => val x = stack.pop; stack.push(stack.pop - x)
case "*" => stack.push(stack.pop * stack.pop)
case "/" => val x = stack.pop; stack.push(stack.pop / x)
case "^" => val x = stack.pop; stack.push(math.pow(stack.pop, x))
case _ => throw new RuntimeException( s""""$token" is not an operator""")
}
else stack.push(token.toDouble)
if (PRINT_STACK_CONTENTS) {
print("Input: " + token)
print(" Stack: ")
for (element <- stack.seq.reverse) print(element + " ");
println("")
}
}
stack.pop
}
def isOperator(token: String): Boolean = {
token match {
case "+" => true; case "-" => true; case "*" => true; case "/" => true; case "^" => true
case _ => false
}
}
}