Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. //Koen Buitenhuis   s4069471
  2. //Bas de Leeuw      s4064267
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstdlib>
  6. using namespace std ;
  7.  
  8.  
  9. void reading (char Tprog [], int& k, int& n, int& ch)
  10. {
  11.     int kn = 0 ;
  12.     bool ok ;
  13.     char character ;
  14.     ifstream input_file ( "TProgram.txt" ) ;
  15.     while (ok)
  16.     {
  17.         if (input_file)
  18.         {
  19.             input_file.get (character) ;
  20.             if (character < 58 and character > 47)
  21.             {
  22.                 if (kn == 0)
  23.                 {
  24.                     k = character - 48 ;
  25.                     kn = 1 ;
  26.                 }
  27.                 else
  28.                     if (kn == 1)
  29.                     {
  30.                         n = character - 48 ;
  31.                         kn = 2 ;
  32.                         input_file.get (character) ;
  33.                     }
  34.                 if (kn != 0 and kn != 1 and kn != 2)
  35.                     cout << "Fatal Error" ;
  36.             }
  37.             if (kn == 2)
  38.             {
  39.                 Tprog[ch] = character ;
  40.                 ch++ ;
  41.             }
  42.         }
  43.         else ok = false ;
  44.     }
  45.     cout << "k=" << k << " n=" << n << "\n" ;
  46.     for (int i = 0 ; i < ch ; i++ )
  47.         cout << Tprog [i] ;
  48. }
  49.  
  50. void rulesarray (char Tprog [], char rules [], int ch)
  51. {
  52.     int h = 0;
  53.     for (int i = 0 ; i < ch ; i++ )
  54.         {
  55.             if ( (Tprog [i] < 58 and Tprog [i] > 47 ) or Tprog [i] == 'L' or Tprog [i] == 'S' or Tprog [i] == 'R' )
  56.                 {
  57.                     rules [h] = Tprog [i] ;
  58.                     h++ ;
  59.                 }
  60.         }
  61.     cout << rules ;
  62. }
  63.  
  64.  
  65. void input (int prog [999], int n, int& count, int proglengte)
  66. {
  67.     cout << "Geef een aantal symbolen tussen de 0 en " << n << ": " ;
  68.     for (int i = 0 ; i < proglengte ; i++ )
  69.         {
  70.             cin >> prog [i] ;
  71.             if ( !(prog[i] >= 0 and prog[i] <= n ))
  72.                 {
  73.                     cout << "Deze waarde is niet geldig, vul hem opnieuw in:" ;
  74.                     i-- ;
  75.                 }
  76.         }
  77.     count = proglengte - 1 ;
  78.     cout << "Uw input was: \n" << prog ;
  79.     for (int a = 0 ; a < proglengte ; a++ )
  80.         cout << prog [a] ;
  81.     cout << "\n" ;
  82. }
  83.  
  84. void execute (char rules [999], int count, int prog [999], int k )
  85. {
  86.     int s = 0, h = 0, m = count, p = 0 ;
  87.     bool change = false ;
  88.     while (!(change and s == 0))
  89.     {
  90.         if (s == (rules [h] - 48) and prog [count] == (rules [h + 1] - 48) )
  91.         {
  92.             s = rules [h + 2] - 48 ;
  93.             prog [count] = rules [h + 3] - 48 ;
  94.             if (rules [h + 5] == 'S')
  95.                 count = count ;
  96.             if (rules [h + 5] == 'L')
  97.                 count = count - 1 ;
  98.             if (rules [h + 5] == 'R')
  99.                 count = count + 1 ;
  100.             if (count > m or count < 0)
  101.                 cout << "Program crashed" ;
  102.         }
  103.         else h = h + 5 ;
  104.         p = p + 1 ;
  105.         if (s != 0)
  106.         change = true ;
  107.     }
  108. }
  109.  
  110. void output (int prog [], int proglengte)
  111. {
  112.     for (int i = 0 ; i < proglengte ; i++ )
  113.         cout << prog [i] ;
  114. }
  115.  
  116. int main ()
  117. {
  118.     int k = 0, n = 0, proglengte, count = 0, ch = 0;
  119.     char Tprog [999] ;
  120.     reading (Tprog, k, n, ch) ;
  121.     char rules [ch] ;
  122.     rulesarray (Tprog, rules, ch) ;
  123.     cout << k << " " << n ;
  124.     cout << "\nGeef het aantal symbolen van de input: " ;
  125.     cin >> proglengte ;
  126.     int prog [proglengte] ;
  127.     input (prog, n, count, proglengte) ;
  128.     execute (rules, count, prog, k) ;
  129.     output (prog, proglengte) ;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement