-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab1.cpp
199 lines (175 loc) · 5.83 KB
/
Lab1.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
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
#include "FSMTable.h"
void InNumbers(int *n, int *m) {
std::cout << "Number of states: ";
while (!(std::cin >> *n) || (*n < 1) || (*n > 20)) {
std::cout << "ERROR: Not an integer between 1 and 20.\nNumber of states: ";
std::cin.clear();
std::cin.ignore(132, '\n');
}
std::cout << "Number of letters: ";
while (!(std::cin >> *m) || (*m < 1) || (*m > 20)) {
std::cout << "ERROR: Not an integer between 1 and 20.\nNumber of letters: ";
std::cin.clear();
std::cin.ignore(132, '\n');
}
}
void ChangeState(FSMTable *FSM) {
int n, m, s, l, ifchange = 1;
char c;
do {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Do you want to change a cell? (1/0)" << std::endl;
std::cin >> n;
if (n != 1 && n != 0)
std::cout << "ERROR: Type the number '1' or '0'." << std::endl;
else if (n) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "State: ";
std::cin >> s;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Letter: ";
std::cin >> l;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "New cell's info: ";
std::cin >> c;
if (!FSM->ChangeTerminal(s, l, c)) std::cout << "ERROR: Cannot change this cell." << std::endl;
else {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "The matrix have been changed. Do you want to print it? (1/0)" << std::endl;
std::cin >> m;
if (m) FSM->PrintMatrix();
}
}
else ifchange = 0;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} while (ifchange);
}
void ChangeStatesName(FSMTable *FSM) {
int n, m, ifchange = 0;
char c, nw;
do {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Do you want to change a state's name? (1/0)" << std::endl;
std::cin >> n;
if (n != 1 && n != 0)
std::cout << "ERROR: Type the number '1' or '0'." << std::endl;
else if (n) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Prev state's name: ";
std::cin >> c;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "New state's name: ";
std::cin >> nw;
if (!FSM->ChangeTerminalAll(c, nw))
std::cout << "State's names duplicate or they are the same." << std::endl;
else {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Do you want to print matrix? (1/0)" << std::endl;
std::cin >> m;
if (m) FSM->PrintMatrix();
}
}
else ifchange = 1;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} while (!ifchange);
}
void Task1(FSMTable *FSM) {
FSM->MakeRightGrammar();
FSM->MakeLeftGrammar();
std::cout << "Right grammar:" << std::endl;
if (!FSM->PrintGrammar('r')) std::cout << "Error in printing Right grammar." << std::endl;
std::cout << "Left grammar:" << std::endl;
if (!FSM->PrintGrammar('l')) std::cout << "Error in printing Left grammar." << std::endl;
}
void Task2(FSMTable *FSM) {
std::cout << "Our language: every 3rd symbol - 1." << std::endl;
FSM->MakeMatrixTask2();
std::cout << "Printing the Task 2 Matrix:" << std::endl;
FSM->PrintMatrix();
FSM->MakeRightGrammar();
FSM->MakeLeftGrammar();
std::cout << "Right grammar:" << std::endl;
FSM->PrintGrammar('r');
std::cout << "Left grammar:" << std::endl;
FSM->PrintGrammar('l');
}
void Task3(FSMTable *FSM) {
std::cout << "Our language - Fortran's integers. Latin's alphabet & numbers." << std::endl;
FSM->MakeMatrixTask3();
std::cout << "Printing the Task 3 Matrix:" << std::endl;
FSM->PrintMatrix();
FSM->MakeRightGrammar();
FSM->MakeLeftGrammar();
std::cout << "Right grammar:" << std::endl;
FSM->PrintGrammar('r');
std::cout << "Left grammar:" << std::endl;
FSM->PrintGrammar('l');
int test;
char k;
std::cout << "Do you want to type a word? (0/1)" << std::endl;
std::cin >> k;
if (k == '1') do {
test = FSM->TestTask3();
if (!test) std::cout << "ERROR: First symbol is incorrect. Needed I, J, K, L, M or N (or low)." << std::endl;
else if (test == -1) std::cout << "ERROR: Second symbol is incorrect." << std::endl
<< "Use the whole latin alphabet and numbers." << std::endl;
else if (test == -2) std::cout << "ERROR: Third or futher symbol is incorrect." << std::endl
<< "Use the whole latin alphabet and numbers." << std::endl;
else std::cout << "Your word is ok!" << std::endl;
std::cout << "Do you want to type another word? (0/1)" << std::endl;
std::cin >> k;
} while (k == '1');
}
int main()
{
char task;
int br = 0;
FSMTable Task1FSM, Task2FSM, Task3FSM;
do {
std::cout << "What task do you want to check?" << std::endl;
std::cin >> task;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (task) {
case '0':
br = 1;
break;
case '1':
int n, m;
InNumbers(&n, &m);
if (!Task1FSM.MakeMatrixUser(n, m))
std::cout << "ERROR: Cannot make matrix." << std::endl;
else {
std::cout << "Matrix made successfully." << std::endl;
Task1FSM.PrintMatrix();
}
ChangeState(&Task1FSM);
ChangeStatesName(&Task1FSM);
Task1(&Task1FSM);
break;
case '2':
Task2(&Task2FSM);
break;
case '3':
Task3(&Task3FSM);
break;
default:
std::cout << "I don't understand you. Please retry." << std::endl;
break;
}
} while (!br);
std::cout << "Thank you, goodbye!" << std::endl;
system("pause");
return 0;
}