Pascal代码自动格式化
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代码自动格式化的更多相关文章
- 关于eclispe保存代码自动格式化的设置
最近在项目开发,上级要求所有开发人员,代码必须格式和(Ctrl+Shift+F),但是还是会偶尔忘记格式化,今天看了有一种保存之后eclipse可以自动格式代码的设置 1.请大家在eclipse设置下 ...
- visual studio粘贴html代码.会自行添加一些未知代码(自动格式化)
比如我们要粘贴这样一段代码到VS这里 <option {if empty($param.is_active)}selected=selected{/if}>请选择</option&g ...
- Eclipse中对Python代码自动格式化!
在Eclipse中使用PyDev发现无法进行代码格式化,使用通常的"Ctrl+Shift+F"快捷键,没有任何变化,难道不能对python代码格式化了吗? 通过下面设置,就可以每次 ...
- vue eslint 代码自动格式化
vue-cli 代码风格为 JavaScript Standard Style 代码检查规范严格,一不小心就无法运行,使用eslint的autoFixOnSave可以在保存代码的时候自动格式化代码 V ...
- Myeclipse 自定义代码自动格式化(ctrl+alt+F)
打开如图界面:preference->java->code style->formatter下的edit... 如设设置java代码多长换行:
- vim中代码自动格式化
参考资料: https://blog.csdn.net/qachenzude/article/details/25511875 1,gg 跳转到第一行 2,shift+v 转到可视模式 3,shift ...
- spotless-maven-plugin java代码自动格式化mvn spotless:apply -fn
<plugin> <groupId>com.diffplug.spotless</groupId> <artifactId>spotless-maven ...
- VS Code中python代码自动格式化 代码自动检查
VS Code菜单栏中依次打开 File—Preferences—Settings,搜索框中搜索“python.formatting.provider”, 然后在下拉菜单中选择autopep8,yap ...
- 解决vscode按下ctrl+S的时候自动格式化
按下ctrl+S的时候自动格式化 为什么需要这种操作? 优点: 保存的时候格式化,让我们的代码自动格式化,减少人工调整. 缺点: 有一些打好包的JS有时候修改一下,但不需要格式化,因为打好包就是要压缩 ...
随机推荐
- BOM window对象方法
window对象方法 alert():弹出一个警告对话框. prompt():弹出一个输入对话框. confirm():弹出一个确认对话框.如果单击“确定按钮”返回true,如果单击“取消 ...
- PAT_A1004#Counting Leaves
Source: PAT A1004 Counting Leaves (30 分) Description: A family hierarchy is usually presented by a p ...
- Quartz2作业监听
在本教程中,我们将展示/介绍如何创建一个JobListener,跟踪运行工作状态在作业完成等. P.S 这个例子是Quartz 2.1.5 1. Quartz 作业 作业 - 用于打印一个简单的信息, ...
- 《转》python 10 集合
自 http://www.cnblogs.com/BeginMan/p/3160565.html 一.目录 1.集合概述 2.关于集合的操作符.关系符号 3.集合的一系列操作(添加.更新.访问.删除) ...
- 搭建jeecg-boot项目运行
实验版本: 2.0.2(发布日期:20190708) 项目地址:https://github.com/zhangdaiscott/jeecg-boot 说明文档:http://jeecg-boot.m ...
- 使用idea从svn检出项目
1.创建空的工程 2. 3. 编辑项目进行提交svn服务器进行自动整合
- 02->交互式图形学--用glut库实现Sierpinski镂垫
Sierpinski图形是一类有趣的图形,本文结合交互式图形学教材,讲解Sierpinski镂垫图形生成,作为入门图形学编程的第一课.为了简便,本文就2D来进行讨论.实验环境见01 算法 主要是根据随 ...
- [转]从客户端中检测到有潜在危险的 Request.Form 值。
参考资料: ASP.NET 4.0中使用FreeTextBox和FCKeditor遇到安全问题警告的解决办法关于问题出现的原因说的很清楚 引言 本人在.NET 4.0+VS2010环境下调试一个ASP ...
- JS事件 鼠标经过事件(onmouseover)鼠标经过事件,当鼠标移到一个对象上时,该对象就触发onmouseover事件,并执行onmouseover事件调用的程序。
鼠标经过事件(onmouseover) 鼠标经过事件,当鼠标移到一个对象上时,该对象就触发onmouseover事件,并执行onmouseover事件调用的程序. 现实鼠标经过"确定&quo ...
- java字符串简单介绍
String:String对象初始化之后不可变线程安全简单的字符串操作使用String效率更高 StringBuffer:StringBuffer对象初始化之后可改变线程安全频繁的字符串操作可以使用S ...