Created
September 10, 2015 13:49
-
-
Save jkhoeini/b8a8ac8478741b24b9ad to your computer and use it in GitHub Desktop.
Revisions
-
jkhoeini created this gist
Sep 10, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ // RPN calculator import std.algorithm, std.container.array, std.conv, std.stdio, std.meta; void main() { Array!int stack; void binop(string op)() { stack[$ - 2] = mixin("stack[$ - 2] " ~ op ~ " stack[$ - 1]"); stack.removeBack(); writeln(stack[$ - 1]); } void process(in char[] token) { alias Ops = AliasSeq!("+", "-", "*", "/", "%"); Lswitch: switch (token) { foreach (op; Ops) { case op: binop!op(); break Lswitch; } case "=": writeln(stack[$ - 1]); stack.removeBack(); break; default: stack.insertBack(token.to!int); break; } } stdin.byLine.map!splitter.joiner.each!process; }