-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdfa.cpp
More file actions
135 lines (114 loc) · 3.23 KB
/
dfa.cpp
File metadata and controls
135 lines (114 loc) · 3.23 KB
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
130
131
132
133
134
135
/*
@author: Kshitiz Srivastava (pirateksh)
*/
#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define si set<char>
#define pb(v, x) v.push_back(x)
vector<char> set_of_inputs;
int initial_state = 0;
int number_of_states;
vi set_of_final_states;
vi transition_function[100000];
map<char, int> input_to_index;
void enter_set_of_inputs() {
int n;
char input;
cout << "Enter total number of inputs: ";
cin >> n;
cout << "Enter inputs separated by a space: ";
for(int i = 0; i < n; i++) {
cin >> input;
pb(set_of_inputs, input);
input_to_index[input] = i+1;
}
cout << "Set of inputs have been successfully recorded.\n";
}
void enter_total_number_of_states() {
cout << "Enter total number of states: ";
cin >> number_of_states;
cout << "Set of states have been successfully recorded.\n";
}
void enter_set_of_final_states() {
int n, final_state;
cout << "Enter total number of final states: ";
cin >> n;
cout << "Enter final states separated by a space: ";
for(int i = 0; i < n; i++) {
cin >> final_state;
pb(set_of_final_states, final_state);
}
cout << "Set of final states have been successfully recorded.\n";
}
void enter_initial_state() {
cout << "Initial State is assumed to be 0 by default.\n";
}
void define_transition_function() {
int number_of_inputs = set_of_inputs.size(), next_state;
for(int i = 0; i < number_of_states; i++) {
for(int j = 0; j < number_of_inputs; j++) {
cout << "Current state: " << i << " Input: " << set_of_inputs[j] << " Enter next state: ";
cin >> next_state;
pb(transition_function[i], next_state);
}
}
}
void configure_machine() {
enter_set_of_inputs();
enter_total_number_of_states();
enter_set_of_final_states();
enter_initial_state();
define_transition_function();
cout << "Machine has been successfully configured according to your input(s).\nYou can use this machine now\n";
}
void init() {
cout << "----------------------------------\n";
cout << " ### Welcome to DFA Simulator ###\n";
cout << "----------------------------------\n";
}
void check(string s) {
int current_state = initial_state, i;
for(i = 0; i < s.length(); i++) {
if(input_to_index[s[i]] == 0) break;
current_state = transition_function[current_state][input_to_index[s[i]] - 1];
}
if(i!=s.length()) {
cout << s << " is Invalid Input\n";
return;
}
for(i = 0; i < set_of_final_states.size(); i++) {
if(current_state == set_of_final_states[i]) break;
}
if(i == set_of_final_states.size()) cout << s << " is Rejected\n";
else cout << s << " is Accepted\n";
}
void test() {
fstream file;
string s, filename;
filename = "input.txt";
file.open(filename.c_str());
cout << "Testing on pre-fed inputs from file:\n";
while (file >> s) {
check(s);
}
cout << "All inputs from the file have been exhausted.\n";
file.close();
int flag;
cout << "Do you want to test some more inputs ? (enter 1 for Yes): ";
cin >> flag;
while(flag==1) {
cout << "Enter input string: ";
cin >> s;
check(s);
cout << "Do you want to test some more inputs ? (enter 1 for Yes): ";
cin >> flag;
}
cout << "Thankyou for using this simulator.\n";
}
int main() {
init();
configure_machine();
test();
return 0;
}