home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l3p090
< prev
next >
Wrap
Text File
|
1990-07-15
|
5KB
|
120 lines
╔════════════════════════════════════════════════════╗
║ Lesson 3 Part 090 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
We continue our discussion of IF ... ELSE .... THEN
If you would like to know more about D-Charts check out Starting Forth
2nd edition chapter 11 page 281 "Introduction to Forth Flowcharts".
╓──────────────╖
║ Problem 3.18 ║
╙──────────────╜
Construct the D-Chart for your solution to problem 3.13
╓──────────────╖
║ Problem 3.19 ║
╙──────────────╜
Construct the D-Chart for the word PCKEY whose definition was
given in lesson 3 part 7.
Consider the following scenario:
We are writing a program for the Super-F Radar Gun (used for checking
the speed of motor vehicles). We word that will take the vehicle speed
as a stack input and print one of the following messages when the speed
limit is 50 kmph.
Message Speed range kmph
" issue ticket, impeding traffic flow." 0 - 15
" issue warning, impeding traffic flow." 16 - 30
" no action, safe speed." 31 - 55
" issue warning, exceeding speed limit." 56 - 65
" issue ticket, exceeding speed limit." 66 - 100
" arrest motorist, dangerous driving." 100 -
By the way... we are going to use kilometers per hour to measure the
speed. Do all our Forth Friends in the good old US of A realize that if
the Canada - US Free Trade Deal goes through that they will have to
switch to the metric system for all weights and measures??
Note: 30 kmph = 18 mph, 50 kmph = 30 mph, 100 kmph = 60 mph etc.
Let's see how should we proceed.... Oh.. Let's use our interval testing
words.. Remember [IN] and the rest of the family?
\ Leave true flag is speed is very slow 0 - 15 kmph
: VERY_SLOW? ( speed -- flag) 0 15 [IN] ;
\ Leave true flag if speed is slow 16 - 30 kmph
: SLOW? ( speed -- flag ) 16 30 [IN] ;
\ Leave true flag if speed is normal 31 - 55 kmph
: NORMAL? ( speed -- flag ) 31 55 [IN] ;
\ Leave true flag if speed is fast 56 - 65 kmph
: FAST? ( speed -- flag ) 56 65 [IN] ;
\ Leave true flag if speed is very fast 66- 99 kmph
: VERY_FAST? ( speed -- flag ) 66 99 [IN] ;
\ Leave true flag if speed is dangerous 100 kmph and over.
: DANGEROUS? ( speed -- flag ) 99 > ;
\ Check speed and print appropriate message.
: SPEED_CHECK ( speed -- )
DUP VERY_SLOW?
IF ." Issue ticket, impeding traffic flow." DROP
ELSE DUP SLOW?
IF ." Issue warning, impeding traffic flow." DROP
ELSE DUP NORMAL?
IF ." No action, safe speed." DROP
ELSE DUP FAST?
IF ." Issue warning, exceeding speed limit." DROP
ELSE DUP VERY_FAST?
IF ." Issue ticket, exceeding speed limit." DROP
ELSE DANGEROUS?
IF ." Arrest motorist, dangerous driving."
THEN
THEN
THEN
THEN
THEN
THEN ;
Example of the execution of speed_check.
10 SPEED_CHECK <enter> Issue ticket, impeding traffic flow. ok
20 SPEED_CHECK <enter> Issue warning, impeding traffic flow. ok
40 SPEED_CHECK <enter> No action, safe speed. ok
60 SPEED_CHECK <enter> Issue warning, exceeding speed limit. ok
70 SPEED_CHECK <enter> Issue ticket, exceeding speed limit. ok
200 SPEED_CHECK <enter> Arrest motorist, dangerous driving. ok
-1 SPEED_CHECK <enter> ok
╓───────────────╖
║ Problem 3.20 ║
╙───────────────╜
Make a word called BROKEN? that leaves a true flag if the speed is
negative and then modify the SPEED_CHECK to print the appropriate
message for this case. Let's also make SPEED_CHECK to recognize
impossibly high rates of speed and issue an appropriate message.
╓───────────────╖
║ Problem 3.21 ║
╙───────────────╜
Rewrite SPEED_CHECK without using the words [IN] ...(IN). It should be
possible to write a more efficient version because it should not be
necessary to check both ends of all the speed intervals.
╓───────────────╖
║ Problem 3.22 ║
╙───────────────╜
Rewrite SPEED_CHECK so that the most common occurrences are tested first
and the least common last. For example check for NORMAL? speed first
and BROKEN? last.
┌────────────────────────────────────┐
│ Please move to Lesson 3 Part 100 │
└────────────────────────────────────┘