Plan 9 from Bell Labs’s /usr/web/sources/contrib/fgb/root/sys/src/cmd/4th/examples/romans.scr

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


( ROMANS by Leo Brodie, Thinking Forth:1)                                                                                       CREATE ROMANS    ( ones)        CHAR I , CHAR V ,                                ( tens)        CHAR X , CHAR L ,                                ( hundreds)    CHAR C , CHAR D ,                                ( thousands)   CHAR M ,                                                                                        VARIABLE COLUMN# ( current offset)                              : ONES       0 COLUMN# ! ;                                      : TENS       2 COLUMN# ! ;                                      : HUNDREDS   4 COLUMN# ! ;                                      : THOUSANDS  6 COLUMN# ! ;                                                                                                      : COLUMN  ( -- address of column) ROMANS COLUMN# @ + ;                                                                                                                                          ( ROMANS by Leo Brodie, Thinking Forth:2)                                                                                       : .SYMBOL  ( offset --) COLUMN + @C EMIT ;                      : ONER   0 .SYMBOL ;                                            : FIVER  1 .SYMBOL ;                                            : TENER  2 .SYMBOL ;                                                                                                            : ONERS   ( # of oners --)                                        0 ?DO ONER LOOP ;                                             : ALMOST ( quotient of 5 / --)                                    ONER IF TENER ELSE FIVER THEN ;                               : DIGIT   ( digit --)                                             5 /MOD OVER 4 = IF ALMOST DROP ELSE IF FIVER THEN               ONERS THEN ;                                                                                                                                                                                  ( ROMANS by Leo Brodie, Thinking Forth:3)                                                                                       : ROMAN ( number --)  1000 /MOD THOUSANDS DIGIT                                        100 /MOD HUNDREDS  DIGIT                                         10 /MOD TENS      DIGIT                                                 ONES      DIGIT ;                                                                               : ENTER BEGIN REFILL DROP BL PARSE-WORD NUMBER ERROR?             WHILE DROP REPEAT ;                                                                                                           : PROMPT ." Enter number: " ENTER                                        ." Roman number: " ROMAN CR ;                                                                                          PROMPT                                                                                                                                                                                          

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.