get alist(push,"_polish",the value of alist(pop,"_polish",2,type))
else if (type = "-") then
put alist(pop, "_polish") into op2
get alist(push, "_polish", alist(pop, "_polish") - op2)
else if (type = "/") then
put alist(pop, "_polish") into op2
get alist(push, "_polish", alist(pop, "_polish") / op2)
end if
else if (type = "=") then
-- return top value (result of last calculation)
if (alist(size, "_polish") < 1) then return "ERROR: STACK EMPTY"
return alist(top, "_polish")
else if (type = "c") then
-- clear calculator
get alist(dispose, "_polish")
get alist(new, "_polish")
return empty
else
return "ERROR: ILLEGAL TOKEN"
end if
end repeat
end polish
-- part 1 (field)
-- low flags: 00
-- high flags: 0002
-- rect: left=260 top=242 right=269 bottom=509
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: Expression
----- HyperTalk script -----
on enterInField
calculatorExprClick "Expression"
end enterInField
on returnInField
enterInField
end returnInField
-- part 2 (field)
-- low flags: 01
-- high flags: 0002
-- rect: left=260 top=186 right=241 bottom=509
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 3
-- text size: 10
-- style flags: 256
-- line height: 13
-- part name:
----- HyperTalk script -----
on enterInField
set cursor to watch
put polish(target) into card field result
end enterInField
on returnInField
if (commandKey() = down) then enterInField
else pass returnInField
end returnInField
-- part 3 (field)
-- low flags: 00
-- high flags: 0002
-- rect: left=271 top=34 right=54 bottom=362
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 65535
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 15
-- part name: Result
----- HyperTalk script -----
on closeField
calculatorExprClick "Result"
end closeField
on enterInField
closeField
end enterInField
on returnInField
closeField
end returnInField
-- part 7 (field)
-- low flags: 81
-- high flags: 4007
-- rect: left=377 top=61 right=174 bottom=479
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: Stack
-- part 8 (button)
-- low flags: 00
-- high flags: A003
-- rect: left=378 top=33 right=55 bottom=478
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: Show Stack
----- HyperTalk script -----
on mouseUp
if (the visible of card field "Stack" = false) then
show card field "Stack"
showCalculatorStack
set the name of the target to "Hide Stack"
else
hide card field "Stack"
set the name of the target to "Show Stack"
end if
end mouseUp
-- part 9 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=272 top=60 right=78 bottom=293
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: C
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 10 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=295 top=60 right=78 bottom=316
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: =
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 11 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=318 top=60 right=78 bottom=339
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: /
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 12 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=341 top=60 right=78 bottom=362
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: *
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 13 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=272 top=82 right=100 bottom=293
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 7
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 14 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=295 top=82 right=100 bottom=316
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 8
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 15 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=318 top=82 right=100 bottom=339
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 9
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 16 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=341 top=82 right=100 bottom=362
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: -
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 17 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=272 top=104 right=122 bottom=293
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 4
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 18 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=295 top=104 right=122 bottom=316
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 5
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 19 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=318 top=104 right=122 bottom=339
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 6
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 20 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=341 top=104 right=122 bottom=362
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: +
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 21 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=273 top=148 right=166 bottom=316
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 0
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 22 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=318 top=126 right=144 bottom=339
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 3
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 23 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=295 top=126 right=144 bottom=316
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 2
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 24 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=272 top=126 right=144 bottom=293
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 2
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 25 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=272 top=126 right=144 bottom=293
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: 1
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 26 (button)
-- low flags: 00
-- high flags: 2000
-- rect: left=341 top=126 right=166 bottom=362
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: Enter
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part 27 (button)
-- low flags: 00
-- high flags: A004
-- rect: left=318 top=148 right=166 bottom=339
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: .
----- HyperTalk script -----
on mouseUp
calculatorClick the short name of the target
end mouseUp
-- part contents for background part 9
----- text -----
DemoStack 0.9
-- part contents for background part 1
----- text -----
Reverse Polish
-- part contents for background part 12
----- text -----
Card #5
-- part contents for card part 2
----- text -----
Use the calculator above or enter a complete reverse Polish expression into the field below and press Enter or Return:
-- part contents for background part 14
----- text -----
This card implements a reverse Polish calculator using ArrayList. Reverse Polish notation differs from the more common infix notation in that the operators come after the operands, and not between them. For instance, the infix expression
(1 + 2) * (3 + 5) =
would be written
1 2 + 3 5 + * =
in reverse Polish. Notice that there is no need for parentheses.
________________________________
Using the Calculator
You can use the keys on the calculator to enter an expression. The 'C' key clears the stack used by the calculator; the Enter key is used after a number is entered. Keying in values will take a while to get used to since you can't press an operator (+,-,*,/) unless there are already two numbers on the stack. However, if there is at least one number on the stack and another number in the calculator's display then you can press one of the operator keys directly, without having to press the Enter key first. For instance, you could press the keys:
4 Enter 5 Enter 67 Enter - -
to get 58. You can get the same result by keying in:
4 Enter 5 Enter 67 - -
The "Show Stack" button can be used to display the contents of the calculator's stack. As an expression is evaluated you can actually see the stack grow and shrink.
Expressions can also be entered either into the calculator's display or into the field below the calculator. Each term of an expression must be separated with a space. For example, the expression
55 23 / 4 5 + *
will give the result 21.521736. The calculator recognizes the operations +, -, *, /, = (to print the result), and c (to clear the stack).
I don't think this script is very useful. I included it merely to illustrate the stack operations that ArrayList provides.