PageMac
Home
   dot Projects
   dot Pics and Images
   dot WREK
D++
   dot About / FAQ
   dot Download
   dot Code Library
   dot Documentation
   dot VB6 Runtime Files
About

  Source Code - dpp/source/rpn.dpl

Download link (right click, save as)



1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
' - D++ Reverse-Polish Notation Library
' - Written by AzureIce (www.pagemac.com)
' - Based on the D++ engine itself. :)
' - Can solve any math expression.  Only supports
'   the primary operations as of now, but more can
'   be added easily.
' - #include(rpn.dpl) in your file, and call the RPNsolve()
'   function to use it.

newvar stack[], stacksize;
newvar ops[3], parens[1];
newvar isInitiated;

function initiateVars()
{
    newvar expression, temp;
    ops[0] = "+"; ops[1] = "-"; 
    ops[2] = "*"; ops[3] = "/";
    parens[0] = "("; parens[1] = ")";
    isInitiated = true;
}

function RPNsolve(expression)
{
    if ! isInitiated then initiateVars(); endif
    newvar temp[], i;
    temp = converttorpn(expression);
    RPNsolve = calculaterpn(temp);
}

function converttorpn[] (expression);
{
    newvar tokens[], finalProduct[];
    newvar tokenCount, n;

    expression = "( " & expression & " )";
    'tokens = split(expression, " ");
    tokens = tokenize(expression);
    resize finalproduct, ubound(tokens);
    clearstack();

    for n=0 to ubound(tokens);
        if tokens[n] = "(" then
            push("(");
        elseif tokens[n] = ")" then
            do while peek() <> "(";
                finalProduct[tokencount] = pop();
                tokenCount++;
            loop
            if peek() = "(" then pop(); endif
        elseif isequalany(tokens[n], ops) then
            do while 1=1;
                if stacksize = 0 then
                    push(tokens[n]);
                    exit_do;
                elseif isequalany(peek(), parens) then
                    push(tokens[n]);
                    exit_do;
                elseif getpval(peek()) < getpval(tokens[n]) then
                    push(tokens[n]);
                    exit_do;
                else
                    finalProduct[tokencount] = pop();
                    tokenCount++;
                endif
            loop
        else
            finalproduct[tokencount] = tokens[n];
            tokencount++;
        endif
    next n;

    resize preserve finalproduct, tokencount-1;
    converttorpn = finalproduct;
}

function calculaterpn(expression[])
{
    newvar tokens[], n;
    newvar temp;
    clearstack();
    tokens = expression;

    for n = 0 to ubound(tokens);
        if tokens[n] = "+" then
            push(val(pop()) + val(pop()));
        elseif tokens[n] = "-" then
            push(val(pop()) - val(pop()));
        elseif tokens[n] = "*" then
            push(val(pop()) * val(pop()));
        elseif tokens[n] = "/" then
            temp = pop();
            push(val(pop()) / val(temp));
        else
            push(tokens[n]);
        endif
    next n;

    if stacksize > 1 then
        return "Invalid Expression";
    else
        return pop();
    endif
}

function tokenize[] (expression)
{
    newvar i, j, sChar;
    newvar tokenArray[], tokenCount;

    resize tokenArray, len(expression);

    for i = 1 to len(expression);
        sChar = mid(expression, i, 1);
        if isequalany(sChar, ops) then
            tokenArray[tokencount] = sChar;
            tokencount++;
        elseif isnumeric(sChar) then
            j = i;
            do until j > len(expression);
                if isnumeric(mid(expression, j, 1)) = false then
                    exit_do;
                endif
                j++;
            loop
            tokenArray[tokenCount] = mid(expression, i, j-i);
            tokencount++;
            i = j - 1;
        elseif isequalany(sChar, parens) then
            tokenArray[tokenCount] = sChar;
            tokencount++;
        endif
    next i;
    resize preserve tokenArray, tokencount - 1;

    return tokenArray;
}

function push(item)
{
    if stacksize = 0 then
        resize preserve stack, 1;
    else
        resize preserve stack, ubound(stack)+1;
    endif
    stacksize++;
    stack[stacksize] = item;
}

function peek()
{
    peek = stack[stacksize];
}

function pop()
{
    pop = stack[stacksize];
    stacksize--;
}

function clearstack()
{
    stacksize = 0;
}

function getpval(thing)
{
    if thing = "+" then
        getpval = 1;
    elseif thing = "-" then
        getpval = 1;
    elseif thing = "*" then
        getpval = 2;
    elseif thing = "/" then
        getpval = 2;
    else
        getpval = 5;
    endif
}

function isequalany(var1, var2[])
{
    newvar i;
    for i = 0 to ubound(var2);
        if var1 = var2[i] then
            return true;
        endif
    next i;
    return false;
}





This code is Copyright (C) 2000-2012 PageMac Programming. Reproduction of this code must adhere to the GNU General Public License.