Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- typedef int Cell;
- typedef int State;
- const int MAX_LENGTH = 25;
- typedef Cell Tape[MAX_LENGTH];
- typedef Cell Head;
- enum Actie {L,R,S};
- // Siemen Looijen, 4083679, K.I.
- //De verschillende arrays, globaal voor makkelijkere functie signaturen. Volgens werkcollege slides was dit acceptabel.
- State programma_lees_states[MAX_LENGTH];
- Cell programma_lees_cell_content[MAX_LENGTH];
- State programma_schrijf_states[MAX_LENGTH];
- Cell programma_schrijf_cell_content[MAX_LENGTH];
- Actie actie[MAX_LENGTH];
- 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
- void read_to_arrays(ifstream& inputFile, string& s, int& i)
- {
- s=""; // string weer leegmaken, is in ieder geval de eerste keer nodig
- getline(inputFile, s);
- programma_lees_states[i] = s[0] - '0';
- programma_lees_cell_content[i] = s[2]- '0';
- programma_schrijf_states[i]=s[7]- '0';
- programma_schrijf_cell_content[i] = s[9] - '0';
- if(s[11] == 'L')
- actie[i] = L;
- else if(s[11] == 'R')
- actie[i] = R;
- else if(s[11] == 'S')
- actie[i] = S;
- i++;
- }
- void display_program(int aantal_gebruikte_regels)
- {
- cout << "Ingelezen programma: " << endl << endl;
- for(int i=0; i< aantal_gebruikte_regels; i++)
- {
- cout << programma_lees_states[i] << " " << programma_lees_cell_content[i] << " -> " << programma_schrijf_states[i] << " "
- << programma_schrijf_cell_content[i] << " " << actie[i] << endl;
- }
- cout << endl << "Einde ingelezen programma! " << endl << endl;
- }
- void display_tape()
- {
- cout << "Huidige tape: " << endl;
- for(int i=0; i < MAX_LENGTH; i++)
- {
- cout << tape[i] << " ";
- }
- }
- void keyboard_naar_turing()
- {
- char c;
- int i=0 ,k=0, n=0,input=0; //k = states, n = cell content
- cout << "Wilt u de huidige tape veranderen? Y/N " << endl;
- cin >> c;
- if(c == 'Y' || c == 'y')
- {
- cout << "Hoeveel verschillende states zijn er? 0 voor slechts 1 state, 1 voor 2 states (0 en 1), etc. : " << endl;
- cin >> k;
- cout << "Hoeveel verschillende soorten cell content zijn er? 0 voor slechts 0 als cell content, 1 voor 2 types (0 en 1), etc. : " << endl;
- cin >> n;
- cout <<"Geef de nieuwe tape in. Gebruik aub enkel positieve integers en sluit af met een negatief getal: " << endl;
- while(cin && (i < MAX_LENGTH) && input >=0 && input <= n )
- {
- cin >> input;
- tape[i]=input;
- i++;
- }
- }
- else
- {
- cout << "U heeft geen Y of y opgegeven. Tape is niet veranderd: " << endl;
- display_tape();
- }
- }
- void turing_machine_read_file(ifstream& inputFile)
- {
- int k, n; // k = states, n = cell waardes
- string s=""; // gebruikt voor getline
- int aantal_gebruikte_regels=0;
- if(!inputFile)
- {
- cout << "Opening file failed!" << endl;
- }
- else
- {
- getline(inputFile,s);
- k= s[0] - '0';
- n= s[2] - '0';
- }
- int i=0;
- while(inputFile)
- {
- read_to_arrays(inputFile, s, i);
- aantal_gebruikte_regels++;
- }
- aantal_gebruikte_regels--; //omdat anders de EOF file gelezen wordt en ook meetelt
- display_program(aantal_gebruikte_regels);
- }
- int main()
- {
- ifstream inputFile("binary_increment.txt");
- turing_machine_read_file(inputFile);
- inputFile.close();
- // display_tape();
- return 0;
- }
Add Comment
Please, Sign In to add comment