sicily 1205. brainf*ck

1205. brainf*ck

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

brainf*ck is the ungodly creation of Urban Mller, whose goal was apparently to create a Turing-complete language for which he could write the smallest compiler ever. http://en.wikipedia.org defines it as ``a computer programming language designed to challenge and amuse programmers, and is not suitable for practical use. Its name has been variously euphemized, as in brainf*ck."

A brainf*ck program has an implicit byte pointer, called ``the pointer", which is free to move around within an array of 32768 bytes, initially all set to zero. The pointer itself is initialized to point to the beginning of this array.

The brainf*ck programming language consists of seven commands, each of which is represented as a single character. Note: ``Industry standard" brainf*ck actually has eight commands, but for the purposes of this problem one command was intentionally omitted.

 


COMMANDOPERATION
>Increment the pointer.
 Incrementing a pointer value of 32767
 results in a pointer value of 0.
<Decrement the pointer.
 Decrementing a pointer value of 0
 results in a pointer value of 32767.
+Increment the byte at the pointer.
 Incrementing the byte value 255 results
 in the byte value 0.
-Decrement the byte at the pointer.
 Decrementing the byte value 0 results
 in the byte value 255.
.Output the character whose ASCII
 value is the byte at the pointer
[Jump forward past the matching ] if the
 byte at the pointer is zero.
]Jump backward to the matching [
 unless the byte at the pointer is zero.

 


For this problem, you will write a program that reads in, parses and executes a brainf*ck program.

Input

The first line of input contains an integer N , (1<=N<=100) , which is the number of brainf*ck programs that follow. Each program consists of one or more lines of brainf*ck commands ending with a line that consists of the word `end'. Your program should ignore any illegal characters (I.E. any character not in the set: <>+=.[]), If a percent sign (%) is encountered during parsing, the remainder of the line should be discarded. This constitutes a comment. The maximum number of commands in a brainf*ck program is 128000.

Output

For each brainf*ck program, your program should output the text `PROGRAM #n :' on a single line (where n is the program number: 1<=n<=N ), followed by the output generated by the brainf*ck program, followed by a single newline character. The only possible parsing error that can occur is if there is an unmatched [ or ] in the brainf*ck program. If your program encounters such an error, it should simply print `COMPILE ERROR' instead of executing the program. All brainf*ck programs will use no more than the specified 32768 bytes of memory.

Sample Input

3 
++++++++[>+++++++++ % hello-world.
<-]>.<+++++[>++++++<-]>-.+++++++.. 
+++.<++++++++[>>++++<<-]>>.<<++++[> 
------<-]>.<++++[>++++++<-]>.+++. 
------.--------.>+. 
end 
+++[>+++++++[. 
end 
%% Print alphabet, A-Z. 
+ + + + + +++++++++++++++++++++> 
++++++++++++++++++++++++++++++++ 
++++++++++++++++++++++++++++++++ 
+< [ >.+<- ] 
end

Sample Output

PROGRAM #1: 
Hello World! 
PROGRAM #2: 
COMPILE ERROR 
PROGRAM #3: 
ABCDEFGHIJKLMNOPQRSTUVWXYZ

题目分析

根据题意,模拟操作一个32768维,范围为[0,255]的数组
耐心点读题目即可
一,注意提前剪枝判断COMPILE ERROR
二,读到'[',如果data[pointer]==0,往前跳的时候,是跳到匹配的']'的前一个字符,同理处理']'
三,在输入的代码里面有' ',是不需要处理的
四,请无视sample里面每行末尾的空格


#include <iostream>
#include <memory.h>

int data[32768];

int main()
{
  int num;
  std::cin >> num;
  std::string line;
  std::string code;
  int match;
  for (int id = 1; id <= num; ++id) {
    code = "";
    match = 0;
    while (getline(std::cin, line) && line!="end") {
      for (int i = 0; i < line.length(); ++i) {
        if (line[i] == '%')
          break;
        else if (line[i] == '[')
          match++;
        else if (line[i] == ']')
          match--;
        else if (line[i] == ' ')
          continue;
        code += line[i];
      }
    }
    std::cout << "PROGRAM #" << id << ":" << std::endl;
    if (match != 0) {
      std::cout << "COMPILE ERROR" << std::endl;
      continue;
    }

    memset(data, 0, sizeof(data));
    int pointer = 0;
    for (int i = 0; i < code.length(); ++i) {
      if (code[i] == '>') pointer = pointer==32767? 0 : pointer+1;
      else if (code[i] == '<') pointer = pointer==0? 32767 : pointer-1;
      else if (code[i] == '+') data[pointer] = data[pointer]==255? 0 : data[pointer]+1;
      else if (code[i] == '-') data[pointer] = data[pointer]==0? 255 : data[pointer]-1;
      else if (code[i] == '.') std::cout << char(data[pointer]);
      else if (code[i] == '[' && data[pointer] == 0) {
        i++;
        match = 1;
        while (match != 0) {
          if (code[i] == '[') match++;
          else if (code[i] == ']') match--;
          i++;
        }
        i--;
      } else if (code[i] == ']' && data[pointer] != 0) {
        i--;
        match = 1;
        while (match != 0) {
          if (code[i] == '[') match--;
          else if (code[i] == ']') match++;
          i--;
        }
        i++;
      }
    }
    std::cout << std::endl;
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值