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

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


\ Simple Math Program  4tH - (c) 1995 HanSoft & Partners

[needs lib/enter.4th]

: add + ;                              ( possible operators )
: sub - ;
: mul * ;

: getnum random 100 mod ;              ( put a number on the stack )

: check                                ( checks right answer )
  execute dup                          ( calculate right answer )
  enter =                              ( get user response )
  if ." That's right: " . 1 +
    else ." You're wrong, the right answer is " .
  then
  cr cr
;

: makecalc                             ( create a calculation )
  getnum getnum                        ( get two numbers )
  over over swap                       ( put them on the stack )
  random 3 mod                         ( select operator )
    dup 0=  if drop . ." + " . ." = " ' add else
    dup 1 = if drop . ." - " . ." = " ' sub else
        2 = if      . ." x " . ." = " ' mul
            then then then
  check
;

: calculations
  4 0 do makecalc 1 +loop
;

0                                      ( init number of calculations )
calculations                           ( execute calculations )

." Total: " . ." right answers!" cr cr cr

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.