brainf*ck,其实就是brainfuck,一种语言,
http://zh.wikipedia.org/wiki/Brainfuck
模拟题
#include <stdio.h>
#include <string.h>
const int M=32768;
unsigned char str[M];
char in[200000], cmd[200000];
int n;
int main()
{
scanf("%d%*c", &n);
for ( int cases = 0 ; cases < n ; cases++ )
{
int t=0, i, j, point = 0;
while ( gets(in), in[0] != 'e' )
{
for ( i = 0 ; in[i] ; i++ )
{
if ( ' ' == in[i] ) continue;
else if ( '%' == in[i] ) break;
else if ( '[' == in[i] ) t++;
else if ( ']' == in[i] ) t--;
cmd[point++]= in[i];
}
}
cmd[point]=0;
printf("PROGRAM #%d:\n", cases+1);
if ( t != 0 )
{
printf("COMPILE ERROR\n");
continue;
}
int p= 0;
memset (str, 0, sizeof(str));
for ( i = 0 ; cmd[i] ; i++ )
{
if ( cmd[i] == '>' )
p=(p+1)%M;
else if ( cmd[i] == '<' )
p=(p+M-1)%M;
else if ( cmd[i] == '+' )
str[p]++;
else if ( cmd[i] == '-' )
str[p]--;
else if ( cmd[i] == '.' )
putchar(str[p]);
else if ( cmd[i] == '[' && !str[p] )
{
i++;
for ( t = 1; 0 != t ; i++ )
{
if ( '[' == cmd[i] ) t++;
else if ( ']' == cmd[i] ) t--;
}
i--;
}
else if ( cmd[i] == ']' && str[p] )
{
i--;
for ( t = -1; 0 != t ; i-- )
{
if ( '[' == cmd[i] ) t++;
else if ( ']' == cmd[i] ) t--;
}
i++;
}
}
putchar(10);
}
return 0;
}