-
Notifications
You must be signed in to change notification settings - Fork 0
/
turingmachine.cpp
110 lines (94 loc) · 3.37 KB
/
turingmachine.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*(C)Copyright 2023 Malone Napier-Jameson
*
* This file is part of Turing Machine Simulator.
* Turing Machine Simulator is free software: you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
* Turing Machine Simulator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Turing Machine Simulator.
* There is also a copy available inside the application.
* If not, see <https://www.gnu.org/licenses/>.
*/
#include "turingmachine.h"
#include <QStringList>
#include <QString>
#include <QDebug>
TuringMachine::TuringMachine(QStringList data): m_Data(data), m_NumOfStates(0)
{
}
TMState TuringMachine::getState(int stateNum) const
{
if(stateNum >= 0 && stateNum < m_Machine.length() && !m_Machine.isEmpty())
return m_Machine[stateNum];
return TMState();
}
QStringList TuringMachine::getSummaryTableData() const
{
return m_SummaryTableData;
}
int TuringMachine::getNumStates()
{
return m_NumOfStates;
}
void TuringMachine::addState(TMState theState)
{
m_Machine.append(theState);
}
void TuringMachine::build()
{
//Create each state:
for(int i = 0; i < m_Data.length(); i++)
{
QString state = m_Data[i];
QString ss = state[0];
QString hs = state[2];
//Variables to check if the state is a halt or start state:
bool isHALTState = false;
bool isSTARTState = false;
//Check if HALT state:
if(hs == "1")
isHALTState = true;
else if(hs == "0")
isHALTState = false;
//Check if START state:
if(ss == "1")
isSTARTState = true;
else if(ss == "0")
isSTARTState = false;
//Remove start/halt indicators:
state.remove(0,4);
if(!isHALTState)
{
QStringList edges = state.split('_', Qt::SkipEmptyParts);
QString stateNum = edges[0][1];
TMState tempState(stateNum.toInt(), isSTARTState, isHALTState);
for(int j = 0; j < edges.length(); j++)
{
//Append the edge data to a variable for the summary table:
m_SummaryTableData.append(edges[j]);
//Create an edge and append it t the state:
QString from = edges[j][1];
QString to = edges[j][4];
TMEdge tempEdge(from.toInt(),
to.toInt(),
edges[j][6],
edges[j][8],
edges[j][10]);
tempState.addEdge(tempEdge);
}
m_Machine.append(tempState);
}
else
{
QString stateNum = state[1];
TMState tempState(stateNum.toInt(), false, isHALTState);
m_Machine.append(tempState);
//Append halt state data to the summary table data:
QString data = "q" + stateNum + ",H ,A,L,T";
m_SummaryTableData.append(data);
}
m_NumOfStates++;
}
}