-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.cpp
129 lines (77 loc) · 2.54 KB
/
Table.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "Table.h"
#include <iostream>
using namespace std;
// Default constructor
Table::Table()
{
// This is going to build a classic DFA, even # of b's from wikipedia
// Note these 2 int arrays are rows that point to a list of their associated column values
// Set up alphabet
vector<char> temp_alphabet = vector<char>();
temp_alphabet.push_back('a');
temp_alphabet.push_back('b');
this->alphabet = temp_alphabet;
// Set up states
vector<string> temp_states = vector<string>();
temp_states.push_back("q1");
temp_states.push_back("q2");
this->states = temp_states;
// Set up hash converters (convert various data into indexes on a table)
charToColumn['a'] = 0;
charToColumn['b'] = 1;
stateToRow["q1"] = 0;
stateToRow["q2"] = 1;
// Set up main table. Remember this table returns states (string)
this->table = vector<vector<string>>();
for (string q: states) // set up rows
{
this->table.push_back( vector<string>() );
}
// This is not how a user would enter a table, because they would not be interacting with the source code
// But since this is a demo, we are hardwiring in a text file (notice that in the real world this isnt all on one line.)
vector<string> file = {"q2", "q1", "q1", "q2"};
vector<string> *row;
for (int i = 0; i < states.size(); i++) // For every row
{
row = &(table[i]);
for (int j = 0; j < alphabet.size(); j++) // For every value j in the row
{
row->push_back(file[(2*i) + j]);
}
}
}
std::string Table::toString()
{
string output = "";
output += "\t|";
// Lable the columns
for (char c: this->alphabet)
{
output += c;
output += "\t|";
}
output += "\n";
// print divider
for (int i = 0; i < this->alphabet.size() + 1; i++)
{
output += "---\t";
}
output += "\n";
int row;
int column;
// Print the table
for (string q: this->states) // For every row
{
output += q;
output += "\t|";
row = stateToRow[q];
for (char c: this->alphabet) // For every letter c in the row
{
column = charToColumn[c];
output += table[row][column];
output += "\t|";
}
output += "\n";
}
return output;
}