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

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


( Program by Leo Brodie)

[needs lib/enter.4th]

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# @ + ;

: .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 ;

: ROMAN  ( number --)  1000 /MOD THOUSANDS DIGIT
                        100 /MOD HUNDREDS DIGIT
                         10 /MOD TENS DIGIT
                                 ONES DIGIT ;

: 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.