-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacks_for_regular_languages-1.h
282 lines (243 loc) · 7.76 KB
/
stacks_for_regular_languages-1.h
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Dania Etienne
// stacks_for_regular_languages.h
// COP3530 Data Structures and Algorithms Project 1
// Evaluate if a given string is in a regular language L. To do this write a stack code yourself (Do not use the library stack).Be sure to take advantage of a stacks LIFO nature. You are not allowed to count the input string in any manner.Include the code for your stack as part of the source code you submit.
#ifndef stacks_for_regular_languages_h
#define stacks_for_regular_languages_h
#include <iostream>
#include"stack_p1.h"
using namespace std;
class stacks_for_regular_languages
{
public:
bool L1(char * inputString);
bool L2(char * inputString);
bool L3(char * inputString);
bool L4(char * inputString);
};
//L1: { w: w contains equal numbers of A's and B's (in any order) and no other characters}
bool stacks_for_regular_languages::L1(char * inputString){
if(!*inputString){
return false;
}
stack_p1<char> *S=new stack_p1<char>; //Create empty stack
while (*inputString){
if( (*inputString!='A')&&(*inputString!='B')){
return false;
}
if (S->isEmpty()){
S->push(*inputString);
}
else if(S->peek()==*inputString){
S->push(*inputString);
}
else{
S->pop();
}
inputString++;
}
if (S->isEmpty()){// Check if stack is empty.
return true;
}
else {
return false;
}
return false; // Default
};
//L2: { w: w is of the form (A^n)(B^n), for some n >0 }
bool stacks_for_regular_languages::L2(char * inputString){
if(!*inputString){
return false;
}
stack_p1<char> *S=new stack_p1<char>;
int flag=0;
while (*inputString){
if( (*inputString!='A')&&(*inputString!='B') ){
return false;
}
if(*inputString=='A' && flag==1){
return false;
}
if(*inputString=='B'&& S->isEmpty()){
return false;
}
if (*inputString=='A'&& flag==0){
S->push(*inputString);
}
if(*inputString=='B'&& S->isEmpty()==false && flag==0){
flag=1;
}
if(*inputString=='B'&& S->isEmpty()==false && flag==1){
S->pop();
}
inputString++;
}
if (S->isEmpty()&&(flag==1)){// Check if stack is empty.
return true;
}
return false; //Default
};
//L3: { w: w is of the form ((A^n)(B^n))^p, for some n, p >0 }
bool stacks_for_regular_languages::L3(char * inputString){
if(!*inputString){
return false;
}
stack_p1<char> *S=new stack_p1<char>;
stack_p1<char> *S2=new stack_p1<char>;
int flag=0;
while (*inputString){
if( *inputString!='A'&&*inputString!='B' ){
return false;
}
if (S->isEmpty()&& *inputString=='A' && flag==0){//Begin by pushing an A
S->push(*inputString);
}
if(S->isEmpty()==false && *inputString=='A'&& flag==0){ //Keep pushing A's
S->push(*inputString);
}
if(S2->isEmpty() && *inputString=='B'&& flag==0){
flag=1; //Needs to change at least once.
}
if(S2->isEmpty() && *inputString=='B'&& flag==1){
S2->push(*inputString);
if(S->isEmpty()){
return false;
}
if(S->isEmpty()==false){
S->pop();
}
}
if (S2->isEmpty()==false && *inputString=='B'&& flag==1){
S2->push(*inputString);
if(S->isEmpty()){
return false;
}
if(S->isEmpty()==false){
S->pop();}
}
if(S->isEmpty() && *inputString=='A'&& flag==1){
S->push(*inputString);
if(S2->isEmpty()){
return false;
}
if(S2->isEmpty()==false){
S2->pop();
}
}
if(S->isEmpty()==false && *inputString=='A'&& flag==1){
S->push(*inputString);
if(S2->isEmpty()){
return false;
}
if(S2->isEmpty()==false){
S2->pop();
}
}
inputString++;
}
if (S->isEmpty()&&flag==1){// Check if stack is empty
return true;
}
return false; //Default
};
//L4 { w: w is of the form ((A^n)(B^m))^p, for some m,n,p >0 }
bool stacks_for_regular_languages::L4(char * inputString){
if(!*inputString){
return false;
}
stack_p1<char> *A=new stack_p1<char>;
stack_p1<char> *B=new stack_p1<char>;
stack_p1<char> *P=new stack_p1<char>;
int flag=0;
bool pattern=false;
while (*inputString){
if (*inputString=='A'){
if (flag==0){
A->push(*inputString);
P->push(*inputString);
}
else if (flag==1){
pattern=true;
if (A->isEmpty()==false&& B->isEmpty()==false){
A->pop();
}
else if (A->isEmpty()&& B->isEmpty()==false ){
return false;
}
else if (A->isEmpty()&&B->isEmpty()){
A->push(*inputString);
}
else if (A->isEmpty()==false && B->isEmpty()){
A->push(*inputString);
}
}
}
if ( *inputString=='B'){
if(flag==0 && pattern==false){
if(B->isEmpty()){
if (A->isEmpty()){
return false; // Makes sure we started with A.
}
else{
flag=1;
B->push(*inputString); //Keep pushing B's until pattern is found
P->push(*inputString);
}
}
}
else if (flag==1){
if (pattern==true){
if (A->isEmpty()==false&& B->isEmpty()==false){
B->push(*inputString);
}
else if (A->isEmpty()&& B->isEmpty()==false ){
B->pop();
}
else if (A->isEmpty()&&B->isEmpty()){
return false;
}
else if (A->isEmpty()==false && B->isEmpty()){
B->push(*inputString);
}
}
else if (pattern!=true){
B->push(*inputString);
P->push(*inputString);
}
}
}
if( *inputString!='A'&&*inputString!='B'){
return false;
}
inputString++;
}
if (A->isEmpty()&& B->isEmpty()&& flag==1){
return true;
}
if (A->isEmpty()==false && B->isEmpty()==false && flag==1){
stack_p1<char> *C=new stack_p1<char>; //Create new stack C
while (A->isEmpty()==false){
C->push(A->pop()); //Push all A's onto stack C
}
while(B->isEmpty()==false){ //Push all B's onto stack C
C->push(B->pop());
}
while(P->isEmpty()==false){
if (P->peek()== C->peek()){
P->pop();
C->pop();
}
else{
return false;
}
}
if (C->isEmpty()==false){
return false;
}
else {
return true;
}
}
return false; //default
};
#endif /* stacks_for_regular_languages_h */