- Code: Select all

\ Last Revision: 23 Jul 2018 08:03:12 dbh

\ Douglas B. Hoffman

\ This program was written to illustrate one way the Leo Brodie

\ Starting Forth program "No Weighting" can be written in Oforth.

\ For comparison to the original SF program see the listing that

\ comes after. Also, a link to the pertinent SF section is:

\ http://home.iae.nl/users/mhx/sf12/sf12.html "3. No Weighting"

\ *** BEGIN PROGRAM

import: math \ to get PI and round

tvar: material-in-use

Object Class new: material ( i.d. density tan )

m: initialize \ str density tan

:= tan := density := i.d. ;

\ ht^3*pi*D

\ W = -----------

\ 3*tan^2

m: weight \ ( ht -- ) input height in feet

3 ^ PI * @density *

@tan 2 ^ 3 * /

dup round . "pounds of" . @i.d. . "or" .

2000 /

<<wjp(System.Out, 7, JUSTIFY_RIGHT, 6)

" tons" . ;

m: use self to material-in-use ;

: feet \ ( ht -- ) height in feet

material-in-use weight ;

\ i.d. density tan

"cement" 131 .7 material new const: cement

"loose gravel" 93 .649 material new const: loose-gravel

"packed gravel" 100 .7 material new const: packed-gravel

"dry sand" 90 .754 material new const: dry-sand

"wet sand" 118 .9 material new const: wet-sand

"clay" 120 .727 material new const: clay

\ *** END PROGRAM

cement use

10 feet

\ => 279965 pounds of cement or 139.983 tons ok

10.25 feet

\ => 301492 pounds of cement or 150.746 tons ok

dry-sand use

10 feet

\ => 165779 pounds of dry sand or 82.8893 tons ok

false IFTRUE: [

\ for comparison, a standard Forth version based on the program from Starting Forth

\ to be fair, most Forths can use a floating point extension and word-set these days

\ so this could be written in an easier-to-read fashion

VARIABLE density VARIABLE theta VARIABLE i.d.

: STRING ( c) WORD DUP C@ 1+ ALLOT ;

: ," [CHAR] " STRING ; ( -- c-addr )

: DU.3 ( du --) <# # # # [CHAR] . HOLD #S #> TYPE SPACE ;

: MATERIAL ( 'string density theta -- )

CREATE , , ,

DOES> DUP @ theta !

CELL+ DUP @ density !

CELL+ @ i.d. ! ;

: .SUBSTANCE i.d. @ COUNT TYPE SPACE ;

: CUBE 2DUP OVER 10 M*/ DROP 10 M*/ ; ( d1 -- d2 )

: /TAN 1000 theta @ M*/ ; ( d1 -- d2 )

: FEET ( d -- )

CUBE 355 339 M*/ density @ 1 M*/

/TAN /TAN 5 M+ 1 10 M*/

2DUP ." = " D. ." pounds of " .SUBSTANCE

1 2 M*/ ." or " DU.3 ." tons " ;

\ table of materials

\ string-address density theta

," cement" 131 700 MATERIAL cement

," loose gravel" 93 649 MATERIAL loose-gravel

," packed gravel" 100 700 MATERIAL packed-gravel

," dry sand" 90 754 MATERIAL dry-sand

," wet sand" 118 900 MATERIAL wet-sand

," clay" 120 727 MATERIAL clay

cement

10.0 feet

\ = 279939 pounds of cement or 139.969 tons ok

cr

10.3 feet

\ = 305658 pounds of cement or 152.829 tons ok

cr

dry-sand

10.3 feet

\ = 165763 pounds of dry sand or 82.881 tons ok

]