Sunday, October 14, 2018

A Forth Program for Making HTML Multiplication Tables

Since Forth programs tend to be self-documenting when written carefully, there really isn't much to say. Remember that Forth expressions are postfix.

Enjoy.


( Forth program for making HTML multiplication tables )
( Joel Matthew Rees, October 2018, Amagasaki, Japan )
( Copyright 2018 Joel Matthew Rees )
( Permission granted for private, non-profit use. )

( Runs in gforth. Should run in almost any Forth. )

: decout ( n --- )
  base @ swap
  10 base ! 1 .R
  base ! ;

: colheadout ( percentwidth n --- )
  over ." <th align='center' width='" decout ." %'>"
  1 .R ." </th>" ;

: hrow ( percentwidth limit --- )
  0 do i colheadout loop drop ;

: headrow ( percentwidth limit --- )
  ." <tr>" CR ." <th>×</th>" hrow CR ." </tr>" CR ;

: thout ( n --- )
  ." <th align='center'>" 1 .R ." </th>" ;

: tdout ( n --- )
  ." <td align='center'>" 1 .R ." </td>" ;

: row (  limit current --- limit )
  over 0 do dup i * tdout loop drop ;

: fullrow ( limit current --- limit )
  ." <tr>" CR dup thout row CR ." </tr>" CR ;

: htmltable ( base --- )
  CR ." <table border='1'>" CR
  base @ >R
  dup base !
  3 + ( limit )
  100 over 1 + / ( width in percent )
  over headrow
  dup 0 do
    i fullrow
  loop
  drop
  R> base !
  ." </table>" CR
;

: maketables
  1+ 2 do
    ." <br />" CR
    i 1 .R ." s table:<br />" CR
    i htmltable
  loop ;


The results of running "10 maketables" (copied from terminal window and pasted as-is into Blogger's HTML edit mode):

2s table:
×011011100
000000
1011011100
100101001101000
1101111010011100
10001001000110010000

3s table:
×012101112
0000000
1012101112
202112022101
1001020100110120
1101122110121202
12012101120202221

4s table:
×0123101112
00000000
10123101112
2021012202230
30312213033102
100102030100110120
110112233110121132
1201230102120132210

5s table:
×01234101112
000000000
101234101112
20241113202224
303111422303341
4041322314044103
10010203040100110120
11011223344110121132
120122441103120132144

6s table:
×012345101112
0000000000
1012345101112
2024101214202224
30310132023303340
40412202432404452
505142332415055104
1001020304050100110120
1101122334455110121132
12012244052104120132144

7s table:
×0123456101112
00000000000
10123456101112
20246111315202224
303612152124303336
4041115222633404451
5051321263442505563
60615243342516066105
100102030405060100110120
110112233445566110121132
1201224365163105120132144

8s table:
×01234567101112
000000000000
101234567101112
2024610121416202224
30361114172225303336
404101420243034404450
505121724313643505562
606142230364452606674
7071625344352617077106
10010203040506070100110120
11011223344556677110121132
120122436506274106120132144

9s table:
×012345678101112
0000000000000
1012345678101112
20246811131517202224
3036101316202326303336
4048131722263135404448
50511162227333844505561
60613202633404653606673
70715233138465462707785
808172635445362718088107
1001020304050607080100110120
1101122334455667788110121132
12012243648617385107120132144

10s table:
×0123456789101112
00000000000000
10123456789101112
2024681012141618202224
30369121518212427303336
404812162024283236404448
5051015202530354045505560
6061218243036424854606672
7071421283542495663707784
8081624324048566472808896
90918273645546372819099108
100102030405060708090100110120
110112233445566778899110121132
1201224364860728496108120132144