December SPECIAL! For a limited time only. Get 20% discount on a LIFETIME PRO account!Want more features on Pastebin? Sign Up, it's FREE!
tweet
Guest

Untitled

By: a guest on Sep 19th, 2015  |  syntax: C  |  size: 1.76 KB  |  views: 61  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print  |  QR code  |  clone
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.         char code[10000];
  7.         if(argc == 1) {
  8.                 strcpy(code, "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."); /*write your code here if it's too much of a pain in the CLI*/
  9.         }
  10.         else if(argc != 2) {
  11.                 printf("%s: Write your code all at once, without spaces.\n", argv[1]);
  12.                 return 1;
  13.         }
  14.         else {
  15.                 strcpy(code, argv[1]);
  16.         }
  17.  
  18.         int ptr = 0;
  19.         int array[30000];
  20.  
  21.         int i = 0;
  22.         int jumps = 0;
  23.         while(code[i] != '\0') {
  24.                 char letter = code[i];
  25.                 switch(letter) {
  26.                         case '>':
  27.                                 ptr++;
  28.                                 break;
  29.                         case '<':
  30.                                 ptr--;
  31.                                 break;
  32.                         case '+':
  33.                                 array[ptr]++;
  34.                                 break;
  35.                         case '-':
  36.                                 array[ptr]--;
  37.                                 break;
  38.                         case '[':
  39.                                 if(array[ptr] == 0) {
  40.                                         jumps = 0;
  41.                                         for(int x = i+1; code[x] != '\0'; x++) {
  42.        
  43.                                                 if(code[x] == '[') {
  44.                                                         jumps++;
  45.                                                 }
  46.  
  47.                                                 if(code[x] == ']') {
  48.  
  49.                                                         if(jumps == 0) {
  50.                                                                 i = x;
  51.                                                                 break;
  52.                                                         }
  53.  
  54.                                                         else {
  55.                                                                 jumps--;
  56.                                                         }
  57.                                                 }
  58.                                         }
  59.                                 }
  60.                                 break;
  61.                         case ']':
  62.                                 if(array[ptr] != 0) {
  63.                                         jumps = 0;
  64.                                         /*printf("in");*/
  65.                                         for(int x = i-1; code[x] != '\0'; x--) {
  66.                                                 /*printf("%c",argv[1][x]);*/
  67.                                                 if(code[x] == ']') {
  68.                                                         jumps++;
  69.                                                 }
  70.  
  71.                                                 if(code[x] == '[') {
  72.                                                         if(jumps == 0) {
  73.                                                                 i = x;
  74.                                                                 /*printf("%d, %d",i,x);*/
  75.                                                                 break;
  76.                                                         }
  77.  
  78.                                                         else {
  79.                                                                 jumps--;
  80.                                                         }
  81.                                                 }
  82.                                         }
  83.                                         /*printf("out");*/
  84.                                 }
  85.                                 break;
  86.                         case '.':
  87.                                 printf("%d",array[ptr]);
  88.                                 break;
  89.                         case ',':
  90.                                 printf("NOT ACCEPTING INPUT FOR NOW. F U.\n");
  91.                                 return 1;
  92.                                 break;
  93.                         default:
  94.                                 printf("Invalid code.\n");
  95.                                 return 1;
  96.                 }
  97.                 i++;
  98.         }
  99. }
clone this paste RAW Paste Data
Top