Advertisement
Guest User

Untitled

a guest
Jun 9th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. #define CELLS_NUM 30000
  6.  
  7.  
  8. int main()
  9. {
  10.     unsigned char arr[CELLS_NUM] = {};
  11.     unsigned int current_cell = 0;
  12.  
  13.     using namespace std;
  14.     string filename;
  15.     cout << "Source:";
  16.     cin >> filename;
  17.     fstream source_file(filename.c_str(), ios_base::in);
  18.     vector<unsigned char>source;
  19.    
  20.     while (!source_file.eof())
  21.     {
  22.         char ch = 0;
  23.         source_file.get(ch);
  24.         source.push_back(ch);
  25.     }
  26.  
  27.     source_file.close();
  28.  
  29.     for (unsigned i = 0; i < static_cast<unsigned>(source.size()); ++i)
  30.     {
  31.         unsigned char ch = source[i];
  32.         switch (ch)
  33.         {
  34.         case '>':
  35.             current_cell++;
  36.             break;
  37.         case '<':
  38.             current_cell--;
  39.             break;
  40.         case '+':
  41.             arr[current_cell]++;
  42.             break;
  43.         case '-':
  44.             arr[current_cell]--;
  45.             break;
  46.         case '.':
  47.             cout << arr[current_cell];
  48.             break;
  49.         case ',':
  50.             cin >> ch;
  51.             arr[current_cell] = ch;
  52.             break;
  53.         case '[':
  54.             if (arr[current_cell] == 0)
  55.             {
  56.                 unsigned j = 1;
  57.                 while (j != 0)
  58.                 {
  59.                     i++;
  60.                     switch (source[i])
  61.                     {
  62.                     case '[':
  63.                         j++;
  64.                         break;
  65.                     case ']':
  66.                         j--;
  67.                         break;
  68.                     default:
  69.                         break;
  70.                     }
  71.                 }
  72.             }
  73.             break;
  74.         case ']':
  75.             if (arr[current_cell] != 0)
  76.             {
  77.                 unsigned j = 1;
  78.                 while (j != 0)
  79.                 {
  80.                     i--;
  81.                     switch (source[i])
  82.                     {
  83.                     case '[':
  84.                         j--;
  85.                         break;
  86.                     case ']':
  87.                         j++;
  88.                         break;
  89.                     default:
  90.                         break;
  91.                     }
  92.                 }
  93.             }
  94.             break;
  95.         default:
  96.             break;
  97.         }
  98.     }
  99.     system("PAUSE");
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement