September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
56
Task/Align-columns/BASIC/align-columns-1.basic
Normal file
56
Task/Align-columns/BASIC/align-columns-1.basic
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
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."
|
||||
|
||||
REM First find the maximum length of a 'word':
|
||||
max% = 0
|
||||
READ nlines%
|
||||
FOR Line% = 1 TO nlines%
|
||||
READ text$
|
||||
REPEAT
|
||||
word$ = FNword(text$, "$")
|
||||
IF LEN(word$) > max% THEN max% = LEN(word$)
|
||||
UNTIL word$ = ""
|
||||
NEXT Line%
|
||||
@% = max% : REM set column width
|
||||
|
||||
REM Now display the aligned text:
|
||||
RESTORE
|
||||
READ nlines%
|
||||
FOR Line% = 1 TO nlines%
|
||||
READ text$
|
||||
REPEAT
|
||||
word$ = FNword(text$, "$")
|
||||
PRINT FNjustify(word$, max%, "left"),;
|
||||
UNTIL word$ = ""
|
||||
PRINT
|
||||
NEXT Line%
|
||||
|
||||
END
|
||||
|
||||
DEF FNword(text$, delim$)
|
||||
PRIVATE delim%
|
||||
LOCAL previous%
|
||||
IF delim% = 0 THEN
|
||||
previous% = 1
|
||||
ELSE
|
||||
previous% = delim% + LEN(delim$)
|
||||
ENDIF
|
||||
delim% = INSTR(text$+delim$, delim$, previous%)
|
||||
IF delim% = 0 THEN
|
||||
= ""
|
||||
ELSE
|
||||
= MID$(text$, previous%, delim%-previous%) + " "
|
||||
ENDIF
|
||||
|
||||
DEF FNjustify(word$, field%, mode$)
|
||||
IF word$ = "" THEN = ""
|
||||
CASE mode$ OF
|
||||
WHEN "center": = STRING$((field%-LEN(word$)) DIV 2, " ") + word$
|
||||
WHEN "right": = STRING$(field%-LEN(word$), " ") + word$
|
||||
ENDCASE
|
||||
= word$
|
||||
Loading…
Add table
Add a link
Reference in a new issue