tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,43 @@
import std.stdio, std.string, std.array;
void parseRPN(in string e) {
enum nPrec = 9;
static struct Info { int prec; bool rAssoc; }
immutable /*static*/ opa = ["^": Info(4, true),
"*": Info(3, false),
"/": Info(3, false),
"+": Info(2, false),
"-": Info(2, false)];
writeln("\nPostfix input: ", e);
static struct Sf { int prec; string expr; }
Sf[] stack;
foreach (immutable tok; e.split()) {
writeln("Token: ", tok);
if (tok in opa) {
immutable op = opa[tok];
immutable rhs = stack.back;
stack.popBack();
auto lhs = &stack.back;
if (lhs.prec < op.prec ||
(lhs.prec == op.prec && op.rAssoc))
lhs.expr = "(" ~ lhs.expr ~ ")";
lhs.expr ~= " " ~ tok ~ " ";
lhs.expr ~= (rhs.prec < op.prec ||
(rhs.prec == op.prec && !op.rAssoc)) ?
"(" ~ rhs.expr ~ ")" :
rhs.expr;
lhs.prec = op.prec;
} else
stack ~= Sf(nPrec, tok);
foreach (immutable f; stack)
writefln(` %d "%s"`, f.prec, f.expr);
}
writeln("Infix result: ", stack[0].expr);
}
void main() {
foreach (immutable test; ["3 4 2 * 1 5 - 2 3 ^ ^ / +",
"1 2 + 3 4 + ^ 5 6 + ^"])
parseRPN(test);
}