Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*I know this code ain't good, but the main thing is idea for you to do it better.*/
- void run_99_bottles_of_beer()
- {
- char i = 99;
- while (i >= 3)
- {
- printf("%i bottles of beer on the wall, %i bottles of beer!\n", i, i);
- printf("Take one down, pass it around, %i bottles of beer on the wall.\n\n", --i);
- }
- printf("2 bottles of beer on the wall, 2 bottles of beer.\n");
- printf("Take one down, pass it around, 1 bottle of beer on the wall.\n\n");
- printf("1 bottle of beer on the wall, 1 bottle of beer.\n");
- printf("Take one down, pass it around, no more bottles of beer on the wall.\n\n");
- printf("No more bottles of beer on the wall, no more bottles of beer.\n");
- printf("Go to the store and buy some more, 99 bottles of beer on the wall.\n\n");
- }
- int main(int argc, char **argv)
- {
- FILE* source_file;
- long size;
- if (argc <= 1)
- {
- puts("No source.");
- return 0;
- }
- source_file = fopen(argv[1], "r");
- if (source_file == NULL)
- {
- puts("Can't open file.");
- return 0;
- }
- fseek(source_file, 0, SEEK_END);
- size = ftell(source_file);
- fseek(source_file, 0, SEEK_SET);
- char* tape = calloc(60000, 1);
- char* source = malloc(size);
- long i = 0;
- while (size-- > 0)
- {
- char ch = getc(source_file);
- source[i] = ch;
- ++i;
- }
- int tape_pointer = 0;
- int src_pointer = 0;
- int src_length = strlen(source);
- int loop = 0;
- while (src_pointer < src_length)
- {
- char src_op = source[src_pointer];
- /*Brainfuck extension*/
- if (src_op == '<') tape_pointer--; else
- if (src_op == '>') tape_pointer++; else
- if (src_op == '+') tape[tape_pointer]++; else
- if (src_op == '-') tape[tape_pointer]--; else
- if (src_op == ',') tape[tape_pointer] = getchar(); else
- if (src_op == '.') putchar(tape[tape_pointer]); else
- if (src_op == ']')
- {
- if (tape[tape_pointer] != 0)
- {
- loop = 1;
- while (loop != 0)
- {
- --src_pointer;
- if (source[src_pointer] == '[') loop--; else
- if (source[src_pointer] == ']') loop++;
- }
- }
- } else
- if (src_op == '[')
- {
- if (tape[tape_pointer] == 0)
- {
- loop = 1;
- while (loop != 0)
- {
- ++src_pointer;
- if (source[src_pointer] == ']') loop--; else
- if (source[src_pointer] == '[') loop++;
- }
- }
- }
- /*HQ9+ extension (command '+' taken by brainfuck)*/
- if (src_op == 'H') puts("Hello world!"); else
- if (src_op == 'Q') puts(source); else
- if (src_op == '9') run_99_bottles_of_beer();
- /*Deep Thought extension, you write 'DT' in code, it prints '42'*/
- if (src_op == 'D' && source[src_pointer + 1] == 'T')
- {
- ++src_pointer;
- printf("%i\n", 42);
- }
- src_pointer++;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment