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循环嵌套无法处理,求大神帮修改

Pascal代码自动格式化的更多相关文章

  1. 关于eclispe保存代码自动格式化的设置

    最近在项目开发,上级要求所有开发人员,代码必须格式和(Ctrl+Shift+F),但是还是会偶尔忘记格式化,今天看了有一种保存之后eclipse可以自动格式代码的设置 1.请大家在eclipse设置下 ...

  2. visual studio粘贴html代码.会自行添加一些未知代码(自动格式化)

    比如我们要粘贴这样一段代码到VS这里 <option {if empty($param.is_active)}selected=selected{/if}>请选择</option&g ...

  3. Eclipse中对Python代码自动格式化!

    在Eclipse中使用PyDev发现无法进行代码格式化,使用通常的"Ctrl+Shift+F"快捷键,没有任何变化,难道不能对python代码格式化了吗? 通过下面设置,就可以每次 ...

  4. vue eslint 代码自动格式化

    vue-cli 代码风格为 JavaScript Standard Style 代码检查规范严格,一不小心就无法运行,使用eslint的autoFixOnSave可以在保存代码的时候自动格式化代码 V ...

  5. Myeclipse 自定义代码自动格式化(ctrl+alt+F)

    打开如图界面:preference->java->code style->formatter下的edit... 如设设置java代码多长换行:

  6. vim中代码自动格式化

    参考资料: https://blog.csdn.net/qachenzude/article/details/25511875 1,gg 跳转到第一行 2,shift+v 转到可视模式 3,shift ...

  7. spotless-maven-plugin java代码自动格式化mvn spotless:apply -fn

    <plugin> <groupId>com.diffplug.spotless</groupId> <artifactId>spotless-maven ...

  8. VS Code中python代码自动格式化 代码自动检查

    VS Code菜单栏中依次打开 File—Preferences—Settings,搜索框中搜索“python.formatting.provider”, 然后在下拉菜单中选择autopep8,yap ...

  9. 解决vscode按下ctrl+S的时候自动格式化

    按下ctrl+S的时候自动格式化 为什么需要这种操作? 优点: 保存的时候格式化,让我们的代码自动格式化,减少人工调整. 缺点: 有一些打好包的JS有时候修改一下,但不需要格式化,因为打好包就是要压缩 ...

随机推荐

  1. NX二次开发-UFUN CSYS坐标系转换UF_CSYS_map_point

    1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_curve.h> 5 #include <uf_csys.h> 6 ...

  2. sql 生成javabean实体

    select a.name,c.name,b.name,'private String '+lower(c.name)+';' from sysobjects a, systypes b, sysco ...

  3. JavaScript笔记 – 程序语法设计

    一.基础语法设计 JavaScript是可以与HTML标记语言混合.用于网页交互式的解释型脚本语言.由国际标准ECMAScript提供核心语言功能.文档对象模型(DOM)提供访问和操作网页内容的方法和 ...

  4. java could not open `C|D|E|F:\jre\lib\amd64\jvm.cfg' 解决方案与原因

    因为安装了 jdk 后发现有多个 jre 一个是安装目录下的. 还有一个是安装后的自动安装的注意路径都不一样. 由于本人有强迫症所有不能容忍有两个 jre 目录的存在,所以果断删除了 D 盘下的.谨慎 ...

  5. Maven如何发布jar包到Nexus私库

    Nexus2可以通过管理界面来上传jar包到私库中,而最新的Nexus3却找不到了上传界面,只能通过以下方式来发布到私库. 发布第三方jar包 这种情况是maven远程仓库没有,本地有的第三方jar包 ...

  6. JQuery AJAX 通过一般处理程序 取列表

    由于上一篇的积累 这一个就简单了 也就是把反回了字符串 显示到table中 $("#btnSearch").click(function () { $.post("Cur ...

  7. 织梦自增函数[field:global name=autoindex/]常见用法

    看来不少朋友需要不了解这个自增函数的用法,在这里我列举一些常见的写法以及作用.   [field:global name=autoindex/] !--普通打印递增的数字-- [field:globa ...

  8. .NET Core 3.0之深入源码理解Startup的注册及运行

    原文:.NET Core 3.0之深入源码理解Startup的注册及运行   写在前面 开发.NET Core应用,直接映入眼帘的就是Startup类和Program类,它们是.NET Core应用程 ...

  9. ES6 学习 -- Generator函数

    (1)语法说明:Generator函数其实是一个普通函数,其有两个特点,一是,function关键字与函数名之间有一个星号(*):二是Generator函数内部使用yield表达式,定义不同的状态,然 ...

  10. 论文学习02-《On the Effectiveness of Visible Watermarks》

    I. Estimating the Matted Watermark 给定所有图像中的水印当前估计的区域,我们通过观察这些区域图像梯度的一致性来检测出水印梯度,也就是我们通过计算这些区域的图像梯度的中 ...