Poj Solution 2849

http://poj.org/problem?id=2849

//2849
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int Array[32768];
string Program;
string input;
int index;
int inputP;
int PromP;
int cc;
int Programsize;
inline void IncP()
{
    index = (index + 1) % 32768;
}

inline void DecP()
{
    index--;
    if (index == -1)
        index = 32768 - 1;
}

inline void Inc()
{
    Array[index] = (Array[index] + 1) % 256;
}

inline void Dec()
{
    Array[index]--;
    if (Array[index] == -1)
        Array[index] = 255;
}

inline void out()
{
    cout<<(char)Array[index];
}

inline void read()
{
    Array[index] = input[inputP++];
}

inline bool right()
{
    if(Array[index] != 0)
        return true;
    int head = 1;
    PromP++;
    while(1)
    {
        if(Program[PromP] == '[')
            head++;
        if(Program[PromP] == ']')
            head--;
        if(head == 0)
            break;
        PromP++;
        if(PromP >= Programsize)
            return false;
    }
    return true;
}

inline bool left()
{
    if(Array[index] == 0)
        return true;
    int head = 1;
    PromP--;
    while(1)
    {
        if(Program[PromP] == ']')
            head++;
        if(Program[PromP] == '[')
            head--;
        if(head == 0)
            break;
        PromP--;
        if(PromP < 0)
            return false;
    }
    return true;
}

void process()
{
    cout<<"PROGRAM #"<<cc+1<<":"<<endl;
    int len = Program.size();
    int count = 0;
    for(PromP = 0;PromP < len;PromP++)
    {
        if(Program[PromP] == '[')
            count++;
        if(Program[PromP] == ']')
            count--;
    }
    if(count != 0)
    {
        cout<<"COMPILE ERROR"<<endl;
        return;
    }
    for(PromP = 0;PromP < len;)
    {
        if(Program[PromP] == 'S')
        {
            string s;
            s.size();
        }
        bool bComE = true;
        switch(Program[PromP])
        {
        case '>':
            IncP();
            break;
        case '<':
            DecP();
            break;
        case '+':
            Inc();
            break;
        case '-':
            Dec();
            break;
        case '.':
            out();
            break;
        case '[':
            bComE = right();
            break;
        case ']':
            bComE = left();
            break;
        }
        PromP++;
        if(!bComE)
        {
            cout<<"COMPILE ERROR";
            break;
        }
    }
    cout<<endl;
}

int main()
{
    int N;
    while (cin>>N)
    {
        for (cc = 0;cc < N;cc++)
        {
            Program.clear();
            input.clear();
            memset(Array,0,sizeof(Array));
            index = 0;
            inputP = 0;
            PromP = 0;
            string Case,s;
            while (1)
            {
                int mid = 0;
                string::size_type index = 0;
                int count = 0;
                getline(cin,s,'n');
                index = s.find("%");
                if(index != string::npos)
                {
                    Case += s.substr(0,index);
                    continue;
                }
                index = s.find("end");
                if(index != string::npos)
                    break;
                Case += s;
            }
            Programsize = Case.size();
            Program = Case;
            process();
        }
    }
    return 0;
}

/*
brainf*ck
Time Limit: 1000MS  Memory Limit: 65536K 
Total Submissions: 1039  Accepted: 439 

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. 




COMMAND  OPERATION  
>  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
*/
											
This entry was posted in poj. Bookmark the permalink.