-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
474 lines (442 loc) · 19.1 KB
/
main.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <iostream>
#include <fstream>
#include <string>
#include <climits>
using namespace std;
/**
Р.2) вставить в предложении новое слово перед заданным словом,
Р.6) удалить в предложении знак пунктуации (указанный и/или все).
Указание определенного предложения:
П.2) предложение, начинающееся с указанного слова,
Указание заданного слова:
С.7) содержащее заданную последовательность символов
**/
struct MyStor {//for save all inform
string nameOfFile;
int sentenceCounter = 0;//порядковый номер предложения
string commandLine;//line with command 6
int pos = -1;// P2 position
int blockerP2 = 0;
int blockerH2 = 0;
int blockerC7 = 0;
int blockerP6 = 0;
char *newWord = new char[CHAR_MAX];// P2 word
int nwLength = 0;// р2 newWord length
char *word = new char[CHAR_MAX];//C7
int wLength = 0;//c7 word length
};
struct nexSumb//структура хранения всех слов и символов
{
nexSumb* nWord;// указатель на следующее слово
int length = 0;// длина слова
char sent[20];// само слово
int flag = 0;//punctuation checker
//0 - не пунктуация, 1 - пунктуаця, 2 - многоточие
int deleted = 0; // 0 - не удалено, 1 - удалено
};
MyStor myStor;
class Sentence//новое предложение
{
public:
// char **sentenceWith = new char *[CHAR_MAX];// указатель на начало слов и знаков пунктуации
int sWCounter = 0;// подсчет количества элементов в sentenceWith
//char **punctuation = new char *[CHAR_MAX];// указатель на знаки пунктуации
//int *wordLength = new int[INT16_MAX];// для sentenceWith
//int pCounter = 0;// подсчет количества элементов в punctuation
nexSumb *firstWord; // указатель на первое слово
//для создания нового предложения просто ввожу строку
Sentence(string line) { splitter(line); }
//метод парсит предложение на слова и вызывает метод добавления
void splitter(string line) {
int totalWordCount = 0;// количество букв в слове, на один меньше для записи в массив
int i = 0;
char *wordFast = new char [line.length()];
while (line.length() > i && line[i] != ' ')
{
wordFast[i] = line[i];
i++;
totalWordCount++;
}
nexSumb *tmp = add(wordFast,totalWordCount);
nexSumb *prevWord = new nexSumb;
if(sWCounter == 2)prevWord = tmp->nWord;
else prevWord = firstWord;
int j = totalWordCount + 1;// корректировка на пробел
totalWordCount = 0;
for (int i = j; i < line.length() + 1; ++i) {
if (line[i] == ' ' || i == line.length())
{
nexSumb *temp;
int controller = sWCounter;
temp = add(wordFast, totalWordCount);// связываю его с предыдущим элементом
prevWord->nWord = temp;
controller +=2;
if(controller == sWCounter) prevWord = temp->nWord;
else prevWord = temp; // элемент становится предыдущим
totalWordCount = 0;
}
else {
wordFast[totalWordCount] = line[i];
totalWordCount++;
}
}
commandParser();
strPrinter();
delete[] wordFast;
}
// метод добавляет новое слово в структуру, если была пунктуация, то добавится и она отдельным словом. Возвращает
// указатель на это слово
nexSumb *add(char *wordAdd, int length) {
int controller = sWCounter;
int fastLength = length;// хранение первоначального размера
nexSumb *sumb = NULL;// знак препинания если будет
//если последняя буква в слове - знак препинания, делаю длину слова меньше на 1
if (punctuationChecker(wordAdd[length - 1]) || wordAdd[length - 1] == '.') {
if (wordAdd[length - 2] == '.')// проверяю на многоточие
{
nexSumb *nexSumb = new struct nexSumb;
nexSumb->flag = 2;// многоточие
nexSumb->length = 3;
nexSumb->sent[0] = '.';
nexSumb->sent[1] = '.';
nexSumb->sent[2] = '.';
sumb = nexSumb;
length -= 3;
sWCounter++;
} else { // если не многоточие, добавляю
nexSumb *nexSumb = new struct nexSumb;
nexSumb->flag = 1;
nexSumb->length = 1;
nexSumb->sent[0] = wordAdd[length - 1];
sumb = nexSumb;
length -= 1;
sWCounter++;
}
}//если была пунктуация я создал новую структуру
nexSumb *word = new nexSumb;
for (int j = 0; j < length; ++j) {
word->sent[j] = wordAdd[j];
}// основное добавление слова
sWCounter++;
word->length = length;
if (length < fastLength) //проверяю, был ли в конце знак препинания и добавляю в слово как следующий элемент
{
word->nWord = sumb;
}
if(controller == 0) firstWord = word;
return word;
}
// check punctuation mark
// if the char is a punctuation mark function return true, and return false if is not
static bool punctuationChecker(char mark) {
switch (mark) {
case ',':
return true;
case ':':
return true;
case ';':
return true;
case '-':
return true;
case '=':
return true;
case '_':
return true;
case '*':
return true;
case '(':
return true;
case ')':
return true;
case '{':
return true;
case '}':
return true;
case '[':
return true;
case ']':
return true;
case '/':
return true;
case '|':
return true;
case '\\':
return true;
case '+':
return true;
case '@':
return true;
case '#':
return true;
case '$':
return true;
case '%':
return true;
case '^':
return true;
case '!':
return true;//возможно вынести в отдельную проверку
case '?':
return true;//возможно вынести в отдельную проверку
default:
return false;
}
}
// в этом методе буду разбивать строку комманд и сразу вызывать связанные методы
void commandParser() {
for (int i = 0; i < myStor.commandLine.length(); ++i) {
int length = 0;
int tempI;
if (myStor.commandLine[i] == 'H' && myStor.blockerH2 == 0) {
tempI = i;
i += 4;//+ 2 для пропуска номера и точки
while (myStor.commandLine[i] != ',') {
length++;
i++;
}
i = tempI;
i +=4;
char *nxWord = new char[length];
length = 0;
while (myStor.commandLine[i] != ',') {
nxWord[length] = myStor.commandLine[i];
length++;
i++;
}
startOnThisWord(nxWord, length);
delete[] nxWord;
}
if (myStor.commandLine[i] == 'C' && myStor.blockerC7 == 0) {
tempI = i;
i += 4;//
while (myStor.commandLine[i] != ',') {
length++;
i++;
}
i = tempI;
i += 4;
char *nxWord = new char[length];
length = 0;
while (myStor.commandLine[i] != ',') {
nxWord[length] = myStor.commandLine[i];
length++;
i++;
}
usingSumbolSequence(nxWord, length);
myStor.nwLength = length;
}
if (myStor.commandLine[i] == 'P') {
int index = (int) myStor.commandLine[i + 4] - 48;
if (index == myStor.sentenceCounter) {
if (myStor.commandLine[i + 2] == '6' && myStor.blockerP6 == 0) {
i += 6;
if (myStor.commandLine[i] == '0') {
i += 1;
deletePunctuationMark(0, reinterpret_cast<char *>('r'), 0);
} else {
if (myStor.commandLine[i] != '.') {
char *punctu = &myStor.commandLine[i];
deletePunctuationMark(1, punctu, 1);
i += 2;
} else {
char *punctu = new char[3]{'.','.','.'};
deletePunctuationMark(1, punctu, 3);
i += 2;
}
}
} else if (myStor.blockerP2 == 0){//&& blockerP2 == 0
i += 4;
myStor.pos = myStor.commandLine[i + 2] - 48;// позиция
i += 2;
length = 0;
while (myStor.commandLine[i + 2] != ',') {
myStor.newWord[length] = myStor.commandLine[i + 2];
length++;
i++;
}
myStor.nwLength = length;
}
}
}
}
}
// / isDeleteAll - если не 0 то удалить только punctMarc,
// иначе удалить все, модифицирует punctuation массив, заменяя выбранные знаки пробелом
void deletePunctuationMark(int isDeleteAll, char *punctMarc, int length) {
nexSumb *nextSumb = firstWord->nWord;// ссылаю сразу на 2 элемент, если что удаляю его
if(isDeleteAll == 1)
{
if(firstWord->flag != 0 && length == firstWord->length)
if(firstWord->sent[0] == '.' || firstWord->sent[0] == *punctMarc)
{
firstWord->deleted = 1;// удалил знак
}
for (int i = 1; i < sWCounter; ++i)
{// прохожу по всем элементам в
if(nextSumb->flag != 0 && length == nextSumb->length)
if(nextSumb->sent[0] == *punctMarc)
{
nextSumb->deleted = 1;// удалил знак
}
nexSumb *temp = nextSumb->nWord;
nextSumb = temp;
}
}
else
{
if(firstWord->flag != 0) firstWord->deleted = 1;
for (int i = 1; i < sWCounter; ++i)
{// прохожу по всем элементам в
if(nextSumb->flag != 0 )nextSumb->deleted = 1;// удалил знак
nexSumb *temp = nextSumb->nWord;
nextSumb = temp;
}
}
myStor.blockerP6 = 1;
}
void startOnThisWord(char *word, int length) {
if (firstWord->length == length) {
int controller = 0;
for (int i = 0; i < firstWord->length; ++i) {
if (firstWord->sent[i] != word[i]) break;
else controller++;
}
if (controller == length) {
myStor.blockerH2 = 1;
}
}
}//H2
void usingSumbolSequence(char *wordUse, int length) {
int counter = 0;
nexSumb *nextSumb = firstWord->nWord;// ссылаю сразу на 2 элемент, если что удаляю его
if(firstWord->flag == 0 && length <= firstWord->length)
{
for (int i = 0; i < firstWord->length; ++i) {
if (firstWord->sent[i] == wordUse[counter]) counter++;
else if(length == counter) break;
else counter = 0;
}
if (counter == length) {
myStor.word = firstWord->sent;
myStor.blockerC7 = -1;
myStor.wLength = firstWord->length;
return;
}
}
for (int i = 1; i < sWCounter; ++i)
{// прохожу по всем элементам
if(nextSumb->flag == 0 && length <= nextSumb->length)
{
for (int j = 0; j < nextSumb->length; ++j) {
if (nextSumb->sent[j] == wordUse[counter]) counter++;
else if(length == counter) break;//если подошло
// else if (length-=i < length) break;
else counter = 0;
}
}
nexSumb *temp = nextSumb->nWord;
nextSumb = temp;
}
if (counter == length) {
myStor.word = firstWord->sent;
myStor.blockerC7 = -1;
myStor.wLength = firstWord->length;
return;
}
}
// здесь я возвращаю адрес первого вхождения указанного слова
void strPrinter() {
int counter = 1; // подсчет вывода слов для пункта P2
ofstream out;// поток для записи
nexSumb *nextSumb = firstWord;// cледующий элемент
out.open(myStor.nameOfFile, ios::app);// окрываем файл для записи
if (out.is_open()) {
for (int i = 0; i < sWCounter; ++i) {
if (myStor.blockerH2 == 1) {
out << "->" << " ";
myStor.blockerH2 = -1;
}
if (myStor.pos == counter && myStor.blockerP2 == 0)//проверка для пункта р2
{
if (nextSumb->flag == 0) {
if (myStor.pos - 1 != 0) out << " ";
for (int j = 0; j < myStor.nwLength; ++j) {
out << myStor.newWord[j];
}
if (myStor.pos - 1 == 0) out << " ";
myStor.blockerP2 = 1;//блокирую метод p2
}
}
if (nextSumb->deleted == 0) // слово не удалено
{
if (nextSumb->flag == 0) {
if (i != 0)out << " ";// если не первое слово или не пунктуация выведу пробел
counter++;
}
for (int j = 0; j < nextSumb->length; ++j) {
out << nextSumb->sent[j];
}
}
nexSumb *temp = nextSumb->nWord;
nextSumb = temp;
}
}
out << '\n';
}
};
int main() {
string nameOfFile;
cout << "Hello!\n"
"This program can:\n"
"R.2) insert in the sentence a new word before the given word,\n"
"R.6) remove the punctuation mark (indicated and / or all) in the sentence.\n"
"Indication of a specific sentence:\n"
"H.2) a sentence starting with the specified word,\n"
"Indication of a given word:\n"
"C.7) containing a given sequence of characters\n"
"to run programs \";\n"
"To start it is necessary:\n"
"1) enter the file from where you will take the sentences (each sentence from a new line),\n"
" and on the next line, the file where you will display the sentences (these files must be different!)\n"
"2) enter a sequence of commands separated by commas\n"
"For C7 - C.7.ing, where C.7 is the command number, and ing is what we will look for\n"
"For H.2 - H.2.hello, - where H.2 is the command number and hello is what we are looking for\n"
"For P.6 - P.6.2.;, - where P.6 is the number of the command,\n"
" 2 is the number of the sentence,; is the sign we are about to remove.\n"
" Instead, you can put 0, then the program will delete all characters\n"
"For P.2 - P.2.2.1.trtrtr, - where P.2 is the command number, 2 is the sentence number,\n"
"1 is the word before which a new word must be inserted.\n"
"The most important:\n"
"1) each sentence from a new line\n"
"2) each line of commands should end with a comma\n"
"3) input and output files must be different\n";
cin >> nameOfFile; // where i take text
cin >> myStor.nameOfFile; // where i write answer
cin >> myStor.commandLine;
ifstream in;
in.open(nameOfFile);
string line;
if (in.is_open()) {
while (getline(in, line)) {
myStor.sentenceCounter++;
new Sentence(line);
if (myStor.blockerC7 == -1) {
ofstream out; // поток для записи
out.open(myStor.nameOfFile, ios::app);// окрываем файл для записи
if (out.is_open()) {
out << "c7 result is: ";
for (int j = 0; j < myStor.wLength; ++j) {
out << myStor.word[j];
}
out << '\n';
myStor.blockerC7 = 1;
delete [] myStor.word;
}
out.close();
}
}
}
in.close();
}
// в отчете только перемахнуть код на новый
// глянуть флаги пунктуации в ADD