http://poj.org/problem?id=3972
BF
Description
BF is a language that was designed to challenge and amuse programmers. BF uses a simple machine model consisting, besides the program, of:
In this problem you are asked to implement a BF interpreter, your program should read a BF program and an input stream and should print the output stream. BF syntax is easy, only the following commands are available:
The given BF programs will be syntactically valid also the ‘>’ and ‘<’ operators will never lead to a value outside range [0-30000]. Input
Input starts with the number of test cases on a line by itself. Then a number of test cases; each test case is formatted as follows.
A test case may span on multiple lines and each two consecutive test cases are separated by a blank line. The embedded text could be any characters except the dollar sign terminator, you should ONLY process the 8 commands and detect the end of program/input by the dollar signs, discard any other characters when processing. In the sample input only the relevant input is underlined, however in the real test cases there will be no signs of input termination except for the dollar signs. Output
Output should be “Case “ then test case number (starting with 1) , a colon, a space followed by the contents of the BF program output stream and a new line after each test case.
Sample Input 3 ,>,>,>,.<.<.<. $ abcde $ This is a sample BF program ,>++++++[<-------->-],[<+>-]<. That Prints the summation of 2 numbers $ 23 $ ,>,> ++++[<++++++++>-]<. >++++[<<++++++++>>-]<<. Adds to every cell 8*4=32 and prints 'em in reverse order $ AB $ Sample Output Case 1: dcba Case 2: 5 Case 3: ba Source |
给出BF程式的命令(只含有> < + - , . [ ])即输入流($ $之间的)根据命令操作数组,输出输出流。
其他字符(样例中无下划线的)为注释,直接忽略
只要看懂这8个命令的含义就能切掉的水题
>数组光标向后移1位;
<数组光标向前移1位;
+数组当前光标所指位置ascll码+1;
-数组当前光标所指位置ascll码-1;
.输出当前光标所指字符至输出流;
,从输入流读入一个字符到光标位置;
[数组当前光标所指位置ascll码为0时,命令跳转到与其对应的]的后一位
]数组当前光标所指位置ascll码不为0时,命令跳转到与其对应的[的后一位
注意:数组位置从0开始,且是一个长度为30000的循环数组,[ ]要找到配对位置(用栈处理)
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
using namespace std;
char stdcmd[]={"><+-,.[]\0"};
string cmd,in;
char out[30001];
int nex[30001];
char c;
int pc,pi,po;
stack<int> st;
int main()
{
// freopen("in.txt","r",stdin);
int tt;
scanf("%d",&tt);getchar();
for (int t=1;t<=tt;t++)
{
bool f=true;
cmd=in="";
memset(out,0,sizeof(out));
memset(nex,0,sizeof(nex));
while (f)
{
c=getchar();
if (c=='$') break;
if (strchr(stdcmd,c)!=NULL)
cmd+=c;
}
getchar();
while (f)
{
c=getchar();
if (c=='$') break;
in+=c;
}
int last;
while (!st.empty()) st.pop();
for (int i=0;i<cmd.size();i++)
{
if (cmd[i]=='[') st.push(i);
if (cmd[i]==']') {nex[i]=st.top();st.pop();}
}
while (!st.empty()) st.pop();
for (int i=cmd.size()-1;i>=0;i--)
{
if (cmd[i]==']') st.push(i);
if (cmd[i]=='[') {nex[i]=st.top();st.pop();}
}
/*
for (int i=0;i<cmd.size();i++)
{
printf("%d ",nex[i]);
}
puts("");
*/
pc=0;
pi=0;
po=0;
printf("Case %d: ",t);
// puts(cmd.c_str());
// puts(in.c_str());
while (pc!=cmd.size())
{
if (po==30000) po=0;
if (po==-1) po=29999;
//printf("pc=%d\n",pc);
//system("pause");
if (cmd[pc]=='<')
{
po--;
pc++;
continue;
}
if (cmd[pc]=='>')
{
po++;
pc++;
continue;
}
if (cmd[pc]=='+')
{
out[po]++;
pc++;
continue;
}
if (cmd[pc]=='-')
{
out[po]--;
pc++;
continue;
}
if (cmd[pc]=='.')
{
putchar(out[po]);
//printf("out[po]=%d\n",out[po]);
pc++;
continue;
}
if (cmd[pc]==',')
{
out[po]=in[pi++];
pc++;
continue;
}
if (cmd[pc]=='[')
{
if (out[po]=='\0')
pc=nex[pc];
pc++;
continue;
}
if (cmd[pc]==']')
{
if (out[po]!='\0')
pc=nex[pc];
pc++;
continue;
}
}
puts("");
}
return 0;
}