home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / prctclxf.sit / DemoStack / card_4012.txt < prev    next >
Text File  |  1990-08-28  |  16KB  |  652 lines

  1. -- card: 4012 from stack: in
  2. -- bmap block id: 3519
  3. -- flags: 4000
  4. -- background id: 2685
  5. -- name: Reverse Polish
  6. ----- HyperTalk script -----
  7. on openCard
  8.   if (debugging() = true) then pass openCard
  9.   put empty into card field "Result"
  10.   put empty into card field "Stack"
  11.   get alist(dispose, "_polish")
  12.   get alist(new, "_polish")
  13.   pass openCard
  14. end openCard
  15.  
  16. on closeCard
  17.   if (debugging() = true) then pass closeCard
  18.   get alist(dispose, "_polish")
  19.   pass closeCard
  20. end closeCard
  21.  
  22. ---------------------------------------------------------------------
  23. -- Handlers for entering data into a reverse Polish calculator
  24. ---------------------------------------------------------------------
  25.  
  26. -- Call this function when a complete expression is entered into
  27. -- a field. Each token must be a separate word.
  28. on calculatorExprClick exprField
  29.   set cursor to watch
  30.   calculatorEvaluate card field exprField
  31. end calculatorExprClick
  32.  
  33. -- Enter token into Polish calculator
  34. on calculatorClick token
  35.   global calculatorStatus
  36.  
  37.   -- Clear result if last expression ended
  38.   if (calculatorStatus Γëá empty) then
  39.     put empty into calculatorStatus
  40.     put empty into card field "Result"
  41.   end if
  42.  
  43.   put char 1 of token into type
  44.  
  45.   -- Enter number
  46.   if ( ((type >= 0) and (type <= 9)) or ((type=".") and (type is not in card field "Result")) ) then
  47.     put token after card field "Result"
  48.   else
  49.     -- Build expression for Polish function to evaluate
  50.     if (token = "Enter") then put card field "Result" into token
  51.     else if (token is in "+,-,*,/") then
  52.       put card field "Result" & " " before token
  53.       put " =" after token
  54.     end if
  55.     if (token = "c") then put " =" after token
  56.  
  57.     calculatorEvaluate token
  58.   end if
  59. end calculatorClick
  60.  
  61. -- Evaluate expression
  62. on calculatorEvaluate expr
  63.   global calculatorStatus
  64.  
  65.   get polish(expr)
  66.   if (it contains "ERROR") then
  67.     delete word 1 of it
  68.     put "ERROR" into calculatorStatus
  69.   else put "EVAL" into calculatorStatus
  70.   put it into card field "Result"
  71.   showCalculatorStack
  72. end calculatorEvaluate
  73.  
  74. on showCalculatorStack
  75.   if (the visible of card field "Stack" = true) then
  76.     get alist(top, "_polish", alist(size, "_polish"), return)
  77.     put it into card field "Stack"
  78.   end if
  79. end showCalculatorStack
  80.  
  81. ---------------------------------------------------------------------
  82. -- Reverse Polish calculator in HyperCard! I really don't think this
  83. -- is a very useful program. The expression parameter is a list of words
  84. -- containing either a number or one of the implemented operators.
  85. ---------------------------------------------------------------------
  86. function polish expr
  87. repeat with i=1 to number(words in expr)
  88.   -- get current token and type
  89.   put word i of expr into token
  90.   put char 1 of token into type
  91.   -- push numbers
  92.   if (type >= 0 and type <= 9 or type = ".") then
  93.     get alist(push, "_polish", token)
  94.   else if (type is in "+-*/") then
  95.     -- evaluate the binary operator
  96.     if (alist(size, "_polish") < 2) then
  97.       return "ERROR: STACK EMPTY"
  98.     else if (type = "+" or type = "*") then
  99.       get alist(push,"_polish",the value of alist(pop,"_polish",2,type))
  100.     else if (type = "-") then
  101.       put alist(pop, "_polish") into op2
  102.       get alist(push, "_polish", alist(pop, "_polish") - op2)
  103.     else if (type = "/") then
  104.       put alist(pop, "_polish") into op2
  105.       get alist(push, "_polish", alist(pop, "_polish") / op2)
  106.     end if
  107.   else if (type = "=") then
  108.     -- return top value (result of last calculation)
  109.     if (alist(size, "_polish") < 1) then return "ERROR: STACK EMPTY"
  110.     return alist(top, "_polish")
  111.   else if (type = "c") then
  112.     -- clear calculator
  113.     get alist(dispose, "_polish")
  114.     get alist(new, "_polish")
  115.     return empty
  116.   else
  117.     return "ERROR: ILLEGAL TOKEN"
  118.   end if
  119. end repeat
  120. end polish
  121.  
  122.  
  123.  
  124.  
  125. -- part 1 (field)
  126. -- low flags: 00
  127. -- high flags: 0002
  128. -- rect: left=260 top=242 right=269 bottom=509
  129. -- title width / last selected line: 0
  130. -- icon id / first selected line: 0 / 0
  131. -- text alignment: 0
  132. -- font id: 4
  133. -- text size: 9
  134. -- style flags: 0
  135. -- line height: 12
  136. -- part name: Expression
  137. ----- HyperTalk script -----
  138. on enterInField
  139.   calculatorExprClick "Expression"
  140. end enterInField
  141.  
  142. on returnInField
  143.   enterInField
  144. end returnInField
  145.  
  146.  
  147.  
  148.  
  149.  
  150. -- part 2 (field)
  151. -- low flags: 01
  152. -- high flags: 0002
  153. -- rect: left=260 top=186 right=241 bottom=509
  154. -- title width / last selected line: 0
  155. -- icon id / first selected line: 0 / 0
  156. -- text alignment: 0
  157. -- font id: 3
  158. -- text size: 10
  159. -- style flags: 256
  160. -- line height: 13
  161. -- part name: 
  162. ----- HyperTalk script -----
  163. on enterInField
  164.   set cursor to watch
  165.   put polish(target) into card field result
  166. end enterInField
  167.  
  168. on returnInField
  169.   if (commandKey() = down) then enterInField
  170.   else pass returnInField
  171. end returnInField
  172.  
  173.  
  174.  
  175.  
  176. -- part 3 (field)
  177. -- low flags: 00
  178. -- high flags: 0002
  179. -- rect: left=271 top=34 right=54 bottom=362
  180. -- title width / last selected line: 0
  181. -- icon id / first selected line: 0 / 0
  182. -- text alignment: 65535
  183. -- font id: 4
  184. -- text size: 9
  185. -- style flags: 0
  186. -- line height: 15
  187. -- part name: Result
  188. ----- HyperTalk script -----
  189. on closeField
  190.   calculatorExprClick "Result"
  191. end closeField
  192.  
  193. on enterInField
  194.   closeField
  195. end enterInField
  196.  
  197. on returnInField
  198.   closeField
  199. end returnInField
  200.  
  201.  
  202. -- part 7 (field)
  203. -- low flags: 81
  204. -- high flags: 4007
  205. -- rect: left=377 top=61 right=174 bottom=479
  206. -- title width / last selected line: 0
  207. -- icon id / first selected line: 0 / 0
  208. -- text alignment: 0
  209. -- font id: 4
  210. -- text size: 9
  211. -- style flags: 0
  212. -- line height: 12
  213. -- part name: Stack
  214.  
  215.  
  216. -- part 8 (button)
  217. -- low flags: 00
  218. -- high flags: A003
  219. -- rect: left=378 top=33 right=55 bottom=478
  220. -- title width / last selected line: 0
  221. -- icon id / first selected line: 0 / 0
  222. -- text alignment: 1
  223. -- font id: 0
  224. -- text size: 12
  225. -- style flags: 0
  226. -- line height: 16
  227. -- part name: Show Stack
  228. ----- HyperTalk script -----
  229. on mouseUp
  230.   if (the visible of card field "Stack" = false) then
  231.     show card field "Stack"
  232.     showCalculatorStack
  233.     set the name of the target to "Hide Stack"
  234.   else
  235.     hide card field "Stack"
  236.     set the name of the target to "Show Stack"
  237.   end if
  238. end mouseUp
  239.  
  240.  
  241.  
  242. -- part 9 (button)
  243. -- low flags: 00
  244. -- high flags: A004
  245. -- rect: left=272 top=60 right=78 bottom=293
  246. -- title width / last selected line: 0
  247. -- icon id / first selected line: 0 / 0
  248. -- text alignment: 1
  249. -- font id: 4
  250. -- text size: 9
  251. -- style flags: 0
  252. -- line height: 12
  253. -- part name: C
  254. ----- HyperTalk script -----
  255. on mouseUp
  256.   calculatorClick the short name of the target
  257. end mouseUp
  258.  
  259.  
  260.  
  261. -- part 10 (button)
  262. -- low flags: 00
  263. -- high flags: A004
  264. -- rect: left=295 top=60 right=78 bottom=316
  265. -- title width / last selected line: 0
  266. -- icon id / first selected line: 0 / 0
  267. -- text alignment: 1
  268. -- font id: 4
  269. -- text size: 9
  270. -- style flags: 0
  271. -- line height: 12
  272. -- part name: =
  273. ----- HyperTalk script -----
  274. on mouseUp
  275.   calculatorClick the short name of the target
  276. end mouseUp
  277.  
  278.  
  279.  
  280. -- part 11 (button)
  281. -- low flags: 00
  282. -- high flags: A004
  283. -- rect: left=318 top=60 right=78 bottom=339
  284. -- title width / last selected line: 0
  285. -- icon id / first selected line: 0 / 0
  286. -- text alignment: 1
  287. -- font id: 4
  288. -- text size: 9
  289. -- style flags: 0
  290. -- line height: 12
  291. -- part name: /
  292. ----- HyperTalk script -----
  293. on mouseUp
  294.   calculatorClick the short name of the target
  295. end mouseUp
  296.  
  297.  
  298.  
  299. -- part 12 (button)
  300. -- low flags: 00
  301. -- high flags: A004
  302. -- rect: left=341 top=60 right=78 bottom=362
  303. -- title width / last selected line: 0
  304. -- icon id / first selected line: 0 / 0
  305. -- text alignment: 1
  306. -- font id: 4
  307. -- text size: 9
  308. -- style flags: 0
  309. -- line height: 12
  310. -- part name: *
  311. ----- HyperTalk script -----
  312. on mouseUp
  313.   calculatorClick the short name of the target
  314. end mouseUp
  315.  
  316.  
  317.  
  318. -- part 13 (button)
  319. -- low flags: 00
  320. -- high flags: A004
  321. -- rect: left=272 top=82 right=100 bottom=293
  322. -- title width / last selected line: 0
  323. -- icon id / first selected line: 0 / 0
  324. -- text alignment: 1
  325. -- font id: 4
  326. -- text size: 9
  327. -- style flags: 0
  328. -- line height: 12
  329. -- part name: 7
  330. ----- HyperTalk script -----
  331. on mouseUp
  332.   calculatorClick the short name of the target
  333. end mouseUp
  334.  
  335.  
  336.  
  337. -- part 14 (button)
  338. -- low flags: 00
  339. -- high flags: A004
  340. -- rect: left=295 top=82 right=100 bottom=316
  341. -- title width / last selected line: 0
  342. -- icon id / first selected line: 0 / 0
  343. -- text alignment: 1
  344. -- font id: 4
  345. -- text size: 9
  346. -- style flags: 0
  347. -- line height: 12
  348. -- part name: 8
  349. ----- HyperTalk script -----
  350. on mouseUp
  351.   calculatorClick the short name of the target
  352. end mouseUp
  353.  
  354.  
  355.  
  356. -- part 15 (button)
  357. -- low flags: 00
  358. -- high flags: A004
  359. -- rect: left=318 top=82 right=100 bottom=339
  360. -- title width / last selected line: 0
  361. -- icon id / first selected line: 0 / 0
  362. -- text alignment: 1
  363. -- font id: 4
  364. -- text size: 9
  365. -- style flags: 0
  366. -- line height: 12
  367. -- part name: 9
  368. ----- HyperTalk script -----
  369. on mouseUp
  370.   calculatorClick the short name of the target
  371. end mouseUp
  372.  
  373.  
  374.  
  375. -- part 16 (button)
  376. -- low flags: 00
  377. -- high flags: A004
  378. -- rect: left=341 top=82 right=100 bottom=362
  379. -- title width / last selected line: 0
  380. -- icon id / first selected line: 0 / 0
  381. -- text alignment: 1
  382. -- font id: 4
  383. -- text size: 9
  384. -- style flags: 0
  385. -- line height: 12
  386. -- part name: -
  387. ----- HyperTalk script -----
  388. on mouseUp
  389.   calculatorClick the short name of the target
  390. end mouseUp
  391.  
  392.  
  393.  
  394. -- part 17 (button)
  395. -- low flags: 00
  396. -- high flags: A004
  397. -- rect: left=272 top=104 right=122 bottom=293
  398. -- title width / last selected line: 0
  399. -- icon id / first selected line: 0 / 0
  400. -- text alignment: 1
  401. -- font id: 4
  402. -- text size: 9
  403. -- style flags: 0
  404. -- line height: 12
  405. -- part name: 4
  406. ----- HyperTalk script -----
  407. on mouseUp
  408.   calculatorClick the short name of the target
  409. end mouseUp
  410.  
  411.  
  412.  
  413. -- part 18 (button)
  414. -- low flags: 00
  415. -- high flags: A004
  416. -- rect: left=295 top=104 right=122 bottom=316
  417. -- title width / last selected line: 0
  418. -- icon id / first selected line: 0 / 0
  419. -- text alignment: 1
  420. -- font id: 4
  421. -- text size: 9
  422. -- style flags: 0
  423. -- line height: 12
  424. -- part name: 5
  425. ----- HyperTalk script -----
  426. on mouseUp
  427.   calculatorClick the short name of the target
  428. end mouseUp
  429.  
  430.  
  431.  
  432. -- part 19 (button)
  433. -- low flags: 00
  434. -- high flags: A004
  435. -- rect: left=318 top=104 right=122 bottom=339
  436. -- title width / last selected line: 0
  437. -- icon id / first selected line: 0 / 0
  438. -- text alignment: 1
  439. -- font id: 4
  440. -- text size: 9
  441. -- style flags: 0
  442. -- line height: 12
  443. -- part name: 6
  444. ----- HyperTalk script -----
  445. on mouseUp
  446.   calculatorClick the short name of the target
  447. end mouseUp
  448.  
  449.  
  450.  
  451. -- part 20 (button)
  452. -- low flags: 00
  453. -- high flags: A004
  454. -- rect: left=341 top=104 right=122 bottom=362
  455. -- title width / last selected line: 0
  456. -- icon id / first selected line: 0 / 0
  457. -- text alignment: 1
  458. -- font id: 4
  459. -- text size: 9
  460. -- style flags: 0
  461. -- line height: 12
  462. -- part name: +
  463. ----- HyperTalk script -----
  464. on mouseUp
  465.   calculatorClick the short name of the target
  466. end mouseUp
  467.  
  468.  
  469.  
  470. -- part 21 (button)
  471. -- low flags: 00
  472. -- high flags: A004
  473. -- rect: left=273 top=148 right=166 bottom=316
  474. -- title width / last selected line: 0
  475. -- icon id / first selected line: 0 / 0
  476. -- text alignment: 1
  477. -- font id: 4
  478. -- text size: 9
  479. -- style flags: 0
  480. -- line height: 12
  481. -- part name: 0
  482. ----- HyperTalk script -----
  483. on mouseUp
  484.   calculatorClick the short name of the target
  485. end mouseUp
  486.  
  487.  
  488.  
  489. -- part 22 (button)
  490. -- low flags: 00
  491. -- high flags: A004
  492. -- rect: left=318 top=126 right=144 bottom=339
  493. -- title width / last selected line: 0
  494. -- icon id / first selected line: 0 / 0
  495. -- text alignment: 1
  496. -- font id: 4
  497. -- text size: 9
  498. -- style flags: 0
  499. -- line height: 12
  500. -- part name: 3
  501. ----- HyperTalk script -----
  502. on mouseUp
  503.   calculatorClick the short name of the target
  504. end mouseUp
  505.  
  506.  
  507.  
  508. -- part 23 (button)
  509. -- low flags: 00
  510. -- high flags: A004
  511. -- rect: left=295 top=126 right=144 bottom=316
  512. -- title width / last selected line: 0
  513. -- icon id / first selected line: 0 / 0
  514. -- text alignment: 1
  515. -- font id: 4
  516. -- text size: 9
  517. -- style flags: 0
  518. -- line height: 12
  519. -- part name: 2
  520. ----- HyperTalk script -----
  521. on mouseUp
  522.   calculatorClick the short name of the target
  523. end mouseUp
  524.  
  525.  
  526.  
  527. -- part 24 (button)
  528. -- low flags: 00
  529. -- high flags: A004
  530. -- rect: left=272 top=126 right=144 bottom=293
  531. -- title width / last selected line: 0
  532. -- icon id / first selected line: 0 / 0
  533. -- text alignment: 1
  534. -- font id: 4
  535. -- text size: 9
  536. -- style flags: 0
  537. -- line height: 12
  538. -- part name: 2
  539. ----- HyperTalk script -----
  540. on mouseUp
  541.   calculatorClick the short name of the target
  542. end mouseUp
  543.  
  544.  
  545.  
  546. -- part 25 (button)
  547. -- low flags: 00
  548. -- high flags: A004
  549. -- rect: left=272 top=126 right=144 bottom=293
  550. -- title width / last selected line: 0
  551. -- icon id / first selected line: 0 / 0
  552. -- text alignment: 1
  553. -- font id: 4
  554. -- text size: 9
  555. -- style flags: 0
  556. -- line height: 12
  557. -- part name: 1
  558. ----- HyperTalk script -----
  559. on mouseUp
  560.   calculatorClick the short name of the target
  561. end mouseUp
  562.  
  563.  
  564.  
  565. -- part 26 (button)
  566. -- low flags: 00
  567. -- high flags: 2000
  568. -- rect: left=341 top=126 right=166 bottom=362
  569. -- title width / last selected line: 0
  570. -- icon id / first selected line: 0 / 0
  571. -- text alignment: 1
  572. -- font id: 4
  573. -- text size: 9
  574. -- style flags: 0
  575. -- line height: 12
  576. -- part name: Enter
  577. ----- HyperTalk script -----
  578. on mouseUp
  579.   calculatorClick the short name of the target
  580. end mouseUp
  581.  
  582.  
  583.  
  584. -- part 27 (button)
  585. -- low flags: 00
  586. -- high flags: A004
  587. -- rect: left=318 top=148 right=166 bottom=339
  588. -- title width / last selected line: 0
  589. -- icon id / first selected line: 0 / 0
  590. -- text alignment: 1
  591. -- font id: 4
  592. -- text size: 9
  593. -- style flags: 0
  594. -- line height: 12
  595. -- part name: .
  596. ----- HyperTalk script -----
  597. on mouseUp
  598.   calculatorClick the short name of the target
  599. end mouseUp
  600.  
  601.  
  602.  
  603. -- part contents for background part 9
  604. ----- text -----
  605. DemoStack 0.9
  606.  
  607. -- part contents for background part 1
  608. ----- text -----
  609. Reverse Polish
  610.  
  611. -- part contents for background part 12
  612. ----- text -----
  613. Card #5
  614.  
  615. -- part contents for card part 2
  616. ----- text -----
  617. Use the calculator above or enter a complete reverse Polish expression into the field below and press Enter or Return:
  618.  
  619. -- part contents for background part 14
  620. ----- text -----
  621.  
  622.  
  623. 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
  624.    (1 + 2) * (3 + 5) =
  625. would be written
  626.    1 2 + 3 5 + * =
  627. in reverse Polish. Notice that there is no need for parentheses.
  628.  
  629. ________________________________
  630. Using the Calculator
  631.  
  632. 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:
  633.    4 Enter 5 Enter 67 Enter - - 
  634. to get 58. You can get the same result by keying in:
  635.    4 Enter 5 Enter 67 - -
  636.  
  637. 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.
  638.  
  639. 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
  640.    55 23 / 4 5 + *
  641. will give the result 21.521736. The calculator recognizes the operations +, -, *, /, = (to print the result), and c (to clear the stack).
  642.  
  643. I don't think this script is very useful. I included it merely to illustrate the stack operations that ArrayList provides.
  644.  
  645. -- part contents for background part 13
  646. ----- text -----
  647. Description
  648.  
  649. -- part contents for card part 1
  650. ----- text -----
  651. 1 2 + 3 5 + * 2 / 4 - =
  652.