-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.cpp
More file actions
338 lines (311 loc) · 7.83 KB
/
source.cpp
File metadata and controls
338 lines (311 loc) · 7.83 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <iostream>
#include <Windows.h>
#include <string>
int state = 0; // curr_state variable
using namespace std;
void print_tst(const string &str, int &i) // Trap_State function.. prints out the trap state explicilty
{
for (; i < str.length(); i++)
{
cout << ' ' << str[i] << " Trap" << ' ' << '\n';
}
}
void print_st(const char& c, const int& st)
{
cout << ' ' << c << " q" << st << ' ' << '\n'; // print the current state along with the character
}
void eve_rem(const string &str, int &i, const int& st) // this function evaluates the remaining string
{
for (; i < str.length(); i++)
{
if (str[i] == '.')
print_st(str[i], st);
else if (str[i] == 'c')
{
print_st(str[i++], 8);
if (str[i] == 'o' && i != str.length())
print_st(str[i++], 9);
if (str[i] == 'm' && i != str.length())
print_st(str[i++], 10);
if (i != str.length())
print_tst(str, i); // if string hasnt completed yet send it to the trap state
}
else if (str[i] == 'p')
{
print_st(str[i++], 11);
if (str[i] == 'k' && i != str.length())
print_st(str[i++], 12);
if (i != str.length())
print_tst(str, i); // if string hasnt completed yet send it to the trap state
}
else
print_tst(str, i); // if the string doesnt have . or c or p then send it to the trap state
}
}
void print_st_com(const string& str, int st) // printing .com with respect to each state
{
int len = str.length();
print_st(str[len - 4], st);
if (st == 7)
st++;
if (st == 6)
st = 8;
print_st(str[len - 3], st++);
print_st(str[len - 2], st++);
print_st(str[len - 1], st++);
}
void print_st_pk(const string& str, int st) // printing .pk with respect to each state
{
int len = str.length();
print_st(str[len - 3], st);
if (st == 7)
st = 11;
if (st == 6)
st = 11;
print_st(str[len - 2], st++);
print_st(str[len - 1], st++);
}
bool Check_dot_pk(const string &str) // Function Checks that whether last 4 digits are .com
{
int len = str.length();
if (str[len - 3] != '.')
return false;
if (str[len - 2] != 'p')
return false;
if (str[len - 1] != 'k')
return false;
return true;
}
bool Check_dot_com(const string &str) // Function Checks that whether last 4 digits are .com
{
int len = str.length();
if (str[len - 4] != '.')
return false;
if (str[len - 3] != 'c')
return false;
if (str[len - 2] != 'o')
return false;
if (str[len - 1] != 'm')
return false;
return true;
}
int check_S2S3(const string &str, int &i, int st) // function checks the two sets S1 and S2 states
{
int len = str.length();
for (; str[i] != '.' && i != str.length(); i++) // begin the evaluation of S2 set
{
if (str[i] >= 'a' && str[i] <= 'z')
{
print_st(str[i], st);
}
else
{
print_tst(str, i);
return 0;
}
if (st == 6)
st--;
}
if (i != str.length()) // if the string hasnt been ended then evaluate further set S3
{
int diff = len - i;
if (st == 4)
st = 7;
else if (st == 5)
st = 6;
if (diff == 4 || diff == 3)
{
if (Check_dot_com(str) && (str.length() >= 5))
{
print_st_com(str, st);
return 1;
}
else if (Check_dot_pk(str) && (str.length() >= 4))
{
print_st_pk(str, st);
return 1;
}
else
{ // evaluate the remaining string
eve_rem(str, i, st);
return 0; // if rejected, return 0
}
}
else
{ // evaluate the remaining string
eve_rem(str, i, st);
return 0; // if rejected, return 0
}
}
return 0; // if rejected, return 0
}
void main()
{
cout << "Haris Ali : 18L-1247\nTheory of Automata - Programming Assignment 1\n";
cout << "Program checks if a website name is correct\n\n";
start:
{
cout << "\n\nWant to enter a string? (y/n) : ";
char ch = NULL;
cin >> ch;
while (ch != 'n' && ch != 'N' && ch != 'y' && ch != 'Y')
{
cout << "\nPlease Enter a valid input i.e (y/n)!\n";
cin.ignore();
cin >> ch;
}
if (ch == 'n' || ch == 'N') // if the user presses N or n, then exit the program
{
cout << "\nAs the user have choosen not to enter any string, hence the program will now terminate!\n";
system("pause");
return; // terminating the program
}
else
{ // else, create a string and take user input
string str;
state = 0;
cout << "\nEnter the string: ";
cin.ignore();
getline(cin, str);
int len = str.length(), wc = 0;
for (int i = 0; i != len; i++) // traversing the string in this loop
{
if (str[i] == 'w') // if 'w' comes evaluate it seperately
{
if (wc <= 3) // if 3 w's are completed then dont move further
{
print_st(str[i], ++state);
wc++;
}
else
{
print_st(str[i], 4);
wc++;
}
}
else if (i > 0 && str[i] == '.') // if a dot comes after first digit then evaluate it in this block
{
int sw = 6;
if (wc > 3) // check whether www has passed the evaluation
sw = 7; // if yes then state will be changed
if (str[i + 1] == 'c')
{
if (str[i + 2] == 'o' && str[i + 3] == 'm' && i + 4 == str.length())
{
print_st_com(str, sw);
cout << "\nAccepted";
break;
}
if ((str[i + 1] >= 'a' && str[i + 1] <= 'z'))
{
if (wc > 3) // check whether www has passed the evaluation
{
eve_rem(str, i, sw); // if yes then evaluate remaining string
cout << "\nRejected";
break;
}
print_st(str[i++], 6);
if (check_S2S3(str, i, 5) == 1) // check the S2S3 sets
{
cout << "\nAccepted";
}
else
{
cout << "\nRejected";
}
break;
}
else
{
eve_rem(str, i, sw);
cout << "\nRejected";
break;
}
}
else if (str[i + 1] == 'p')
{
if (str[i + 2] == 'k' && i + 3 == str.length())
{
print_st_pk(str, sw);
cout << "\nAccepted";
break;
}
else if ((str[i + 1] >= 'a' && str[i + 1] <= 'z'))
{
if (wc > 3) // check whether www has passed the evaluation
{
eve_rem(str, i, sw); // if yes then evaluate remaining string
cout << "\nRejected";
break;
}
print_st(str[i++], 6);
if (check_S2S3(str, i, 5) == 1) // check the S2S3 sets
{
cout << "\nAccepted";
}
else
{
cout << "\nRejected";
}
break;
}
else
{
eve_rem(str, i, sw); // evaluate the remaining string
cout << "\nRejected";
break;
}
}
else if (str[i + 1] >= 'a' && str[i + 1] <= 'z') // if a to z letters come then execute this block
{
if (wc > 3) // check whether www has passed the evaluation
{
eve_rem(str, i, sw); // if yes then evaluate remaning string
cout << "\nRejected";
break;
}
print_st(str[i++], 6);
if (check_S2S3(str, i, 5) == 1) // check the S2S3 sets
{
cout << "\nAccepted";
}
else
{
cout << "\nRejected";
}
break;
}
else
{
eve_rem(str, i, sw); // evaluate the remaining string
cout << "\nRejected";
break;
}
}
else if (str[i] >= 'a' && str[i] <= 'z') // if there's a to z input move on to the q5, if w comes first then this block will be ignored
{
int st = 5;
if (wc > 3) // check whether 4 w's have come already
st = 4;
if (check_S2S3(str, i, st) == 1) // check the S2S3 sets
{
cout << "\nAccepted";
}
else
{
cout << "\nRejected";
}
break;
}
else
{ // Trap state has occured
print_tst(str, i); // Rest of the inputs will be in trap
cout << "\n Rejected";
break;
}
}
}
}
goto start; // transfer the control of the program again to the start label (moving to the step-2 again)
system("pause");
}