1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int expand(char *dst,char ch,int amount){
#define WRITE(s__) {int l__=strlen(s__); if(dst){memcpy(dst,s__,l__+1); dst+=l__;} written+=l__;}
#define WGOTO(i__) {while(curpos<i__){WRITE(">"); curpos++;} while(curpos>i__){WRITE("<"); curpos--;}}
#define WMOVE(i1__,i2__) {int p__=curpos; WGOTO(i1__); WRITE("[-"); WGOTO(i2__); WRITE("+"); WGOTO(i1__); WRITE("]"); WGOTO(p__);}
int written=0,curpos=0;
switch(str[i]){
case '+': //strcpy(p,"+>+[->+<]>[<<->>[-<+>]]<<"); p+=25; break;
case '+': //strcpy(p,"+>+[<->[->+<]]>[-<+>]<<"); p+=23; break;
case '+':
WRITE("+");
for(int i=1;i<amount;i++){WGOTO(i); WRITE("+");}
for(int i=1;i<amount;i++){
WGOTO(i);
WRITE("[");
WRITE("<->");
}
case '-': strcpy(p,"->[->+<]>[<<+>>[-<+>]]<-<"); p+=25; break;
case '[': strcpy(p,"[->>+>>>+<<<<<]>>>>>[<<<[-<<+>>]+>>>[-]]<<<<[->+>>>+<<<<]>>>>[[-<<<-<+>>>>]<<<+>>>]<<<[[-]<<"); p+=92; break;
case ']': strcpy(p,"[->>+>>>+<<<<<]>>>>>[<<<[-<<+>>]+>>>[-]]<<<<[->+>>>+<<<<]>>>>[[-<<<-<+>>>>]<<<+>>>]<<<]<<"); p+=89; break;
case '>': strcpy(p,">>>"); p+=3; break;
case '<': strcpy(p,"<<<"); p+=3; break;
case ',': strcpy(p,"[-]>,<"); p+=6; break;
case '.': strcpy(p,">.<"); p+=3; break;
default: *(p++)=str[i]; break;
}
return res;
#undef WRITE
#undef WGOTO
#undef WMOVE
}
int main(void){
int amount=1;
char c;
while((c=getchar())!=EOF){
if(c=='{'){
amount++;
} else if(c=='}'){
amount--;
if(amount<=0){
fprintf(stderr,"More '}' than '{'\n");
return 1;
}
} else if(amount==1){
putchar(c);
} else {
int len=expand(NULL,c,amount);
char *str=malloc(len+1);
expand(str,c,amount);
printf("%s",str);
free(str);
}
}
}
|