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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 6 Part 030  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.                 ┌───────────────────┐
  6.                 │ A Dashed Example  │
  7.                 └───────────────────┘
  8.  
  9. Example:  Here are number of ways to make a dashed line using 10 dash
  10. characters.
  11.  
  12. : DASHED1  ( -- ) CR ." ----------" ;
  13.   CREATE DASH  ," ----------"
  14. : DASHED2  ( -- ) CR DASH COUNT TYPE ;
  15. : DASHED3  ( -- ) CR " ----------" TYPE ;
  16. : DASHED4  ( -- ) CR 10 0 DO  ASCII - EMIT LOOP ;
  17.  
  18. ╓──────────────╖                              **
  19. ║ Problem 6.3  ║                             ****
  20. ╙──────────────╜                            ******
  21. Use each of the four techniques            ********
  22. above to produce the display              **********
  23. shown at the right.                           **
  24.  
  25.           ┌─────────────────────────────────────┐
  26.           │  String Initialization Primitives   │
  27.           └─────────────────────────────────────┘
  28.  
  29. The Forth words FILL and ERASE  which were studied earlier are often
  30. used as string initialization primitives.
  31.  
  32. FILL  ( addr n c -- )   Fill string at addr with n copies of c .
  33. ERASE ( addr n -- )     Fill string at addr with n null's or 0's.
  34.  
  35. Try the following:
  36. NAME$ ," George Smith" <enter>  ok
  37. NAME$ COUNT TYPE <enter> George Smith ok
  38. NAME$ COUNT ASCII * FILL <enter>  ok
  39. NAME$ COUNT TYPE <enter> ************ ok
  40. NAME$ COUNT BL FILL <enter>
  41. NAME$ COUNT TYPE <enter>              ok
  42.  
  43.             ┌─────────────────────────────────┐
  44.             │ Forth String Input Techniques.  │
  45.             └─────────────────────────────────┘
  46.  
  47. The Forth word EXPECT is used to input a string of up to length n to a
  48. buffer at address, addr . The actual number of characters entered is
  49. stored in a variable called SPAN for use by the application program. The
  50. Forth 83 implementation of EXPECT will return control to the calling
  51. program when the nth character has been received.  Pressing <enter>
  52. before the nth character will also return control to the calling program
  53. The string placed in the buffer at addr by EXPECT is not a counted
  54. string, the application program must determine the actual number of
  55. characters entered by the user by interrogating the variable SPAN.
  56.  
  57. EXPECT ( addr n -- ) \ Input up to n characters to buffer at addr
  58.  
  59. CREATE BUFFER1  80 C, 80 ALLOT       VARIABLE LEN
  60.  
  61. \ Accept a string up to 80 characters long from the console.
  62. : READLINE  ( -- )
  63.            BUFFER1 COUNT BL FILL    \ Clear BUFFER1 to blanks.
  64.         CR BUFFER1 COUNT  EXPECT    \ Input up to 80 characters
  65.          \ SPAN @ BUFFER1 C!        \ Alternate to below method.
  66.            SPAN @ LEN ! ;           \ Save actual character count
  67.  
  68. Note:  Actual character count is returned in variable SPAN and is saved
  69. for our own use in the VARIABLE LEN .   This must be done because the
  70. Forth system itself uses EXPECT to input characters and our value stored
  71. in SPAN would be lost.
  72.  
  73. \ Display string stored in BUFFER1
  74. : SHOWLINE  ( -- )
  75.      CR  BUFFER1 1+  \  skip of buffer length of 80
  76.      LEN @  TYPE ;   \  display actual number entered.
  77.  
  78. READLINE <enter>
  79. Now is the time for a party!  ok
  80. SHOWLINE <enter>
  81. Now is the time for a party! ok
  82.  
  83. ╓─────────────╖
  84. ║ Problem 6.4 ║
  85. ╙─────────────╜
  86. Write a simple ELIZA program that will interact with a user.  Your
  87. program should use EXPECT for string input.  Your program should find
  88. and save at least the following information about the user:  NAME  SEX
  89. and EYE-COLOUR saving the information.  Your program should ask at least
  90. two questions that can be answered with  a yes or no answer.  Save the
  91. yes/now response in a string called ANSWER .  You program should only
  92. check the first letter of the entered string and case should not matter
  93. so that  Y YES yes yah  etc would all be interpreted as a  YES and   No
  94. no N Nah Nyet etc would all be taken as a NO.  Your program should
  95. provide alternate paths depending upon the answers given.
  96.  
  97. Hints:
  98.    CREATE EYE-COLOUR ," **********"
  99.  : EYE-COLOUR? ( -- )
  100.           ." What colour are your eyes? "
  101.           EYE-COLOUR COUNT BL FILL
  102.           EYE-COLOUR COUNT EXPECT
  103.           SPAN @ EYE-COLOUR C!   ;
  104.  
  105. EYE-COLOUR? <enter> What colour are your eyes? blue <enter> ok
  106. EYE-COLOUR COUNT TYPE <enter> blue ok
  107.  
  108. ┌────────────────────────────────────┐
  109. │  Please Move to Lesson 6 Part 040  │
  110. └────────────────────────────────────┘
  111.