-
Notifications
You must be signed in to change notification settings - Fork 0
/
TurningMachine.cpp
72 lines (68 loc) · 1.75 KB
/
TurningMachine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "TurningMachine.h"
#include <iostream>
using namespace std;
TurningMachine::TurningMachine(list<string> q, list<char> sigma, list<TurningMachineTransition> delta, string q0, list<string> finalstate)
{
Q0 = q0;
Q = list<string>(q);
Sigma = list<char>(sigma);
Delta = list<TurningMachineTransition>(delta);
FinalState = list<string>(finalstate);
tape = LinkedList();
}
TurningMachine::~TurningMachine()
{
Q0.clear();
Q.clear();
Sigma.clear();
Delta.clear();
FinalState.clear();
}
void TurningMachine::Accepts(string input)
{
cout << "input: " << input << endl;
input = u + input + u;
// load the data to the tape
for (int i = 0; i < input.length(); i++)
{
tape.appendNode(input.at(i));
}
string currentState = Q0; // setting at the begging the start state
string steps = ""; // contain all the steps taken so far
if (tape.CurretLocation == NULL)
{
tape.getHead();
}
// get the letter
tape.move('R');
// char letter = tape.CurretLocation->value;
// search all transitions for a one that have the same
bool found = false;
do
{
found = false;
for (list<TurningMachineTransition>::iterator it = Delta.begin(); it != Delta.end(); it++)
{
if (it->getStartState() == currentState && it->getToken() == tape.CurretLocation->value)
{
tape.CurretLocation->value = it->getWrite();
tape.move(it->getMoveDirection());
currentState = it->getEndState();
steps += it->toString();
found = true;
break;
}
}
if (!found)
{
if (steps.length() <= 0)
{
steps += "this is was the first transation and no transation were found";
}
cout << "FAILED" << endl;
break;
}
} while (true);
tape.displayList();
cout << steps << endl;
}