团体程序设计天梯赛L3-019 代码排版(23分)
打算学完编译原理后再次实现它。。。
以下为比较“杂乱”的方法:
海量数据: 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++;}
*/
团体程序设计天梯赛L3-019 代码排版(23分)的更多相关文章
- 团体程序设计天梯赛(CCCC) L3019 代码排版 方法与编译原理密切相关,只有一个测试点段错误
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code
- PTA|团体程序设计天梯赛-练习题目题解锦集(C/C++)(持续更新中……)
PTA|团体程序设计天梯赛-练习题目题解锦集(持续更新中) 实现语言:C/C++: 欢迎各位看官交流讨论.指导题解错误:或者分享更快的方法!! 题目链接:https://pintia.cn/ ...
- 团体程序设计天梯赛(CCCC) L3021 神坛 的一些错误做法(目前网上的方法没一个是对的) 和 一些想法
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code
- 团体程序设计天梯赛(CCCC) L3015 球队“食物链” 状态压缩
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code #include <cstdio> #include ...
- 团体程序设计天梯赛(CCCC) L3014 周游世界 BFS证明
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code
- 团体程序设计天梯赛(CCCC) L3013 非常弹的球 不同思路
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code
- 团体程序设计天梯赛(CCCC) L3012 水果忍者 上凸或下凹的证明
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code #include <cstdio> #include ...
- 团体程序设计天梯赛(CCCC) L3009 长城 方法证明
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code
- PTA L2-001 紧急救援-最短路(Dijkstra)多条最短路找最优解并输出路径 团体程序设计天梯赛-练习集
L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快 ...
- 树状数组+二分答案查询第k大的数 (团体程序设计天梯赛 L3-002. 堆栈)
前提是数的范围较小 1 数据范围:O(n) 2 查第k大的数i:log(n)(树状数组查询小于等于i的数目)*log(n)(二分找到i) 3 添加:log(n) (树状数组) 4 删除:log(n) ...
随机推荐
- Java 内存模型_2
title: Java 内存模型_2 date: 2017-01-28 02:04:06 tags: [JMM] categories: [Programming,Java] --- Why 理解 J ...
- __construct 与 __destruct 区别
其实这个问法是有问题的,__construct 与 __destruct 没什么可比性,两个方法一个在对象被创建的时候触发,另一个在对象被销毁的时候触发 具体可以翻阅PHP官方手册中的 http:// ...
- Python序列之列表 (list)
作者博文地址:http://www.cnblogs.com/spiritman/ 列表是Python中最基本的数据结构,是Python最常用的数据类型.Python列表是任意对象的有序集合,通过索引访 ...
- 使用git-premit时的问题
package.json 相关配置如下 { "scripts": { "lint": "eslint pages/* component/* --fi ...
- hive创建临时函数
add jar /home/hjl/sunwg/Lower.jar; create temporary function my_lower as ‘com.example.hive.udf.Lower ...
- Alpha版本BUG BASH
在本次软件开发的第一轮迭代中,我们团队遇到了很多问题.首先是和学长联系不上导致拿到项目前一版本的代码的时间延后了一个星期. 拿到代码后发现由于安装环境的问题代码无法移植.在这一阶段我们就耗费了大量的时 ...
- 面向对象OO第9-11次作业总结
面向对象OO第9-11次作业总结 1.关于规格化设计的调研程序规格说明:对程序所应满足的要求,以可验证的方式作出完全.精确陈述的文件.“规格说明”一词与其他工业产品的“规格说明书”有相似的含义.不过, ...
- iOS开发学习-放大长图与屏幕等宽
/* 需要得到一个图片的放大比例,这个比例就是屏幕的宽度与图片真实宽度的比值 */ CGFloat newZoomScale = LZ_SCREEN_WIDTH / [_photoImageView. ...
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配
题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...
- HTML和CSS <h1> --1-- <h1>
Html和CSS的关系 学习web前端开发基础技术需要掌握:HTML.CSS.JavaScript语言.下面我们就来了解下这三门技术都是用来实现什么的: 1. HTML是网页内容的载体.内容就是网页制 ...