-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical_02.cpp
67 lines (57 loc) · 1.58 KB
/
Practical_02.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
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void addEdge(vector <pair<int, int> > adj[], int u,
int v, int wt){
adj[u].push_back(make_pair(v, wt));
}
void init_graph(vector<pair<int,int>>adj[]){
addEdge(adj, 0, 0, 0);
addEdge(adj, 0, 1, 1);
addEdge(adj, 1, 0, 1);
addEdge(adj, 1, 2, 0);
addEdge(adj, 2, 1, 0);
addEdge(adj, 2, 2, 1);
}
class DFA{
int state;
string input_str;
vector<pair<int,int>> graph[3];
public:
DFA(){}
DFA(string init_string){
input_str = init_string;
state = 0;
init_graph(graph);
}
void evaluate_str(){
for(auto x: input_str){
//cout<<x<<endl;
//int inp_char = int(x);
x = int(x)-48;
vector<pair<int,int>> vec = graph[state];
for(int i = 0;i<vec.size();i++){
int new_state = get<0>(vec[i]);
int weight = get<1>(vec[i]);
//cout<<weight<<" "<<x<<endl;
if(weight== x){
cout<<"State Transition: "<<state<<"--->"<<new_state<<endl;
state = new_state;
break;
}
}
}
if(state == 0){
cout<<"Binary string divisible by 3"<<endl;
}
else{
cout<<"Binary string not divisible by 3 and remainder is: "<<state<<endl;
}
}
};
int main(){
DFA dfa_obj = DFA("1001");
dfa_obj.evaluate_str();
return 0;
}