在codewars里升级吧~4:一道5级题代码及讲解视频

继续预告大项目,鸽这么久也不是本意。主要是项目越写越大,功能越来越多有点停不下来。。。

希望能做出10h以上的视频。

今天讲一道 codewars 5级题。

腾讯讲解视频链接

https://v.qq.com/x/page/w0724xdcnih.html

b站讲解视频链接

https://www.bilibili.com/video/av26980703

题目

Inspired from real-world Brainf**k, we want to create an interpreter of that language which will support the following instructions ( the machine memory or 'data' should behave like a potentially infinite array of bytes, initialized  to 0 ):   

  • > increment the data pointer (to point to the next cell to the right).

  • < decrement the data pointer (to point to the next cell to the left).

  • + increment (increase by one, truncate overflow: 255 + 1 = 0) the byte at the data pointer.

  • - decrement (decrease by one, treat as unsigned byte: 0 - 1 = 255 ) the byte at the data pointer.

  • . output the byte at the data pointer.

  • , accept one byte of input, storing its value in the byte at the data pointer.

  • [ if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.

  • ] if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command,  jump it back to the command after the matching [ command.

The function will take in input...

  • the program code, a string with the sequence of machine instructions, the program input, a string, eventually empty, that  will be interpreted as an array of bytes using each character's ASCII code and will be consumed by the , instruction

... and will return ...

  • the output of the interpreted code (always as a string), produced by the  .

    instruction.

SOLUTION

 #include <string>
#include <vector>
#include <deque>

std::string brainLuck(std::string code, std::string input)
{
    std::deque<unsigned char> memory;
    memory.push_back(0);
    int DataPointer = 0;
    int inputpointer = 0;
    std::string out = "";
    for (int CodePointer = 0; CodePointer < code.size(); CodePointer++) {
        switch (code[CodePointer])
        {
        case '>':
            DataPointer++;
            if (DataPointer == memory.size())memory.push_back(0);
            break;
        case '<':
            DataPointer--;
            if (DataPointer == -1) {
                memory.push_front(0);
                DataPointer = 0;
            }
            break;
        case '+':
            memory[DataPointer]++;
            break;
        case '-':
            memory[DataPointer]--;
            break;
        case '.':
    out.push_back(memory[DataPointer]);
            break;
        case ',':
            memory[DataPointer] = input[inputpointer++];
            break;
        case '[': {
            if (!memory[DataPointer]) {
                int leftStack = 0;
                CodePointer++;
                for (; CodePointer < code.size(); CodePointer++) {
                    if (code[CodePointer] == '[')leftStack++;
                    else if (code[CodePointer] == ']')leftStack--;
                    if (leftStack == -1)break;
                }
            }

        }break;
        case ']': {
            if (memory[DataPointer]) {
                int RightStack = 0;
                CodePointer--;
                for (; CodePointer > -1; CodePointer--) {
                    if (code[CodePointer] == ']')RightStack++;
                    else if (code[CodePointer] == '[')RightStack--;
                    if (RightStack == -1)break;
                }
           }

        }break;
        default:
            break;
        }
    }
    return out;
}

交流qq:1980185974

微信:Huo-coding

猜你喜欢

转载自blog.csdn.net/Huocoding/article/details/81068262
今日推荐