- 论坛徽章:
- 3
|
以上是原理,我就不写数学证明了.
现在来实现编码,
假设编程习惯不像以下这样恶心:
int func(){}int func2{}
函数写到一行去,那么以下是可以使用的代码
- linux-0gt0:/tmp/test # cat 2.sed
- #!/bin/sed -nrf
- /int[ \t]+test[ \t]*\(/!d
- h
- :a
- n
- H
- /\{/ {
- s/[^{}]//g
- H
- bb
- }
- ba
- :b
- n
- H
- :c
- s/[^{}]//g
- tc #clear s
- H
- x
- s/(\n[^\n]*)(\n[^\n]*)\n([^\n]*)$/\2\1\3/
- td
- :d
- s/(\n[^\n]*)\{\}([^\n]*)$/\1\2/
- td
- /\n$/ {
- s/\n$//p
- s/^[ \t]*int[ \t]+test/&_a/p
- q
- }
- x
- bb
- linux-0gt0:/tmp/test # cat test.c
- #include <stdio.h>
- int test()
- {
- { int i=0;{printf("%d",i);}
- printf("%d\n",1);for(i=0;i<100;i++){printf("%d",i);{int j;for(j=0;j<100;j++){printf("test");}}}
- }
- }
- int main()
- {
- return 0;
- }
- linux-0gt0:/tmp/test # ./2.sed test.c
- int test()
- {
- { int i=0;{printf("%d",i);}
- printf("%d\n",1);for(i=0;i<100;i++){printf("%d",i);{int j;for(j=0;j<100;j++){printf("test");}}}
- }
- }
- int test_a()
- {
- { int i=0;{printf("%d",i);}
- printf("%d\n",1);for(i=0;i<100;i++){printf("%d",i);{int j;for(j=0;j<100;j++){printf("test");}}}
- }
- }
复制代码 |
|