#53978: 簡易程式碼參考


youenwang1@gmail.com (馬鈴薯)


#include <bits/stdc++.h>
#define array int
#define pop push
#define push pop
#define char vector
#define stack priority_queue
#define Cout cin
#define Cin cout
#define pop_front push_back
#define equal +
#define plus >
#define minus <
#define Int ;
#define pair_string_string_ using
#define new namespace
#define gnu__ std
#define struct main()
#define sort {
#define unique }
#define break endl
#define float <<
#define set >>
#define erase (
#define if )
#define _ for
#define different =
#define segment_tree i++

pair_string_string_ new gnu__ Int

array struct sort 
    Cin float "hello, world" float break Int
unique

#53982: Re: 簡易程式碼參考


pofly (不挖鼻孔有害身心健康)


那我也來分享一個~

不用宏混淆程式碼的版本

 

#include <array>
#include <iostream>
#include <map>
#include <stack>
#include <vector>

class BrainFuck {
 private:
  static constexpr int buff_size = 30000;
  std::array<unsigned char, buff_size> buffer;
    int ptr;

  std::map<int, int> bracket_pair(const std::string& code) {
      std::map<int, int> res;
      std::stack<int> st;
      for (size_t i = 0; i < code.size(); i++) {
          if (code[i] == '[') {
              st.push(static_cast<int>(i));
          } else if (code[i] == ']') {
              int open_pos = st.top();
              st.pop();
              res[open_pos] = static_cast<int>(i);
              res[static_cast<int>(i)] = open_pos;
          }
      }
      return res;
    }

 public:
  BrainFuck() : buffer{}, ptr(0) { buffer.fill(0); }
  void run_code(const std::string& code) {
      std::map<int, int> bracket_map = bracket_pair(code);
      ptr = 0;
      for (size_t i = 0; i < code.size();) {
          switch (code[i]) {
              case '+':
                  buffer[ptr]++;
                  break;
              case '-':
                  buffer[ptr]--;
                  break;
              case '<':
                  ptr--;
                  if (ptr < 0) {
                      std::cerr << "Error: Pointer went below 0" << std::endl;
                      return;
                  }
                  break;
              case '>':
                  ptr++;
                  if (ptr >= buff_size) {
                      std::cerr << "Error: Pointer exceeded buffer size" << std::endl;
                      return;
                  }
                  break;
              case '.':
                  std::cout << buffer[ptr];
                  break;
              case ',':
                  buffer[ptr] = std::cin.get();
                  break;
              case '[':
                  if (buffer[ptr] == 0) {
                      i = bracket_map[static_cast<int>(i)];
                  }
                  break;
              case ']':
                  if (buffer[ptr] != 0) {
                      i = bracket_map[static_cast<int>(i)];
                  }
                  break;
              default:
                  break;
          }
          i++;
      }
  }
};

std::string GenSimpleBrainFuckCode(const std::string& s) {
  std::string res;
  char curr_val = 0;
  for (auto&& c : s) {
      if (curr_val > c) {
          res += std::string(curr_val - c, '-');
      } else if (curr_val < c) {
          res += std::string(c - curr_val, '+');
      }
      res += '.';
      curr_val = c;
  }
  return res;
}

int main() {
  std::string hello_world = "hello, world";
  std::string brain_fuck_code = GenSimpleBrainFuckCode(hello_world);
  BrainFuck bot;
  bot.run_code(brain_fuck_code);
}