-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLEX.PAS
606 lines (522 loc) · 14.6 KB
/
LEX.PAS
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
unit lex; {database moderating unit}
interface
uses crt, graph;
type multitrans=array[1..10] of string; {database structure}
def=record
word:string[20];
trans:multitrans;
end;
vocabulary=file of def;
voc=object
main_file:vocabulary;
password:string;
procedure convert_from_txt(var size:longint; var fname:string);
procedure start(var flag:boolean; var size:longint);
procedure add_by_hand(var size:longint);
procedure delete_by_hand(var size:longint);
procedure search_def;
procedure game;
procedure change_pass;
procedure info;
end;
var database:voc;
implementation
procedure voc.info; {some information about program for user}
var info_file:text; info_string:string;
begin
gotoxy(20,4);
writeln('HELP');
gotoxy(0,6);
assign(info_file,'info.txt'); {information is downloading from INFO.TXT}
{$I-}
reset(info_file);
{$I+}
if IOResult<>0 then writeln('No information. Something is corrupted')
else begin
while not(eof(info_file)) do
begin
readln(info_file,info_string);
writeln(info_string);
end;
close(info_file);
end;
gotoxy(18,24);
writeln('PRESS ANY KEY for exit');
readkey;
exit;
end;
procedure voc.start; {initialization of database}
var pass_file:text;
begin
flag:=false;
repeat
if flag then
begin
clrscr;
writeln('VOC.DAT is not found. PRESS ANY KEY');
readkey;
flag:=false;
exit;
end;
{$I-}
assign(main_file,'Voc.dat');
reset(main_file);
{$I+}
flag:=true;
until ioresult=0;
size:=filesize(main_file);
close(main_file);
assign(pass_file,'Security.xxx'); {downloading of information for authorization}
reset(pass_file);
readln(pass_file,password);
close(pass_file);
end;
procedure voc.convert_from_txt; {downloading database from txt-file}
var ft:text; c:char; flag:boolean; def3:def;
tmpc:char; answer, tmps, fpath,way:string; k,i:longint;
begin
k:=1;
{authorization}
if password<>'' then begin
repeat
clrscr;
if k=6 then begin writeln('Sorry, but you can''t change database');
exit; end;
writeln('Please enter your secret password');
if k>1 then begin
textcolor(red);
writeln(' (',6-k,' tries left)');
textcolor(black); end;
readln(answer);
inc(k);
until answer=password; end;
clrscr;
{linking to user's file}
writeln('Input file name');
writeln;
writeln('[Example:] Vocabulary');
readln(fname);
CLRSCR;
writeln('Input filepath. Press ENTER if it is in the database folder');
writeln;
writeln('[Examples:] D:\Folder1\');
readln(fpath);
way:=fpath+fname+'.txt';
writeln(fpath+fname+'.txt');
flag:=false;
for i:=0 to length(way) do
if way[i]=' ' then flag:=true;
if flag then way:=''''+way+'''';
readkey;
{$i-}
assign(ft,way);
reset(ft);
{$i+}
if ioresult<>0 then begin writeln('Txt file not found. Press ENTER');
fname:='Vocabulary';
readkey; exit; end;
{downloading information from user's file}
rewrite(main_file);
repeat
tmps:='';
repeat
repeat
read(ft,tmpc);
until tmpc<>'';
if tmpc<>'-' then
tmps:=tmps+tmpc;
until (tmpc='-') or eoln(ft);
if not eoln (ft)
then
read(ft,tmpc);
delete(tmps,length(tmps),1);
def3.word:=tmps;
k:=1;
repeat
tmps:='';
repeat
repeat
read(ft,tmpc);
until tmpc<>'';
if tmpc<>',' then
tmps:=tmps+tmpc;
until (tmpc=',') or eoln(ft);
if not eoln (ft)
then
read(ft,tmpc);
def3.trans[k]:=tmps;
def3.trans[k+1]:='';
k:=k+1;
until eoln(ft);
readln(ft);
write(main_file,def3);
until eof(ft);
close(ft);
close(main_file);
reset(main_file);
size:=filesize(main_file);
close(main_file);
{message to user about result}
writeln('Information from ',fname,'.txt is downloaded into database');
writeln('PRESS ESC');
while keypressed do readkey;
c:=readkey; if c=#27 then exit;
end;
procedure voc.add_by_hand; {adding words to database}
var sign,answer,inp_word,inp_def:string; c:char; i,k,j:longint; def1a,def4:def; flag:boolean;
begin
flag:=false;
k:=1;
{authorization}
if password<>'' then begin
repeat
clrscr;
if k=6 then begin writeln('Sorry, but you can''t change database');
exit; end;
writeln('Please enter your secret password');
if k>1 then begin
textcolor(red);
writeln(' (',6-k,' tries left)');
textcolor(black); end;
while keypressed do readkey;
c:=readkey;
if c=#27 then exit;
readln(answer);
inc(k);
until answer=password; end;
clrscr;
reset(main_file);
repeat
j:=0;
writeln('Input new word');
repeat
readln(inp_word);
until inp_word<>'';
{search for same words}
while j<filesize(main_file) do begin
seek(main_file,j);
read(main_file,def4);
if inp_word=def4.word
then begin
writeln('At first you must delete an old same word');
flag:=true;
end;
inc(j);
end;
if flag=false then begin
seek(main_file,filesize(main_file));
j:=0;
{adding}
with def1a do begin
word:=inp_word;
writeln('Input meanings');
i:=0;
repeat
i:=i+1;
readln(trans[i]);
until trans[i]='';
end;
write(main_file,def1a); end;
{dialog with user: to exit or to continue}
writeln('Press Esc or continue adding');
while keypressed do readkey;
c:=readkey;
until c=#27;
size:=filesize(main_file);
close(main_file);
end;
procedure voc.game; {studying new words in game mode}
var answer,correct:string; def2:def; q,rand,i,count_of_turns:longint;
letter,keycode, c:char; f:byte;
letters:set of char;
right_letters, false_letters:set of char;
false: array [1..20] of char;
begin
randomize;
reset(main_file);
if filesize(main_file)=0 then exit;
{search for word to ask}
q:=1;
repeat
rand:=random(filesize(main_file)+1);
until rand<>0;
seek(main_file,rand-1);
read(main_file,def2);
correct:=def2.word;
if (length(correct)=0) or (length(def2.trans[1])=0) then exit;
{asking by output of defenitions}
writeln('Can your guess?');
writeln;
writeln(length(correct),' symbols');
writeln;
textcolor(red);
writeln('Translate from one language to another:');
writeln;
repeat
writeln(def2.trans[q]);
q:=q+1;
until def2.trans[q]='';
writeln;
if upcase(correct[1])=correct[1] then begin
textcolor(blue);
write('First letter is ');
textcolor(red);
writeln(correct[1]); end;
writeln;
textcolor(black);
letters:=[];
right_letters:=[];
false_letters:=[];
f:=0;
for i:=1 to length(correct) do
letters:=letters+[correct[i]];
if upcase(correct[1])=correct[1] then
letters:=letters-[correct[1]];
if upcase(correct[1])=correct[1] then
right_letters:=right_letters+[correct[1]];
count_of_turns:=length(correct);
repeat
writeln(count_of_turns,' tries left');
writeln('Input letter');
repeat
readln(letter);
until (not (letter in right_letters)) and (letter<>'')
and (not (letter in false_letters));
clrscr;
if letter in letters then begin
letters:=letters-[letter];
right_letters:=right_letters+[letter]; end
else begin
false_letters:=false_letters+[letter];
inc(f);
false[f]:=letter;
{output of correct and false letters}
dec(count_of_turns); end;
if f<>0 then begin
textcolor(red);
write('Sorry, no such symbols: ');
for i:=1 to f do
write(false[i],' ');
writeln;
textcolor(black); end;
for i:=1 to length(correct) do begin
if (correct[i] in letters)
then begin
textcolor(blue);
write('_ ');
textcolor(black);
end
else begin
textcolor(blue);
write(correct[i],' ');
textcolor(black);
end; end;
writeln;
writeln('Can your guess?');
writeln;
writeln(length(correct),' symbols');
writeln;
q:=1;
repeat
writeln(def2.trans[q]);
q:=q+1;
until def2.trans[q]='';
writeln;
writeln;
until (letters=[]) or (count_of_turns=0);
clrscr;
{end of game and output of final result}
if letters=[] then answer:=correct else answer:='';
if answer=correct then begin
textcolor(blue);
writeln('Yes!');
textcolor(black);
write('This is ');
textcolor(blue);
writeln(correct);
textcolor(black);
end
else
begin
textcolor(red);
writeln('No :(');
textcolor(black);
write('Correct answer is ');
textcolor(lightblue);
writeln(correct);
textcolor(black);
end;
writeln('Press ENTER');
readkey;
Close(main_file);
end;
procedure voc.change_pass; {to change authorization password}
var answer1, answer2:string; k:byte; pass_file:text; c:char;
begin
{authorisation}
k:=1;
if password<>'' then begin
writeln('Please, input old password');
repeat
if k=6 then begin writeln('Please, input old password');
exit; end;
if (k>1) and (k<6) then begin textcolor(red);
writeln(' (',6-k,' tries left)'); textcolor(black); end;
readln(answer1);
inc(k);
until answer1=password; end;
k:=0;
repeat
clrscr;
if k=6 then
begin writeln('Sorry, but you can''t change password. PRESS ENTER');
end;
if k>0 then writeln('Something wrong. Please, try again');
{asking for new password}
writeln;
writeln('Input new password. Press Enter if you don''t want any password');
readln(answer1);
if answer1<>'' then begin
{confirming of new password}
writeln('Please, confirm (input one more time) new password');
readln(answer2); end else answer2:='';
k:=k+1;
until answer1=answer2;
password:=answer1;
{retaining of new password}
assign(pass_file,'Security.xxx');
rewrite(pass_file);
write(pass_file, password);
close(pass_file);
end;
procedure voc.search_def; {search in database}
var def1b:def; i,q,j,k,set_count,similar,count,ideal:longint; tmp:text; out1a,qt:string; flag:boolean;
quest_set, tmp_set:set of char; c,key:char;
qt_letters:set of char; similar_words:array [1..100] of string[20];
begin
repeat
clrscr;
{asking for word to search in database}
writeln('Input word to search');
repeat
readln(qt);
until qt<>'';
qt_letters:=[]; set_count:=0;
for i:=1 to length(qt) do begin
qt_letters:=qt_letters+[qt[i]]; inc(set_count); end;
flag:=false;
reset(main_file);
i:=0;
count:=0;
while i<filesize(main_file) do begin
seek(main_file,i);
read(main_file,def1b); q:=1;
similar:=0;
for j:=0 to length(def1b.word) do
if (def1b.word[j] in qt_letters) then inc(similar);
if (similar>=set_count) and (count<100) then
begin
inc(count);
similar_words[count]:=def1b.word;
end;
{search by title language}
if def1b.word=qt then
repeat
textcolor(red);
writeln(def1b.trans[q]);
flag:=true;
q:=q+1;
until def1b.trans[q]='';
{search by language of definitions}
q:=1;
repeat
if def1b.trans[q]=qt then begin
textcolor(red);
writeln(def1b.word);
flag:=true;
end;
q:=q+1;
until def1b.trans[q]='';
i:=i+1;
end;
textcolor(black);
if flag=false then begin
writeln('No results for this word');
if count<>0 then
{output of words with same letters}
writeln('Maybe, you tried to find one of this words?');
textcolor(blue);
i:=0;
repeat
inc(i);
writeln(similar_words[i]);
until (i=15) or (i=count);
textcolor(black);
end;
close(main_file);
writeln;
writeln('Press ESC for exit or ANY OTHER KEY for new search');
key:=readkey;
until key=#27;
end;
procedure voc.delete_by_hand; {erasing words}
var i,j,k:longint; c:char; dt,def1d:def; flag:boolean; answer,qu:string;
begin
{authorisation}
k:=1; if password<>'' then begin
repeat
clrscr;
if k=6 then begin writeln('Sorry, but you can''t change database');
writeln('Press ENTER');
readkey;
exit; end;
writeln('Please enter your secret password');
if (k>1) and (k<6) then begin textcolor(red);
writeln(' (',6-k,' tries left)'); textcolor(black); end;
readln(answer);
inc(k);
until answer=password; end;
{asking for word to delete}
writeln('Input word to delete');
repeat
readln(qu);
until qu<>'';
clrscr;
flag:=false;
reset(main_file);
i:=0;
{database erasing}
if qu='Delete all words' then begin seek(main_file,0); truncate(main_file);
flag:=true; writeln('Database is clean');
writeln('Press ENTER');
readkey;
end;
{one-word erasing}
while i<filesize(main_file) do begin
seek(main_file,i);
read(main_file,def1d);
if def1d.word=qu then begin if filesize(main_file)<>1 then
for j:=i to filesize(main_file)-2 do
begin
seek(main_file,j+1);
read(main_file,dt);
seek(main_file,j);
write(main_file,dt);
end;
seek(main_file,filesize(main_file)-1);
truncate(main_file);
writeln('Successful');
writeln('Press ENTER');
readkey;
flag:=true;
end; i:=i+1;
end;
if flag=false then begin
writeln('Word is not found');
writeln('Press ENTER');
readkey;
end;
size:=filesize(main_file);
close(main_file);
end;
end.