Guest User

Untitled

a guest
Jan 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. typedef int Cell;
  8. typedef int State;
  9. const int MAX_LENGTH = 25;
  10. typedef Cell Tape[MAX_LENGTH];
  11. typedef Cell Head;
  12. enum Actie {L,R,S};
  13.  
  14. // Siemen Looijen, 4083679, K.I.
  15.  
  16. //De verschillende arrays, globaal voor makkelijkere functie signaturen. Volgens werkcollege slides was dit acceptabel.
  17. State programma_lees_states[MAX_LENGTH];
  18. Cell programma_lees_cell_content[MAX_LENGTH];
  19. State programma_schrijf_states[MAX_LENGTH];
  20. Cell programma_schrijf_cell_content[MAX_LENGTH];
  21. Actie actie[MAX_LENGTH];
  22.  
  23. Tape tape = {0,1,0,1,0,1,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,1,1,1}; //cel contents staan hierin
  24.  
  25.  
  26.  
  27. void read_to_arrays(ifstream& inputFile, string& s, int& i)
  28. {
  29.     s=""; // string weer leegmaken, is in ieder geval de eerste keer nodig
  30.     getline(inputFile, s);
  31.     programma_lees_states[i] = s[0] - '0';
  32.     programma_lees_cell_content[i] = s[2]- '0';
  33.     programma_schrijf_states[i]=s[7]- '0';
  34.     programma_schrijf_cell_content[i] = s[9] - '0';
  35.  
  36.     if(s[11] == 'L')
  37.         actie[i] =  L;
  38.     else if(s[11] == 'R')
  39.         actie[i] = R;
  40.     else if(s[11] == 'S')
  41.         actie[i] = S;
  42.  
  43.     i++;
  44.  
  45. }
  46.  
  47. void display_program(int aantal_gebruikte_regels)
  48. {
  49.     cout << "Ingelezen programma: " << endl << endl;
  50.     for(int i=0; i< aantal_gebruikte_regels; i++)
  51.     {
  52.         cout << programma_lees_states[i] << " " << programma_lees_cell_content[i] << " -> " << programma_schrijf_states[i] << " "
  53.         << programma_schrijf_cell_content[i]  << " " << actie[i] << endl;
  54.     }
  55.      cout << endl << "Einde ingelezen programma! " << endl << endl;
  56. }
  57.  
  58. void display_tape()
  59. {
  60.     cout << "Huidige tape: " << endl;
  61.     for(int i=0; i < MAX_LENGTH; i++)
  62.     {
  63.         cout << tape[i] << " ";
  64.     }
  65.  
  66. }
  67.  
  68.  
  69. void keyboard_naar_turing()
  70. {
  71.     char c;
  72.     int i=0 ,k=0, n=0,input=0; //k  = states, n = cell content
  73.  
  74.     cout << "Wilt u de huidige tape veranderen? Y/N " << endl;
  75.     cin >> c;
  76.  
  77.     if(c == 'Y' || c == 'y')
  78.     {
  79.         cout << "Hoeveel verschillende states zijn er? 0 voor slechts 1 state, 1 voor 2 states (0 en 1), etc. : " << endl;
  80.         cin >> k;
  81.         cout << "Hoeveel verschillende soorten cell content zijn er? 0 voor slechts 0 als cell content, 1 voor 2 types (0 en 1), etc. : " << endl;
  82.         cin >> n;
  83.         cout <<"Geef de nieuwe tape in. Gebruik aub enkel positieve integers en sluit af met een negatief getal: " << endl;
  84.  
  85.          while(cin && (i < MAX_LENGTH) && input >=0 && input <= n )
  86.          {
  87.              cin >> input;
  88.              tape[i]=input;
  89.              i++;
  90.          }
  91.     }
  92.     else
  93.     {
  94.         cout << "U heeft geen Y of y opgegeven. Tape is niet veranderd: " << endl;
  95.         display_tape();
  96.     }
  97.  
  98.  
  99.  
  100. }
  101.  
  102.  
  103. void turing_machine_read_file(ifstream& inputFile)
  104. {
  105.     int k, n; // k = states, n = cell waardes
  106.     string s=""; // gebruikt voor getline
  107.     int aantal_gebruikte_regels=0;
  108.  
  109.     if(!inputFile)
  110.     {
  111.         cout << "Opening file failed!" << endl;
  112.     }
  113.     else
  114.     {
  115.         getline(inputFile,s);
  116.         k= s[0] - '0';
  117.         n= s[2] - '0';
  118.     }
  119.  
  120.     int i=0;
  121.  
  122.     while(inputFile)
  123.     {
  124.         read_to_arrays(inputFile, s, i);
  125.         aantal_gebruikte_regels++;
  126.     }
  127.     aantal_gebruikte_regels--; //omdat anders de EOF file gelezen wordt en ook meetelt
  128.  
  129.     display_program(aantal_gebruikte_regels);
  130. }
  131.  
  132. int main()
  133. {
  134.     ifstream inputFile("binary_increment.txt");
  135.     turing_machine_read_file(inputFile);
  136.     inputFile.close();
  137.   //  display_tape();
  138.     return 0;
  139. }
Add Comment
Please, Sign In to add comment