58 lines
1.9 KiB
Plaintext
58 lines
1.9 KiB
Plaintext
string 0;
|
|
def LF=$0A, CR=$0D;
|
|
def Left, Right, Center;
|
|
|
|
proc AlignCols(S); \Display string S with its columns aligned
|
|
char S, C, Field(80), ColWidth(80);
|
|
int I, J, N, Just;
|
|
|
|
proc Justify;
|
|
int T;
|
|
|
|
proc SpOut(M); \Output M space characters
|
|
int M, K;
|
|
for K:= 0 to M-1 do ChOut(0, ^ );
|
|
|
|
proc FieldOut; \Output Field of N characters
|
|
int K;
|
|
for K:= 0 to N-1 do ChOut(0, Field(K));
|
|
|
|
[case Just of
|
|
Left: [FieldOut(N); SpOut(ColWidth(J)-N+1)];
|
|
Right: [SpOut(ColWidth(J)-N+1); FieldOut(N)];
|
|
Center:[T:= ColWidth(J)-N+1;
|
|
SpOut(T/2); FieldOut(N); SpOut(T/2 + rem(0))]
|
|
other [];
|
|
];
|
|
|
|
[\Get width (in characters) of each column
|
|
for J:= 0 to 80-1 do ColWidth(J):= 0;
|
|
I:= 0; J:= 0; N:= 0;
|
|
loop [repeat C:= S(I); I:= I+1 until C # CR;
|
|
if N > ColWidth(J) then ColWidth(J):= N;
|
|
case C of
|
|
0: quit;
|
|
^$: [N:= 0; J:= J+1];
|
|
LF: [N:= 0; J:= J+1; J:= 0]
|
|
other N:= N+1;
|
|
];
|
|
for Just:= Left to Center do
|
|
[I:= 0; J:= 0; N:= 0;
|
|
loop [repeat C:= S(I); I:= I+1 until C # CR;
|
|
case C of
|
|
0: [Justify(Just); CrLf(0); quit];
|
|
^$: [Justify(Just); N:= 0; J:= J+1];
|
|
LF: [Justify(Just); CrLf(0); N:= 0; J:= 0]
|
|
other [Field(N):= C; N:= N+1];
|
|
];
|
|
CrLf(0);
|
|
];
|
|
];
|
|
|
|
AlignCols("Given$a$text$file$of$many$lines,$where$fields$within$a$line$
|
|
are$delineated$by$a$single$'dollar'$character,$write$a$program
|
|
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
|
|
column$are$separated$by$at$least$one$space.
|
|
Further,$allow$for$each$word$in$a$column$to$be$either$left$
|
|
justified,$right$justified,$or$center$justified$within$its$column.")
|