home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l3p100 < prev    next >
Text File  |  1990-07-15  |  4KB  |  107 lines

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 3 Part 100  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5. If you would like more information on  IF ... ELSE ... THEN study
  6. Starting Forth 2nd Edition Chapter 4 pp 80-94.  Brodie also has a good
  7. collection of exercises and problems starting on page 94.
  8.  
  9. We are now going to look at a problem that is usually asked very quickly
  10. by people who are familiar some other computer languages.
  11.  
  12.        ┌───────────────────────────────────────────────┐
  13.        │  Where is Forth's word for inputting numbers? │
  14.        └───────────────────────────────────────────────┘
  15.  
  16. The answer is quite easy.  It doesn't have a word for inputting numbers.
  17. The good news is that we can make a word for inputting numbers quite
  18. easily.
  19.  
  20. But first you should recall that the numeric input required for any the
  21. small programs ( Forth words ) that we have written so far was taken
  22. care of by first placing the numeric input values on the parameter
  23. stack.  Thus when we wanted to calculate the volume of a rectangular
  24. tank we would just type:
  25.  
  26. 3 4 5 TANK <enter>
  27.  
  28. The length, width and height input were provided to TANK as stack
  29. inputs.  This is the Forth way of doing things.  Many applications
  30. written in Forth actually provide you with a Forth environment in which
  31. to work so that you can get your work done by executing Forth words that
  32. you provide with stack inputs.  But let's suppose that we want to just
  33. type the name of our program ( Forth word) and have the program prompt
  34. us for the data required.
  35.  
  36. To do this we will have to write a forth word to input numbers.  We will
  37. call this new word #IN .  To implement #IN we are going to steal some
  38. ideas from the outer interpreter MYQUIT that we implemented back in
  39. lesson 1.  Remember the words  QUERY and INTERPRET ?
  40.  
  41. QUERY ( -- )     Get a line of text from the keyboard and save in input
  42.                  buffer.
  43.  
  44. INTERPRET ( -- ) Interpret the line of text in the input buffer.
  45.  
  46. The word for inputting numbers is simply:
  47.  
  48. : #IN ( -- n )   QUERY INTERPRET ;
  49.  
  50. That's all.... That's it!  Let's use it in a program.
  51. Numeric Input using QUERY and INTERPRET and an example
  52.  
  53. \ Super simple numeric input.
  54. :  #IN ( -- n )
  55.       QUERY  INTERPRET ;
  56.  
  57. \ Input length.
  58. : GETL  ( --   l )
  59.         CR ." Enter tank length " #IN ;
  60.  
  61. \ Input width.
  62. : GETW  ( --   w )
  63.         CR ." Enter tank width  " #IN ;
  64.  
  65. \ Input height.
  66. : GETH  ( --   h )
  67.         CR ." Enter tank height " #IN ;
  68.  
  69. \ Compute volume.
  70. : .VOLUME ( l w h  -- )
  71.         * *  CR  ." Volume "  .  ." cubic feet." ;
  72.  
  73. \ Compute surface area.
  74. : .AREA   ( l w h  -- )
  75.         3DUP 5 ROLL * 2* -ROT * 2* + -ROT * 2* +
  76.         CR ." Surface area " . ." square feet." ;
  77.  
  78. \ This is the tank program with prompted input
  79. : TANK  ( -- )
  80.         GETL  GETW  GETH
  81.         3DUP  .VOLUME    .AREA ;
  82.  
  83. Here is what happens when we execute TANK
  84.  
  85. FLOAD INTANK <enter> ok
  86. TANK  <enter>
  87. Enter tank length 5
  88. Enter tank width  4
  89. Enter tank height 3
  90. Volume 60 cubic feet.
  91. Surface area 94 square feet. ok
  92.  
  93. There is only one problem with this version on #IN .  It is too
  94. powerful!  You can actually input just about anything when you get the
  95. prompt including Forth words, more than one number and of course
  96. garbage!
  97.  
  98. ╓──────────────╖
  99. ║ Problem 3.23 ║
  100. ╙──────────────╜
  101. Experiment with the program above and list at least 3 problems with this
  102. definition of #IN.  If you can find more than 3 list them all.
  103.  
  104. ┌───────────────────────────────────┐
  105. │  Please move to Lesson 3 Part 110 │
  106. └───────────────────────────────────┘
  107.