Si
This commit is contained in:
83
brainfuck.c
Normal file
83
brainfuck.c
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SIZE 3000000
|
||||
|
||||
void run(char *input);
|
||||
void printList();
|
||||
|
||||
char *cinta;
|
||||
int main(){
|
||||
cinta = (char*)malloc(SIZE);
|
||||
char fileName[50];
|
||||
printf("Introduce el nombre del archivo: ");
|
||||
scanf("%s", fileName);
|
||||
char *input = (char*)malloc(sizeof(char));
|
||||
FILE *file;
|
||||
file = fopen(fileName, "r");
|
||||
int i=0;
|
||||
while(!feof(file)){
|
||||
input[i++] = fgetc(file);
|
||||
input = realloc(input, strlen(input) + 1);
|
||||
}
|
||||
fclose(file);
|
||||
run(input);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void run(char *input){
|
||||
int loopDepth = 0;
|
||||
for (int i=0; i<strlen(input)-1; i++){
|
||||
char c = input[i];
|
||||
switch(c){
|
||||
case '>':
|
||||
cinta++;
|
||||
break;
|
||||
case '<':
|
||||
cinta--;
|
||||
break;
|
||||
case '+':
|
||||
++*cinta;
|
||||
break;
|
||||
case '-':
|
||||
--*cinta;
|
||||
break;
|
||||
case '.':
|
||||
putchar(*cinta);
|
||||
break;
|
||||
case ',':
|
||||
getchar();
|
||||
*cinta = getchar();
|
||||
case '[':
|
||||
if (*cinta == 0){
|
||||
int depth = loopDepth;
|
||||
depth++;
|
||||
while(depth > loopDepth){
|
||||
i++;
|
||||
c = input[i];
|
||||
if (c == '[')
|
||||
depth++;
|
||||
else if(c == ']')
|
||||
depth--;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ']':
|
||||
if (*cinta){
|
||||
int depth = loopDepth;
|
||||
depth++;
|
||||
while(depth > loopDepth){
|
||||
i--;
|
||||
c = input[i];
|
||||
if (c == ']')
|
||||
depth++;
|
||||
else if(c == '[')
|
||||
depth--;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user