June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,39 @@
|
|||
estack = [];
|
||||
|
||||
epush(x) = {
|
||||
estack = vector(#estack + 1, i, if(i <= #estack, estack[i], x));
|
||||
return(#estack);
|
||||
};
|
||||
|
||||
epop() = {
|
||||
local(val = estack[#estack]);
|
||||
estack = vector(#estack - 1, i, estack[i]);
|
||||
return(val);
|
||||
};
|
||||
|
||||
registerRPNToken(t) = {
|
||||
local(o1, o2);
|
||||
|
||||
if(type(t) == "t_STR",
|
||||
if(t == "+", o2 = epop(); o1 = epop(); epush(o1 + o2),
|
||||
if(t == "-", o2 = epop(); o1 = epop(); epush(o1 - o2),
|
||||
if(t == "*", o2 = epop(); o1 = epop(); epush(o1 * o2),
|
||||
if(t == "/", o2 = epop(); o1 = epop(); epush(o1 / o2),
|
||||
if(t == "%", o2 = epop(); o1 = epop(); epush(o1 % o2),
|
||||
if(t == "^", o2 = epop(); o1 = epop(); epush(o1 ^ o2)
|
||||
)))))),
|
||||
if(type(t) == "t_INT" || type(t) == "t_REAL" || type(t) == "t_FRAC",
|
||||
epush(t))
|
||||
);
|
||||
print(estack);
|
||||
};
|
||||
|
||||
parseRPN(s) = {
|
||||
estack = [];
|
||||
for(i = 1, #s, registerRPNToken(s[i]));
|
||||
|
||||
if(#estack > 1, error("Malformed postfix expression."));
|
||||
return(estack[1]);
|
||||
};
|
||||
|
||||
parseRPN([3, 4, 2, "*", 1, 5, "-", 2, 3, "^", "^", "/", "+"]); \\ Our input expression
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
[3]
|
||||
[3, 4]
|
||||
[3, 4, 2]
|
||||
[3, 8]
|
||||
[3, 8, 1]
|
||||
[3, 8, 1, 5]
|
||||
[3, 8, -4]
|
||||
[3, 8, -4, 2]
|
||||
[3, 8, -4, 2, 3]
|
||||
[3, 8, -4, 8]
|
||||
[3, 8, 65536]
|
||||
[3, 1/8192]
|
||||
[24577/8192]
|
||||
Loading…
Add table
Add a link
Reference in a new issue