const
WEnter=;
key=;
next_line:array[..WEnter]of string=(';','begin','else','then','repeat','do','var');
key_word:array[..key]of string=('begin','end','function','procedure','var','if','else','repeat','until','while','type','const','for');//关键的语句(需要缩进的)
suojin=;
maxn=;
var
i,j,t,long:longint;
s:string;
pg,pg_key:array[..maxn]of string;
kg:array[..maxn]of longint;
full:array[..maxn]of boolean;
function noAlphabat(s:string):boolean;这个字符串不全为空格
begin
if s='' then
exit(true);
while s[]=' ' do
begin
delete(s,,);
if s='' then break;
end;
if s='' then
exit(true);
exit(false);
end;
procedure Enterat(x,y:longint);在x,y的地方按一个回车
var
i:longint;
s:string;
begin
for i:=long downto x+ do
pg[i+]:=pg[i];
pg[x+]:='';
s:=copy(pg[x],y,length(pg[x])-y+);
pg[x]:=copy(pg[x],,y-);
pg[x+]:=s;
long:=long+;
end;
function next(i:longint):longint;//找与第i行后9含)第一个begin对应的end
var
t,j:longint;
begin
t:=;
for j:=i to long do
begin
if pg_key[j]='begin' then
t:=t+;
if pg_key[j]='end' then
t:=t-;
if t= then
exit(j);
end;
writeln('Pairing error with begin-end!');
//close(output);
halt;
end;
function next2(i:longint):longint;//找与第i行后(含)的第一个repeat配对的until
var
t,j:longint;
begin
t:=;
for j:=i to long do
begin
if pg_key[j]='repeat' then
t:=t+;
if pg_key[j]='until' then
t:=t-;
if t= then
exit(j);
end;
writeln('Pairing error with repeat-until!');
//close(output);
halt;
end;
procedure dfs(l,r,t:longint);//第l到r区间里的代码格式化(初始值t个空格)
var
i,k:longint;
alldo,had_done:boolean;
begin
if l>r then
exit;
for i:=l to r do
kg[i]:=t;
alldo:=true;
for i:=l to r do
if pg_key[i]<>'do' then
alldo:=false;
if alldo then//全部不需缩进则跳出
exit;
i:=l;
while i<=r do
begin
had_done:=false;
if pg_key[i]='begin' then//begin处理
begin
dfs(i+,next(i)-,t+);
i:=next(i)+;
had_done:=true;
end;
if pg_key[i]='repeat' then//repeat处理
begin
had_done:=true;
dfs(i+,next2(i)-,t+);
i:=next2(i)+;
end;
if (pg_key[i]='if')or(pg_key[i]='while')or(pg_key[i]='for') then//if、while、for处理(共同点是后面可以加begin-end、也可以加do)
begin
had_done:=true;
if pg_key[i+]='do' then
begin
dfs(i+,i+,t+);
if pg_key[i+]='else' then //else后相同处理
begin
if pg_key[i+]='do' then
begin
dfs(i+,i+,t+);
i:=i+;
end;
if pg_key[i+]='begin' then
begin
dfs(i+,next(i+),t+);
i:=next(i+)+;
end;
if pg_key[i+]='repeat' then
begin
dfs(i+,next2(i+),t+);
i:=next2(i+)+;
end;
end
else
i:=i+;
end;
if pg_key[i+]='begin' then
begin
dfs(i+,next(i+),t+);
if pg_key[next(i+)+]='else' then
begin
if pg_key[next(i+)+]='do' then
begin
dfs(next(i+)+,next(i+)+,t+);
i:=next(i+);
end;
if pg_key[next(i+)+]='begin' then
begin
dfs(next(i+)+,next(next(i+)+),t+);
i:=next(next(i)+)+;
end;
if pg_key[next(i+)+]='repeat' then
begin
dfs(next(i+)+,next2(next(i+)+),t+);
i:=next2(next(i+)+)+;
end;
end
else
i:=next(i+)+;
end;
if pg_key[i+]='repeat' then
begin
dfs(i+,next2(i+),t+);
if pg_key[next2(i+)+]='else' then
begin
if pg_key[next2(i+)+]='do' then
begin
dfs(next2(i+)+,next2(i+)+,t+);
i:=next2(i+);
end;
if pg_key[next2(i+)+]='begin' then
begin
dfs(next2(i+)+,next(next2(i+)+),t+);
i:=next(next2(i)+)+;
end;
if pg_key[next2(i+)+]='repeat' then
begin
dfs(next(i+)+,next2(next2(i+)+),t+);
i:=next2(next2(i+)+)+;
end;
end
else
i:=next2(i+)+;
end;
end;
if (pg_key[i]='procedure')or(pg_key[i]='function') then
begin
had_done:=true;
dfs(i+,next(i+),t);
i:=next(i+)+;
end;
if (pg_key[i]='var')or(pg_key[i]='type')or(pg_key[i]='const')then
begin
had_done:=true;
k:=i;
while pg_key[k+]='do' do
k:=k+;
dfs(i+,k,t+);
i:=k+;
end;
if not had_done then
i:=i+;
end;
end;
begin
long:=;
while not eof do
begin
long:=long+;
readln(pg[long]);
end;
for i:= to long do
begin
t:=;
while (pg[i]<>'')and(pg[i][]=#) do
begin
t:=t+;
delete(pg[i],,);
end;
for j:= to t* do
pg[i]:=' '+pg[i];
end;
i:=;
while i<=long do//黏在一起的语句拆开
begin
for j:= to WEnter do
if pos(next_line[j],pg[i])<> then
if not(noAlphabat(copy(pg[i],pos(next_line[j],pg[i])+length(next_line[j]),length(pg[i])-pos(next_line[j],pg[i])-length(next_line[j])+))) then
begin
t:=pos(next_line[j],pg[i])+length(next_line[j]);
Enterat(i,t);
end;
i:=i+;
end;
for i:= to long do
while pg[i,]=' ' do
delete(pg[i],,);
fillchar(full,sizeof(full),true);
for i:= to long do
if pg[i]='' then
full[i]:=false;
t:=;
for i:= to long do
if full[i] then
begin
t:=t+;
pg[t]:=pg[i];
end;
for i:=t+ to long do
pg[i]:='';
long:=t;
for i:= to long do
begin
for j:= to key do
if pos(key_word[j],pg[i])<> then
begin
pg_key[i]:=key_word[j];
break;
end;
if pg_key[i]='' then
pg_key[i]:='do';
end;
dfs(,long,);
for i:= to long do
kg[i]:=kg[i]*suojin;
for i:= to long do
writeln('':kg[i],pg[i]);
end.
比较坑爹的就是for循环嵌套无法处理,求大神帮修改