Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,10 +1,10 @@
|
|||
import std.stdio, std.string, std.array, std.algorithm;
|
||||
|
||||
void rpmToInfix(in string str) {
|
||||
void rpmToInfix(in string str) @safe {
|
||||
static struct Exp { int p; string e; }
|
||||
immutable P = (in Exp pair, in int prec) pure =>
|
||||
pair.p < prec ? format("( %s )", pair.e) : pair.e;
|
||||
immutable F = (in string[] s...) pure nothrow => s.join(" ");
|
||||
immutable F = (in string[] s...) pure nothrow => s.join(' ');
|
||||
|
||||
writefln("=================\n%s", str);
|
||||
Exp[] stack;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,62 @@
|
|||
public class PostfixToInfix {
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (String e : new String[]{"3 4 2 * 1 5 - 2 3 ^ ^ / +",
|
||||
"1 2 + 3 4 + ^ 5 6 + ^"}) {
|
||||
System.out.printf("Postfix : %s%n", e);
|
||||
System.out.printf("Infix : %s%n", postfixToInfix(e));
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
static String postfixToInfix(final String postfix) {
|
||||
|
||||
class Expression {
|
||||
final static String ops = "-+/*^";
|
||||
|
||||
String op, ex;
|
||||
int prec = 3;
|
||||
|
||||
Expression(String e) {
|
||||
ex = e;
|
||||
}
|
||||
|
||||
Expression(String e1, String e2, String o) {
|
||||
ex = String.format("%s %s %s", e1, o, e2);
|
||||
op = o;
|
||||
prec = ops.indexOf(o) / 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ex;
|
||||
}
|
||||
}
|
||||
|
||||
Stack<Expression> expr = new Stack<>();
|
||||
|
||||
for (String token : postfix.split("\\s")) {
|
||||
char c = token.charAt(0);
|
||||
int idx = Expression.ops.indexOf(c);
|
||||
if (idx != -1 && token.length() == 1) {
|
||||
|
||||
Expression r = expr.pop();
|
||||
Expression l = expr.pop();
|
||||
|
||||
int opPrec = idx / 2;
|
||||
|
||||
if (l.prec < opPrec || (l.prec == opPrec && c == '^'))
|
||||
l.ex = '(' + l.ex + ')';
|
||||
|
||||
if (r.prec < opPrec || (r.prec == opPrec && c != '^'))
|
||||
r.ex = '(' + r.ex + ')';
|
||||
|
||||
expr.push(new Expression(l.ex, r.ex, token));
|
||||
} else {
|
||||
expr.push(new Expression(token));
|
||||
}
|
||||
System.out.printf("%s -> %s%n", token, expr);
|
||||
}
|
||||
return expr.peek().ex;
|
||||
}
|
||||
}
|
||||
|
|
@ -9,28 +9,29 @@ say "infix: " toInfix( "1 2 + 3 4 + ^ 5 6 + ^" ) /* [↓] Deutsch.*/
|
|||
say "infix: " toInfix( "Mond Sterne Schlamm + * Feur Suppe * ^" )
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────────────────────────────────────────*/
|
||||
pop: pop=#; #=#-1; return @.pop
|
||||
push: #=#+1; @.#=arg(1); return
|
||||
pop: pop=#; #=#-1; return @.pop
|
||||
push: #=#+1; @.#=arg(1); return
|
||||
/*──────────────────────────────────────────────────────────────────────*/
|
||||
stack2str: $=; do j=1 for #; _ = @.j; y=left(_,1)
|
||||
if pos(' ',_)==0 then _ = '{'strip(substr(_,2))"}"
|
||||
else _ = substr(_,2)
|
||||
$=$ '{'strip(y _)"}"
|
||||
stack2str: $=; do j=1 for #; _ = @.j; y=left(_,1)
|
||||
if pos(' ', _)==0 then _ = '{'strip(substr(_, 2))"}"
|
||||
else _ = substr(_, 2)
|
||||
$=$ '{'strip(y _)"}"
|
||||
end /*j*/
|
||||
return space($)
|
||||
/*──────────────────────────────────────────────────────────────────────*/
|
||||
toInfix: parse arg rpn; say copies('─',80-1); say 'RPN: ' space(rpn)
|
||||
toInfix: parse arg rpn; say copies('─',80-1); say 'RPN: ' space(rpn)
|
||||
|
||||
do N=1 to words(RPN); ?=word(RPN,N) /*process each of the RPN tokens.*/
|
||||
if pos(?,oS)==0 then call push '¥' ? /*when in doubt, add a Yen to it.*/
|
||||
else do; g=pop(); gp=left(g,1); g=substr(g,2)
|
||||
h=pop(); hp=left(h,1); h=substr(h,2)
|
||||
tp=substr(oP,pos(?,oS),1);ta=substr(oA,pos(?,oS),1)
|
||||
if hp<tp | (hp==tp & ta=='►') then h="("h")"
|
||||
if gp<tp | (gp==tp & ta=='◄') then g="("g")"
|
||||
call push tp || h ? g
|
||||
end
|
||||
if showAction then say right(?,25) "──►" stack2str()
|
||||
end
|
||||
do N=1 for words(RPN); ?=word(RPN,N) /*process each of the RPN tokens.*/
|
||||
if pos(?,oS)==0 then call push '¥' ? /*when in doubt, add a Yen to it.*/
|
||||
else do; g=pop(); gp=left(g, 1); g=substr(g, 2)
|
||||
h=pop(); hp=left(h, 1); h=substr(h, 2)
|
||||
tp=substr(oP,pos(?, oS), 1)
|
||||
ta=substr(oA,pos(?, oS), 1)
|
||||
if hp<tp | (hp==tp & ta=='►') then h="("h")"
|
||||
if gp<tp | (gp==tp & ta=='◄') then g="("g")"
|
||||
call push tp || h ? g
|
||||
end
|
||||
if showAction then say right(?,25) "──►" stack2str()
|
||||
end /*N*/
|
||||
|
||||
return space(substr(pop(),2))
|
||||
return space(substr(pop(), 2))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue