-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm.cpp
116 lines (110 loc) · 2.54 KB
/
tm.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
#include<iostream>
#include<string>
#define max_node 5
#define right 1
#define left -1
using namespace std;
// +1 for right and -1 for left
int dir=right;
int mxsize;
struct link{
char input;
char change;
int dir;
// if you are going to use the class which is nt declared yet then .... you have to add class keyword
class node *next;
// for the other links connected to node
link *neigh_link;
};
class node{
public :
int node_no;
int link_no;
link *links;//for link linked list
//Default constructor is calling the parameterized constructor
node(){
node(-1);
}
node(int n){
node_no=n;
link_no=0;
links=NULL;
}
//links are establish
void linker(char in,char ch,int dir,node *next){
link *tmp=new link;
tmp->input=in;
tmp->change=ch;
tmp->dir=dir;
tmp->next=next;
tmp->neigh_link=links;
links=tmp;
link_no++;
}
link* check(char in){
link *tmp;
tmp=links;
while(tmp!=NULL){
if(tmp->input==in)
break;
tmp=tmp->neigh_link;
}
return tmp;
}
// trans is to move from one to node
} no[max_node],*trans;
node* node_add(int n){
if(n>mxsize)
return NULL;
return &no[n];
}
int main(){
int ch,no,dir,next,i;
char input,change,str[40];
node *trans,*temp;//
link *aj;//links
cout<<"Enter the no node : ";
cin>>mxsize;
do{
cout<<"\nMenu : ";
cout<<"\n1.Establish links : ";
cout<<"\n2.Enter the string to check : ";
cout<<"\n0.Exit:";
cout<<"\nEnter your choice ";
cin>>ch;
switch(ch)
{case 1: cout<<"Enter the no of node : ";
cin>>no;
temp=node_add(no);
cout<<"Enter the input character : ";
cin>>input;
cout<<"Enter the replacing character : ";
cin>>change;
cout<<"Enter the next node :";
cin>>next;
cout<<"Direction : 1.right 2.Left ";
cin>>dir;
dir=dir==1?right:left;
temp->linker(input,change,dir,node_add(next));
break;
case 2: trans=node_add(0);
cout<<"Enter the string ";
cin>>str;
strcat(str,".");
for(i=0;trans!=NULL;)
{aj=trans->check(str[i]);
if(aj==NULL)
break;
str[i]=aj->change;
i=i+aj->dir;
trans=aj->next;
}
if(trans->link_no==0||str[i]=='\0')
cout<<"String is accepted by turing machine ";
else
cout<<"string is not accepted ";
cout<<str;
break;
}
}while(ch);
}