首页 技术 正文
技术 2022年11月8日
0 收藏 365 点赞 1,804 浏览 5063 个字

打算学完编译原理后再次实现它。。。

以下为比较“杂乱”的方法:

海量数据: https://pan.baidu.com/s/1Prd0ZqNLoCLLvXyJjCef3w

如果大家有发现这个程序的问题,请联系我,谢谢啦~~~ 我很疑惑,不知道错在哪里,难受。。。

 /*把黏在一起的代码以某种格式进行编排
编排方式:https://pintia.cn/problem-sets/994805046380707840/problems/994805047169236992
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define maxlen 1000000 char str[maxlen],s[maxlen];
long len,i,j,w=,g;
long sum_lock=,lock[maxlen],sum_lockif=,lockif[maxlen];
bool vis=false;
char c=' ',vis_kuo; //' ' ///删除多余的空格
void DelSpace()
{
while (str[i]==' ') //delete ' '
i++;
} ///'{'及换行操作
void EndOfLeft()
{
DelSpace();
if (str[i]=='{')
{
i++;
DelSpace();
///锁定,之后的位置(行首空格)大于等于lock[sum_lock]
sum_lock++;
lock[sum_lock]=w+;
}
printf("{\n");
w++;
for (j=;j<*w;j++)
// printf(" ");
printf("%c",c);
} ///_(condition)_
void Parenthese()
{
DelSpace();
// printf(" ");
printf("%c",c);
///c语言中语句有括号,这是定义,即if () while ();而pascal等语言可以没有
vis=false; g=;
while (!(vis && g==)) //not exit initially
{
printf("%c",str[i]);
if (str[i]=='(')
{
vis=true;
g++;
}
else if (str[i]==')') //the number of ')' is the same as '('
g--;
i++;
}
// printf(" ");
printf("%c",c);
EndOfLeft();
} ///遇到else的操作:上一个if的位置决定空格的情况
void elseOp()
{
while (w>lockif[sum_lockif]-) //-1
{
printf("\n");
w--;
for (j=;j<*w;j++)
// printf(" ");
printf("%c",c);
printf("}");
}
sum_lockif--;
printf("\n");
for (j=;j<*w;j++)
// printf(" ");
printf("%c",c);
// printf("else "); //add a ' '
printf("else%c",c);
i+=;
EndOfLeft();
} ///'}'及换行操作
void EndOfRight()
{
///先把已有的'}'输出
while ()
{
DelSpace();
if (str[i]!='}')
break;
i++;
printf("\n");
if (vis_kuo)
vis_kuo=false;
else
w--;
for (j=;j<*w;j++)
// printf(" ");
printf("%c",c);
printf("}");
sum_lock--; //解除锁定
} ///若接下来的几个字符为"else",不按lock的情况输出
strncpy(s,str+i,);
s[]='\0';
if (strcmp(s,"else")==)
{
elseOp();
return;
} /**
本身有'{'的,锁定'}'的输出,直到遇到对应的'}',才结束锁定
当被锁定时,输出的'}'的位置必须大于锁定的'{'的位置
*/
///输出到上一个'{'所在的空格位置
while (w>lock[sum_lock])
{
printf("\n");
w--;
for (j=;j<*w;j++)
// printf(" ");
printf("%c",c);
printf("}");
} printf("\n");
for (j=;j<*w;j++)
// printf(" ");
printf("%c",c);
} int main()
{
gets(str);
len=strlen(str);
i=;
DelSpace();
for (;i<len;)
{
///strncpy:add '\0'
// strncpy(s,str+i,6);
// s[6]='\0';
// if (strcmp(s,"main()")==0)
// {
// printf("main()\n");
// i+=6;
// EndOfLeft();
// continue;
// } strncpy(s,str+i,);
s[]='\0';
if (strcmp(s,"main(")==)
{
printf("main(");
i+=;
while (str[i]!=')')
{
printf("%c",str[i]);
i++;
}
printf(")\n");
i++;
///由后面的'{'处理,避免int main(){}
// EndOfLeft();
DelSpace();
continue;
} strncpy(s,str+i,);
s[]='\0';
if (strcmp(s,"for")==)
{
printf("for");
i+=;
Parenthese();
continue;
} strncpy(s,str+i,);
s[]='\0';
if (strcmp(s,"while")==)
{
printf("while");
i+=;
Parenthese();
continue;
} strncpy(s,str+i,);
s[]='\0';
if (strcmp(s,"if")==)
{
sum_lockif++; //锁定if,在遇到else之前,则输出的'}'的位置必须大于锁定的if的位置
lockif[sum_lockif]=w+;
printf("if");
i+=;
vis=true;
Parenthese();
vis=false;
continue;
} strncpy(s,str+i,);
s[]='\0';
if (strcmp(s,"else")==)
{
elseOp();
continue;
}
///程序体的'{',除int main,if,for后面附带的'{'之外
if (str[i]=='{')
{
i++;
DelSpace();
printf("{");
sum_lock++;
lock[sum_lock]=w+; ///如果'{'后面接')',即{},则无增' '输出
if (str[i]=='}')
{
vis_kuo=true;
EndOfRight();
continue;
} printf("\n");
w++;
for (j=;j<*w;j++)
printf(" ");
continue;
}
///程序体的'}',除int main,if,for后面附带的'}'之外
if (str[i]=='}')
{
EndOfRight();
continue;
}
if (str[i]==';')
{
printf(";");
i++;
EndOfRight();
continue;
}
printf("%c",str[i]);
i++;
}
return ;
}
/*
int main(){ int a=3; int b=2; int c=1; int d=0;}
*/
/*
int main(){int a=0; if (3>2) printf("Y"); int b=2; }
*/
/*
int main() {if (3>2) { printf("Y"); } return 0;}
*/
/*
int main() {if (3>2) printf("Y"); return 0;}
*/
/*
int main(){if (3>2) {if (4>3) { if (5>3) {int i=3;} }}}
*/
/*
int main(){if (3>2) if (4>3) if (5>3) int i=3;}
*/
/*
int main() {if (3>2) while (3>2) {if (3>2) printf("Y"); if (3>2) printf("Y"); } return 0;}
*/ /*
int main() { if (3>2) if (4>3) if (6>-2) printf("Y\n"); else if (3>2) printf("Y\n"); else printf("N\n"); return 0; }
*/
/*
int main(){if (3>2) while(3>2){if (3>2) return 3; else return 2; }else while(3>2){while(5>3) printf("Y");} return 0;}
*/
/*
int main(){if (3>2) while(3>2)if (3>2) return 3; else return 2; else while(3>2){while(5>3) printf("Y");} return 0;}
*/
/*
int main(){int i=0; if (2<3) {if (3>2) while (i<2) while (i<3) i++; printf("Y"); } return 0; }
*/
/*
int main(){int i=0; if (3>2) while (i<2) while (i<3) i++; else i--; return 0;}
*/
/*
int main(){int i=0; if (3>2) while (i<2) { if (3>2) printf("Y"); else printf("N"); while (i<3) i++;} else i--; return 0;}
*/
/*
int main(){int i=0; if (3>2) while (i<2) while (i<3) if (3>2) printf("Y"); else printf("N"); else i--; return 0;}
*/
/*
int main(){int i=0; if (3>2) i++;else if (3>2) i++;else if (3>2)i++; return 0;}
*/
/*
int main(){ if (3>2) while (3>2) while (4>3) i++; else while (4>3) j--; }
*/ /*
int main(){while (1>3) while (3>2) while (5>3) i++; j--; k++;}
*/
/*
int main(){while (1>3) if (3>2) printf("Y\n"); }
*/
/*
int main(){while (1>3) if (3>2) printf("Y\n"); else printf("N"); }
*/ /*
int main() { return 0; }
*/
/*
int main(){}
*/
/*
int main() { int i=3; {int i=2;} }
*/
/*
int main() { {int i=2;} if (3>2) if (4>3) else 5>3; }
*/
/*
int main() { if (3>2) i++; {int i=2;} {int i=2;} }
*/
/*
int main() { {int i=2;} { {} } {} }
*/
/*
int main() { {int i=2;} { {i=3; {}} {} } }
*/
/*
int main() {{{{}}}}
*/
/*
int main() { for (int i=1;i<=3;i++) for (int j=1;j<=5;j++) k++; }
*/
/*
int main() { for (int i=1;i<=3;i++) while (3>2) if(3>2) printf("Y\n"}
*/
/*
int main() { if (3>2) i++; else j--; for (i=1;i<=5;i++) j++; while (3>2) i-; }
*/
/*
int main() { if (3>2) { i++; } else { j--; } for (i=1;i<=5;i++) { j++; } while (3>2) { i-; } }
*/
/*
int main() { if ( 3>2 ) i++; else if ( 5>3 &2<3) j--; for ( i=1;i<=3;i++ ) i--; while (1>3 ) j--; }
*/
/*
int main() { if ( (3>2) && (4>3 | 3>2) ) i++;}
*/
/*
int main() { if ( if (3>2) i++; ) i++;}
*/
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292