RosettaCodeData/Task/Align-columns/Liberty-BASIC/align-columns.basic

79 lines
2.0 KiB
Plaintext

mainwin 140 32
CRLF$ =chr$( 13)
maxlen =0
read y
Dim txt$( y)
For i =1 To y
Read i$
print i$
if right$( i$, 1) <>"$" then i$ =i$ +"$"
txt$( i) =i$
x =max( CountDollars( txt$( i)), x)
Next i
print x
Dim matrix$( x, y)
Print CRLF$; " ---- Left ----"
For yy =1 To y
For xx =1 To x
matrix$( xx, yy) =word$( txt$( yy), xx, "$")
print matrix$( xx, yy), "|";
maxlen =max( maxlen, Len( matrix$( xx, yy)))
Next xx
print ""
Next yy
Print CRLF$; " ---- Right ----"
For yy =1 To y
For xx =1 To x
Print right$( " " +matrix$( xx, yy), maxlen +1); "|";
' will truncate column words longer than 20. Change to use maxlen....
Next xx
Print ""
Next yy
Print CRLF$ +" ---- Center ----"
For yy =1 to y
For xx =1 to x
wordLen =Len( matrix$( xx, yy))
padNeeded =maxlen -wordLen +4
LeftSpaces =padNeeded /2
if LeftSpaces =int( LeftSpaces) then
RightSpaces =LeftSpaces
else
RightSpaces =LeftSpaces -1
end if
Print space$( LeftSpaces); matrix$( xx, yy); space$( RightSpaces); "|";
Next xx
Print ""
Next yy
wait
Data 6
Data "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
Data "are$delineated$by$a$single$'dollar'$character,$write$a$program"
Data "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"
Data "column$are$separated$by$at$least$one$space."
Data "Further,$allow$for$each$word$in$a$column$to$be$either$left$"
Data "justified,$right$justified,$or$center$justified$within$its$column."
function CountDollars( src$)
c =0
for j =1 to len( src$)
if mid$( src$, j, 1) ="$" then c =c +1
next j
CountDollars =c
end function
end