Multiplication tables: Difference between revisions
Content added Content deleted
(→{{header|Visual Basic}}: fixed <pre> tag) |
m (→{{header|REXX}}: simplified the code, add/changed comments and whitespace.) |
||
Line 3,335: | Line 3,335: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
A lot of the program code deals with the presentation/display of the multiplication |
A lot of the program code deals with the presentation/display of the grid for the multiplication table. |
||
<lang REXX>/*REXX program displays a |
<lang REXX>/*REXX program displays a NxN multiplication table (in a boxed grid) to the terminal.*/ |
||
parse arg high . /* |
parse arg high . /*obtain optional grid size from the CL*/ |
||
if high=='' then high=12 |
if high=='' | high=="," then high=12 /*Not specified? Then use the default.*/ |
||
bar = '│' ; dash = |
bar = '│' ; dash = "─" /*(vertical) bar; horizontal bar (dash)*/ |
||
bj = '┴' ; tj = |
bj = '┴' ; tj = "┬" /*bottom and top junctions (or tees).*/ |
||
cj = '┼' /*center junction (or cross). */ |
cj = '┼' /*center junction (or cross). */ |
||
lj = '├' ; rj = |
lj = '├' ; rj = "┤" /*left and right junctions (or tees).*/ |
||
tlc = '┌' ; trc = |
tlc = '┌' ; trc = "┐" /* top left and right corners. */ |
||
blc = '└' ; brc = |
blc = '└' ; brc = "┘" /*bottom " " " " */ |
||
/* [↑] define stuff to hold box glyphs*/ |
/* [↑] define stuff to hold box glyphs*/ |
||
cell = cj || copies(dash, |
cell = cj || copies(dash,max(5,length(high) +1)) /*define the top of the cell. */ |
||
sep = copies(cell, high+1)rj /*construct the table separator. */ |
sep = copies(cell, high+1)rj /*construct the table separator. */ |
||
size = length(cell) - 1 /*width for the products in the table. */ |
|||
box. = left('', size) /*initialize all the cells in the table*/ |
|||
size = width-1 /*width for the products in the table. */ |
|||
box. = left('', width) /*initialize all the cells in the table*/ |
|||
do j=0 to high /*step through zero ───► high. */ |
do j=0 to high /*step through zero ───► high. */ |
||
_=right(j, size- |
_=right(j, size - 2)'x ' /*build the "label" (border) number. */ |
||
box.0.j=_ |
box.0.j=center(_, size) /* " " top label cell. */ |
||
box.j.0=_ |
box.j.0=center(_, max(5, size) ) /* " " left label cell. */ |
||
end /*j*/ |
end /*j*/ |
||
box.0.0= |
box.0.0=center('times', max(5, size)) /*redefine box.0.0 with "times". */ |
||
do r=1 for high /*step through row one ───► high. */ |
do r=1 for high /*step through row one ───► high. */ |
||
do c=r to high /*step through column row ───► high. */ |
do c=r to high /*step through column row ───► high. */ |
||
box.r.c=right(r*c, size) |
box.r.c=right(r*c, size) /*build a single multiplication cell. */ |
||
end /*c*/ |
end /*c*/ |
||
end /*r*/ /*only build the top right-half of grid*/ |
end /*r*/ /*only build the top right-half of grid*/ |
||
do r=0 to high; @=sep /*step through all lines; use a mod sep*/ |
do r=0 to high; @=sep /*step through all lines; use a mod sep*/ |
||
if r==0 then do |
if r==0 then do |
||
@=overlay(tlc,@ , 1) /*use a better tlc (top left corner). */ |
@=overlay(tlc, @ , 1) /*use a better tlc (top left corner). */ |
||
@=overlay(trc,@ , |
@=overlay(trc, @ , length(sep)) /* " " " trc ( " right " ). */ |
||
@=translate(@,tj,cj) /* " " " tj (top junction/tee).*/ |
@=translate(@, tj, cj) /* " " " tj (top junction/tee).*/ |
||
end |
end |
||
else @=overlay(lj, @ |
else @=overlay(lj, @, 1) /* " " " lj (left junction/tee).*/ |
||
say @ /*display a single table grid line. */ |
say @ /*display a single table grid line. */ |
||
if r==0 then call buildLine 00 /* " " " blank grid " */ |
if r==0 then call buildLine 00 /* " " " blank grid " */ |
||
call buildLine r /*build a single line of the grid. */ |
call buildLine r /*build a single line of the grid. */ |
||
if r==0 then call buildLine 00 /*display a single blank grid line. */ |
if r==0 then call buildLine 00 /*display a single blank grid line. */ |
||
end /*r*/ |
end /*r*/ |
||
@=sep /*allow use of a modified separator. */ |
@=sep /*allow use of a modified separator. */ |
||
@=overlay(blc, @ , 1) /*use a better bottom left corner. */ |
@=overlay(blc, @ , 1) /*use a better bottom left corner. */ |
||
@=overlay(brc, @ , |
@=overlay(brc, @ , length(sep) ) /* " " " " right corner. */ |
||
@=translate(@, bj, cj) /* " " " " junction. */ |
@=translate(@, bj, cj) /* " " " " junction. */ |
||
say @ /*display a (single) table grid line. */ |
say @ /*display a (single) table grid line. */ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
buildLine: parse arg row,,$ /*start with a blank cell ($). |
buildLine: parse arg row,,$ /*start with a blank cell ($). */ |
||
do col=0 to high /*step through zero |
do col=0 to high /*step through zero ───► high. */ |
||
$=$ || bar || box.row.col /*build one cell at a time. |
$=$ || bar || box.row.col /*build one cell at a time. */ |
||
end /*col*/ |
end /*col*/ |
||
say $ || bar /*finish building the last cell. |
say $ || bar /*finish building the last cell.*/ |
||
return</lang> |
return</lang> |
||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
<pre> |