home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / l3p120.seq < prev    next >
Encoding:
Text File  |  1990-04-01  |  2.2 KB  |  67 lines

  1. \ Lesson 3 Part 12  ( F-PC 3.5 Tutorial by Jack Brown )
  2.  
  3. COMMENT:
  4. Problem 3.27
  5. Consider the following silly little program.  Study the program to see
  6. how it works. Then expand this silly game to give more and better
  7. responses.
  8. COMMENT;
  9.  
  10. \ A nasty game for the IBM-PC .
  11. : WHITE  177 EMIT ;
  12. : GAME  CR
  13.         CR  ." Press the space bar as hard as you can!"
  14.         BEGIN CR
  15.         KEY DROP CR 64 RND 1+
  16.         DUP 0 ?DO WHITE LOOP CR
  17.         DUP 25 < IF ." Press it harder!!" ELSE
  18.         DUP 50 < IF ." Not bad! Press real hard!" ELSE
  19.         10 0 DO BEEP LOOP
  20.         DROP ." You just busted your space bar!"
  21.         EXIT THEN THEN
  22.         DROP AGAIN  ;
  23.  
  24. COMMENT:
  25. New Words:  >R  R>  and  R@  for accessing the return stack. These words
  26. are very dangerous!! Do NOT test or execute them interactively if you do
  27. you will crash the Forth system. They can only be used within colon
  28. definitions.
  29.  
  30.  
  31. >R  ( n   -- ) Transfer top data stack item to return stack.
  32. R>  ( --   n ) Transfer top return stack item to data stack.
  33. R@  ( --   n ) Copy     top return stack item to data stack.
  34.  
  35. RULES:
  36. 1. Each use of >R must be balanced with a corresponding R>.
  37. 2. Do not use >R R> and R@ within DO ... LOOPs.  Loop control
  38.    info is kept on the return stack and could be destroyed.
  39.  
  40. Here is a sample program that uses the return stack. The DEPTH of
  41. the stack which is equal to the count of numbers to be averaged is
  42. tucked safely away on the return stack until it is needed.
  43. COMMENT;
  44.  
  45. : AVERAGE  ( x1 x2 ... xn --  avg )
  46.       DEPTH >R R@ 1- 0
  47.         ?DO + LOOP
  48.         CR ." The average of the " R@ . ." numbers is "
  49.         R> / .  CR ;
  50.  
  51. COMMENT:
  52. Problem 3.29
  53. Consider the following simple program that produces a simple bar chart
  54. or histogram from a list of frequencies that are placed on the parameter
  55. stack.
  56. COMMENT;
  57.  
  58. : WHITE  177 EMIT ;
  59. \ Given n frequencies construct histogram or bar chart.
  60. : HISTOGRAM ( f1 f2 ... fn   -- )
  61.         CR DEPTH 0
  62.         ?DO  CR DUP 0 ?DO WHITE LOOP  SPACE .  LOOP CR ;
  63.  
  64. \ Modify HISTOGRAM so that the bars come out in the proper order
  65. \ ( f1 first). Hint: " ROLL "  the stack and display bar.  Clean
  66. \ the stack when finished printing bars.
  67.