Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Koen Buitenhuis s4069471
- //Bas de Leeuw s4064267
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- using namespace std ;
- void reading (char Tprog [], int& k, int& n, int& ch)
- {
- int kn = 0 ;
- bool ok ;
- char character ;
- ifstream input_file ( "TProgram.txt" ) ;
- while (ok)
- {
- if (input_file)
- {
- input_file.get (character) ;
- if (character < 58 and character > 47)
- {
- if (kn == 0)
- {
- k = character - 48 ;
- kn = 1 ;
- }
- else
- if (kn == 1)
- {
- n = character - 48 ;
- kn = 2 ;
- input_file.get (character) ;
- }
- if (kn != 0 and kn != 1 and kn != 2)
- cout << "Fatal Error" ;
- }
- if (kn == 2)
- {
- Tprog[ch] = character ;
- ch++ ;
- }
- }
- else ok = false ;
- }
- cout << "k=" << k << " n=" << n << "\n" ;
- for (int i = 0 ; i < ch ; i++ )
- cout << Tprog [i] ;
- }
- void rulesarray (char Tprog [], char rules [], int ch)
- {
- int h = 0;
- for (int i = 0 ; i < ch ; i++ )
- {
- if ( (Tprog [i] < 58 and Tprog [i] > 47 ) or Tprog [i] == 'L' or Tprog [i] == 'S' or Tprog [i] == 'R' )
- {
- rules [h] = Tprog [i] ;
- h++ ;
- }
- }
- cout << rules ;
- }
- void input (int prog [999], int n, int& count, int proglengte)
- {
- cout << "Geef een aantal symbolen tussen de 0 en " << n << ": " ;
- for (int i = 0 ; i < proglengte ; i++ )
- {
- cin >> prog [i] ;
- if ( !(prog[i] >= 0 and prog[i] <= n ))
- {
- cout << "Deze waarde is niet geldig, vul hem opnieuw in:" ;
- i-- ;
- }
- }
- count = proglengte - 1 ;
- cout << "Uw input was: \n" << prog ;
- for (int a = 0 ; a < proglengte ; a++ )
- cout << prog [a] ;
- cout << "\n" ;
- }
- void execute (char rules [999], int count, int prog [999], int k )
- {
- int s = 0, h = 0, m = count, p = 0 ;
- bool change = false ;
- while (!(change and s == 0))
- {
- if (s == (rules [h] - 48) and prog [count] == (rules [h + 1] - 48) )
- {
- s = rules [h + 2] - 48 ;
- prog [count] = rules [h + 3] - 48 ;
- if (rules [h + 5] == 'S')
- count = count ;
- if (rules [h + 5] == 'L')
- count = count - 1 ;
- if (rules [h + 5] == 'R')
- count = count + 1 ;
- if (count > m or count < 0)
- cout << "Program crashed" ;
- }
- else h = h + 5 ;
- p = p + 1 ;
- if (s != 0)
- change = true ;
- }
- }
- void output (int prog [], int proglengte)
- {
- for (int i = 0 ; i < proglengte ; i++ )
- cout << prog [i] ;
- }
- int main ()
- {
- int k = 0, n = 0, proglengte, count = 0, ch = 0;
- char Tprog [999] ;
- reading (Tprog, k, n, ch) ;
- char rules [ch] ;
- rulesarray (Tprog, rules, ch) ;
- cout << k << " " << n ;
- cout << "\nGeef het aantal symbolen van de input: " ;
- cin >> proglengte ;
- int prog [proglengte] ;
- input (prog, n, count, proglengte) ;
- execute (rules, count, prog, k) ;
- output (prog, proglengte) ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement