跳到正文

个人记录

Brainfuck

zhouhaoGG约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年3月15日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 6 分钟

版本

  1. v1当前
C++
#include<cstdio>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int brainfuck(const char* code){
	int ptr=0,codeptr=0,count=0;
	static char data[30000];
	for(;code[codeptr];codeptr++){
		switch(code[codeptr]){
			case '>':ptr++;ptr=min(ptr,29999);break;
			case '<':ptr--;ptr=max(ptr,0);break;
			case '+':data[ptr]++;break;
			case '-':data[ptr]--;break;
			case '.':putchar(data[ptr]);break;
			case ',':data[ptr]=getchar();break;
			case '[':
				if(!data[ptr]){
					count=1;
					while(count){
						codeptr++;
						if(code[codeptr]==']')count--;
						if(code[codeptr]=='[')count++;
						if(code[codeptr]==0)return 1;
					}
				}
				break;
			case ']':
				if(data[ptr]){
					count=1;
					while(count){
						codeptr--;
						if(code[codeptr]=='[')count--;
						if(code[codeptr]==']')count++;
						if(code[codeptr]==0)return 2;
					}
				}
				break;
		}
	}
	return 0;
}
int main(){
	char code[100000];
	scanf("%s",code);
	return brainfuck(code);
}

brainfuck_in_brainfuck.bf

bf
>>>>+[->>>++>+>+++++++[<++++>>++<-]++>>+>+>+++++[>++>++++++<<-]+>>>,<++[[>[->>]<[>>]<<-]<[<]<+>>[>]>[<+>-[[<+>-]>]<[[[-]<]++<-[<+++++++++>[<->-]>>]>>]]<<]>[-]+<<[--[[-]>>-<<<+>]>>[-<<<<[>+<-]+<<+[->[<+>>>>>>+<<<<<-]<[>+<-]>>>>>>>+<[-[-[-[-[-[-[-[-[[-]>-<]>[-<<+++++++>>]<]]]>[-]<]>[-<<+++>>]<]>[-<<+>>]<]>[-]<]>[-<<<<<<<+>>>>>>>]<]>[-]<<<<<]>>>[<<+>>-]<+<[-[-[-[-[-[-[-[-[-[-[[-]>-<<<[-]<<+>>]]]]>[-]<]>[-<<<[-]<<+++++++>>>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