团体程序设计天梯赛L3-019 代码编排(23分)

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

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

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

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

  1 /*把黏在一起的代码以某种格式进行编排
  2 编排方式:https://pintia.cn/problem-sets/994805046380707840/problems/994805047169236992
  3 */
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 #include <string.h>
  7 #include <stdbool.h>
  8 #define maxlen 1000000
  9 
 10 char str[maxlen],s[maxlen];
 11 long len,i,j,w=0,g;
 12 long sum_lock=0,lock[maxlen],sum_lockif=0,lockif[maxlen];
 13 bool vis=false;
 14 char c=' ',vis_kuo; //' '
 15 
 16 ///删除多余的空格
 17 void DelSpace()
 18 {
 19     while (str[i]==' ')            //delete ' '
 20         i++;
 21 }
 22 
 23 ///'{'及换行操作
 24 void EndOfLeft()
 25 {
 26     DelSpace();
 27     if (str[i]=='{')
 28     {
 29         i++;
 30         DelSpace();
 31         ///锁定,之后的位置(行首空格)大于等于lock[sum_lock]
 32         sum_lock++;
 33         lock[sum_lock]=w+1;
 34     }
 35     printf("{\n");
 36     w++;
 37     for (j=0;j<2*w;j++)
 38 //        printf(" ");
 39         printf("%c",c);
 40 }
 41 
 42 ///_(condition)_
 43 void Parenthese()
 44 {
 45     DelSpace();
 46 //    printf(" ");
 47     printf("%c",c);
 48     ///c语言中语句有括号,这是定义,即if () while ();而pascal等语言可以没有
 49     vis=false; g=0;
 50     while (!(vis && g==0))        //not exit initially
 51     {
 52         printf("%c",str[i]);
 53         if (str[i]=='(')
 54         {
 55             vis=true;
 56             g++;
 57         }
 58         else if (str[i]==')')    //the number of ')' is the same as '('
 59             g--;
 60         i++;
 61     }
 62 //    printf(" ");
 63     printf("%c",c);
 64     EndOfLeft();
 65 }
 66 
 67 ///遇到else的操作:上一个if的位置决定空格的情况
 68 void elseOp()
 69 {
 70     while (w>lockif[sum_lockif]-1)  //-1
 71     {
 72         printf("\n");
 73         w--;
 74         for (j=0;j<2*w;j++)
 75 //            printf(" ");
 76             printf("%c",c);
 77         printf("}");
 78     }
 79     sum_lockif--;
 80     printf("\n");
 81     for (j=0;j<2*w;j++)
 82 //        printf(" ");
 83         printf("%c",c);
 84 //    printf("else ");    //add a ' '
 85     printf("else%c",c);
 86     i+=4;
 87     EndOfLeft();
 88 }
 89 
 90 ///'}'及换行操作
 91 void EndOfRight()
 92 {
 93     ///先把已有的'}'输出
 94     while (1)
 95     {
 96         DelSpace();
 97         if (str[i]!='}')
 98             break;
 99         i++;
100         printf("\n");
101         if (vis_kuo)
102             vis_kuo=false;
103         else
104             w--;
105         for (j=0;j<2*w;j++)
106 //            printf(" ");
107             printf("%c",c);
108         printf("}");
109         sum_lock--;            //解除锁定
110     }
111 
112     ///若接下来的几个字符为"else",不按lock的情况输出
113     strncpy(s,str+i,4);
114     s[4]='\0';
115     if (strcmp(s,"else")==0)
116     {
117         elseOp();
118         return;
119     }
120 
121     /**
122         本身有'{'的,锁定'}'的输出,直到遇到对应的'}',才结束锁定
123         当被锁定时,输出的'}'的位置必须大于锁定的'{'的位置
124     */
125     ///输出到上一个'{'所在的空格位置
126     while (w>lock[sum_lock])
127     {
128         printf("\n");
129         w--;
130         for (j=0;j<2*w;j++)
131 //            printf(" ");
132             printf("%c",c);
133         printf("}");
134     }
135 
136     printf("\n");
137     for (j=0;j<2*w;j++)
138 //        printf(" ");
139         printf("%c",c);
140 }
141 
142 int main()
143 {
144     gets(str);
145     len=strlen(str);
146     i=0;
147     DelSpace();
148     for (;i<len;)
149     {
150         ///strncpy:add '\0'
151 //        strncpy(s,str+i,6);
152 //        s[6]='\0';
153 //        if (strcmp(s,"main()")==0)
154 //        {
155 //            printf("main()\n");
156 //            i+=6;
157 //            EndOfLeft();
158 //            continue;
159 //        }
160 
161         strncpy(s,str+i,5);
162         s[5]='\0';
163         if (strcmp(s,"main(")==0)
164         {
165             printf("main(");
166             i+=5;
167             while (str[i]!=')')
168             {
169                 printf("%c",str[i]);
170                 i++;
171             }
172             printf(")\n");
173             i++;
174             ///由后面的'{'处理,避免int main(){}
175 //            EndOfLeft();
176             DelSpace();
177             continue;
178         }
179 
180         strncpy(s,str+i,3);
181         s[3]='\0';
182         if (strcmp(s,"for")==0)
183         {
184             printf("for");
185             i+=3;
186             Parenthese();
187             continue;
188         }
189 
190         strncpy(s,str+i,5);
191         s[5]='\0';
192         if (strcmp(s,"while")==0)
193         {
194             printf("while");
195             i+=5;
196             Parenthese();
197             continue;
198         }
199 
200         strncpy(s,str+i,2);
201         s[2]='\0';
202         if (strcmp(s,"if")==0)
203         {
204             sum_lockif++;            //锁定if,在遇到else之前,则输出的'}'的位置必须大于锁定的if的位置
205             lockif[sum_lockif]=w+1;
206             printf("if");
207             i+=2;
208             vis=true;
209             Parenthese();
210             vis=false;
211             continue;
212         }
213 
214         strncpy(s,str+i,4);
215         s[4]='\0';
216         if (strcmp(s,"else")==0)
217         {
218             elseOp();
219             continue;
220         }
221         ///程序体的'{',除int main,if,for后面附带的'{'之外
222         if (str[i]=='{')
223         {
224             i++;
225             DelSpace();
226             printf("{");
227             sum_lock++;
228             lock[sum_lock]=w+1;
229 
230             ///如果'{'后面接')',即{},则无增' '输出
231             if (str[i]=='}')
232             {
233                 vis_kuo=true;
234                 EndOfRight();
235                 continue;
236             }
237 
238             printf("\n");
239             w++;
240             for (j=0;j<2*w;j++)
241                 printf(" ");
242             continue;
243         }
244         ///程序体的'}',除int main,if,for后面附带的'}'之外
245         if (str[i]=='}')
246         {
247             EndOfRight();
248             continue;
249         }
250         if (str[i]==';')
251         {
252             printf(";");
253             i++;
254             EndOfRight();
255             continue;
256         }
257             printf("%c",str[i]);
258             i++;
259     }
260     return 0;
261 }
262 /*
263 int main(){ int a=3; int b=2; int c=1; int d=0;}
264 */
265 /*
266 int main(){int a=0; if (3>2) printf("Y"); int b=2;  }
267 */
268 /*
269 int main() {if (3>2) { printf("Y"); } return 0;}
270 */
271 /*
272 int main() {if (3>2) printf("Y"); return 0;}
273 */
274 /*
275 int main(){if (3>2) {if (4>3) { if (5>3) {int i=3;}   }}}
276 */
277 /*
278 int main(){if (3>2)  if (4>3) if (5>3) int i=3;}
279 */
280 /*
281 int main() {if (3>2) while (3>2) {if (3>2) printf("Y"); if (3>2) printf("Y"); } return 0;}
282 */
283 
284 /*
285 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; }
286 */
287 /*
288 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;}
289 */
290 /*
291 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;}
292 */
293 /*
294 int main(){int i=0; if (2<3) {if (3>2) while (i<2) while (i<3) i++; printf("Y"); } return 0; }
295 */
296 /*
297 int main(){int i=0; if (3>2) while (i<2) while (i<3) i++; else i--; return 0;}
298 */
299 /*
300 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;}
301 */
302 /*
303 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;}
304 */
305 /*
306 int main(){int i=0; if (3>2) i++;else if (3>2) i++;else if (3>2)i++; return 0;}
307 */
308 /*
309 int main(){ if (3>2) while (3>2) while (4>3) i++; else while (4>3) j--; }
310 */
311 
312 /*
313 int main(){while (1>3)  while (3>2) while (5>3) i++; j--; k++;}
314 */
315 /*
316 int main(){while (1>3) if (3>2) printf("Y\n"); }
317 */
318 /*
319 int main(){while (1>3) if (3>2) printf("Y\n"); else printf("N"); }
320 */
321 
322 
323 /*
324    int main()  {   return 0;   }
325 */
326 /*
327 int main(){}
328 */
329 /*
330 int main() { int i=3; {int i=2;} }
331 */
332 /*
333 int main() { {int i=2;} if (3>2) if (4>3) else 5>3; }
334 */
335 /*
336 int main() { if (3>2) i++; {int i=2;} {int i=2;} }
337 */
338 /*
339 int main() { {int i=2;} { {} } {} }
340 */
341 /*
342 int main() { {int i=2;} { {i=3; {}} {} } }
343 */
344 /*
345 int main() {{{{}}}}
346 */
347 /*
348 int main()  { for (int i=1;i<=3;i++) for (int j=1;j<=5;j++) k++; }
349 */
350 /*
351 int main() { for (int i=1;i<=3;i++) while (3>2) if(3>2) printf("Y\n"}
352 */
353 /*
354      int main()  {    if           (3>2)        i++;    else    j--;    for (i=1;i<=5;i++)              j++;      while          (3>2)    i-;        }
355 */
356 /*
357      int main()  {    if           (3>2)   {     i++;   }  else   {   j--;  }  for (i=1;i<=5;i++)   {           j++;    }  while          (3>2)   {  i-;   }     }
358 */
359 /*
360 int main() { if (  3>2   )  i++;  else if (   5>3 &2<3) j--;  for (  i=1;i<=3;i++   )    i--;   while (1>3   )  j--; }
361 */
362 /*
363 int main() { if ( (3>2) && (4>3 | 3>2)  ) i++;}
364 */
365 /*
366 int main() { if ( if (3>2) i++;  ) i++;}
367 */

猜你喜欢

转载自www.cnblogs.com/cmyg/p/9221899.html