可以在Linux和Windows上运行的Brainfuck模拟器
  • 板块灌水区
  • 楼主Alea
  • 当前回复3
  • 已保存回复3
  • 发布时间2022/4/16 23:56
  • 上次更新2023/10/28 03:32:09
查看原帖
可以在Linux和Windows上运行的Brainfuck模拟器
322792
Alea楼主2022/4/16 23:56

代码见下

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int choice;
char prog[2000]={};
unsigned memory[300]={};
void editProg();
void compilrunProg();
void information();
int main(){
    //Main page
    while(1){
        printf("What do you want to do?\n");
        printf("1.Edit program\n2.Run program\n3.Information\n4.Quit\n");
        printf("Please answer me in number:");
        scanf("%d",&choice);
        printf("\n");
        switch(choice){
            case 1:
                editProg();
                break;
            case 2:
                runProg();
                break;
            case 3:
                information();
                break;
            case 4:
                return 0;
                break;
            default:
                printf("You can\'t choise that!\n");
        }
        printf("\n");
    }
    return 0;
}
void editProg(){
    printf("Please enter your program here(space or enter is the singal of end):");
    scanf("%s",prog);
}
void runProg(){
    int stack[1000]={},top=0,go[2000]={};
    for(int i=0;i<strlen(prog);i++){
        if(prog[i]=='[') stack[++top]=i;
        else if(prog[i]==']'){
            go[stack[top]]=i;
            go[i]=stack[top];
            top--;
        }
        if(prog[i]!='<' && prog[i]!='>' && prog[i]!='+' && prog[i]!='-' && prog[i]!='.' && prog[i]!=',' && prog[i]!='[' && prog[i]!=']'){
            printf("Illegal symbol.\n");
            return;
        }
    }
    if(top!=0){
       printf("Pair Error.\n");
       return;
    }
    printf("This is the run area of your program.\n\n");
    int mempos=150;
    for(int i=0;i<strlen(prog);i++){
        if(prog[i]=='+') memory[mempos]++;
        else if(prog[i]=='-') memory[mempos]--;
        else if(prog[i]=='>') mempos++;
        else if(prog[i]=='<') mempos--;
        else if(prog[i]=='.') printf("%c",memory[mempos]);
        else if(prog[i]==',') scanf("%c",&memory[mempos]);
        else if(prog[i]=='['){
            if(memory[mempos]==0){
                i=go[i];
            }
        }else if(prog[i]==']'){
            if(memory[mempos]!=0){
                i=go[i];
            }
        }
    }
    printf("\n");
}
void information(){
    printf("Starmen\'s Brainfxxk analysis program.Version 0.1\n");
    return;
}
2022/4/16 23:56
加载中...