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
| uses crt;
const
max_notes_number = 8;
menu_number = 5;
key_down = #80; key_up = #72; key_enter = #13; key_backspace = #8;
empty = '...';
type note_type = record
name: string;
sname: string;
tel: string;
birthday: array[1..3] of integer;
end;
type note_array = array[1..max_notes_number] of note_type;
procedure sort_notes(var note: note_array);
var flag: boolean;
temp: note_type;
i: integer;
begin
flag := true;
for i := 1 to max_notes_number do begin
if note[i].name = empty then note[i].name := #255;
end;
while flag do begin
flag := false;
for i := 1 to max_notes_number-1 do begin
if note[i].name > note[i+1].name then begin
temp := note[i];
note[i] := note[i+1];
note[i+1] := temp;
flag := true;
end;
end;
end;
for i := 1 to max_notes_number do begin
if note[i].name = #255 then note[i].name := empty;
end;
end;
procedure load(filename: string; var note: note_array);
var notes_number, field_to_load, i, j: integer;
file_in: text;
s: string;
error_code: integer;
begin
assign(file_in, filename); reset(file_in);
notes_number := 1;
field_to_load := 1;
repeat
readln(file_in, s);
if s = '' then begin
field_to_load := 1;
inc(notes_number);
end
else if s <> 'END' then begin
case field_to_load of
1: note[notes_number].name := s;
2: note[notes_number].sname := s;
3: note[notes_number].tel:= s;
4: begin
for i := 1 to 3 do begin
if i < 3 then begin
val(copy(s, 1, pos('.', s)-1), note[notes_number].birthday[i], error_code);
delete(s, 1, pos('.', s));
end
else begin
val(s, note[notes_number].birthday[i], error_code);
end;
end;
end;
end;
inc(field_to_load);
end;
until s = 'END';
for i := 1 to max_notes_number do begin
if note[i].name = '' then note[i].name := '...';
if note[i].sname = '' then note[i].sname := '...';
end;
close(file_in);
sort_notes(note);
end;
function get_birthday(note: note_array; i: integer): string;
var j: integer;
s, s_temp: string;
begin
s := '';
if (note[i].birthday[1] = 0) or (note[i].birthday[2] = 0) or (note[i].birthday[3] = 0) then begin
s := '...';
end
else begin
for j := 1 to 3 do begin
str(note[i].birthday[j], s_temp);
if note[i].birthday[j] < 10 then s := s + '0';
s := s + s_temp;
if j < 3 then s := s + '.';
end;
end;
get_birthday := s;
end;
procedure save(filename: string; var note: note_array);
var i, j: integer;
file_out: text;
first: boolean;
begin
assign(file_out, filename);
rewrite(file_out);
first := true;
for i := 1 to max_notes_number do begin
if first = true then first := false
else writeln(file_out);
writeln(file_out, note[i].name);
writeln(file_out, note[i].sname);
writeln(file_out, note[i].tel);
writeln(file_out, get_birthday(note, i));
writeln(file_out);
end;
write(file_out, 'END');
close(file_out);
end;
procedure enter_note(text: string; var what_to_enter: string);
var s: string;
begin
TextColor(yellow);
writeln(text);
TextColor(white);
readln(s);
if s <> '' then begin
what_to_enter := s;
writeln;
end
else begin
gotoXY(WhereX, WhereY-1);
if (what_to_enter = '') or (what_to_enter = #255) or (pos('00', what_to_enter) > 0) then writeln('...')
else writeln(what_to_enter);
gotoXY(WhereX, WhereY+1);
end;
end;
procedure edit_note(var note: note_array; cursor_position: integer; var menu: string);
var s, s1: string;
i, j: integer;
error_code: integer;
begin
enter_note('Enter first name', note[cursor_position].name);
enter_note('Enter second name', note[cursor_position].sname);
enter_note('Enter telephone number', note[cursor_position].tel);
repeat
s := '';
for i := 1 to 3 do begin
str(note[cursor_position].birthday[i], s1);
if note[cursor_position].birthday[i] < 10 then s := s + '0' + s1
else s := s + s1;
if i < 3 then s := s + '.';
end;
enter_note('Enter birthday (DD.MM.YY)', s);
error_code := 0;
for i := 1 to 3 do begin
if error_code = 0 then begin
if i < 3 then begin
val(copy(s, 1, pos('.', s)-1), note[cursor_position].birthday[i], error_code);
if pos('.', s) = 0 then error_code := 1;
delete(s, 1, pos('.', s));
end
else begin
val(s, note[cursor_position].birthday[i], error_code);
end;
end;
end;
if (error_code <> 0) and (note[cursor_position].birthday[1]>31)
then begin
TextColor(red);
writeln('Wrong birthday. Please, enter again (in format DD.MM.YY).');
TextColor(white);
end;
until error_code = 0;
writeln;
TextColor(yellow);
writeln('Press BACKSPACE to return to menu. Press ENTER to re-edit.');
TextColor(white);
end;
procedure convert(note: note_array);
var i: integer;
begin
for i := 1 to max_notes_number do begin
if note[i].name = '' then note[i].name := '...';
if note[i].sname = '' then note[i].sname := '...';
end;
end;
procedure draw_window(x, y: integer);
begin
Window(x+4, y+1, 79, 24);
TextBackground(black);
TextColor(white);
clrscr;
end;
procedure draw_menu(menu: string; var note: note_array; cursor_position: integer);
var i, j: integer;
filename: string;
month: integer;
begin
draw_window(1, 1);
if menu = 'menu' then begin
draw_window(3, 1);
TextColor(yellow);
writeln('You are in main menu.');
writeln('Press ARROWS to move. Press ENTER to choose.');
draw_window(1, 5);
TextColor(white);
for i := 1 to menu_number do begin
if cursor_position = i then writeln('> ')
else writeln;
end;
draw_window(3, 5);
writeln('Edit');
writeln('Search by month');
writeln('Load');
writeln('Save');
writeln('Exit');
end;
if menu = 'notes' then begin
draw_window(3, 1);
TextColor(yellow);
writeln('Press ENTER to edit. Press BACKSPACE to quit.');
draw_window(1, 5);
TextColor(white);
for i := 1 to max_notes_number do begin
if cursor_position = i then writeln('> ')
else writeln;
end;
draw_window(3, 3);
TextColor(yellow);
writeln('First name');
writeln;
TextColor(white);
for i := 1 to max_notes_number do begin
writeln(note[i].name);
end;
draw_window(24, 3);
TextColor(yellow);
writeln('Second name');
writeln;
TextColor(white);
for i := 1 to max_notes_number do begin
writeln(note[i].sname);
end;
draw_window(45, 3);
TextColor(yellow);
writeln('Telephone number');
writeln;
TextColor(white);
for i := 1 to max_notes_number do begin
writeln(note[i].tel);
end;
draw_window(65, 3);
TextColor(yellow);
writeln('Birthday');
writeln;
TextColor(white);
for i := 1 to max_notes_number do begin
writeln(get_birthday(note, i));
end;
end;
if menu = 'search_by_month' then begin
draw_window(4, 1);
TextColor(yellow);
writeln('Write month and press ENTER to find.');
writeln;
TextColor(white);
readln(month);
draw_window(4, 5);
TextColor(yellow);
writeln('First name');
writeln;
TextColor(white);
for i := 1 to max_notes_number do begin
if note[i].birthday[2] = month then writeln(note[i].name);
end;
draw_window(24, 5);
TextColor(yellow);
writeln('Second name');
writeln;
TextColor(white);
for i := 1 to max_notes_number do begin
if note[i].birthday[2] = month then writeln(note[i].sname);
end;
draw_window(45, 5);
TextColor(yellow);
writeln('Telephone number');
writeln;
TextColor(white);
for i := 1 to max_notes_number do begin
if note[i].birthday[2] = month then writeln(note[i].tel);
end;
draw_window(66, 5);
TextColor(yellow);
writeln('Birthday');
writeln;
TextColor(white);
for i := 1 to max_notes_number do begin
if note[i].birthday[2] = month then writeln(get_birthday(note, i));
end;
draw_window(4, max_notes_number + 14);
TextColor(yellow);
writeln('Press BACKSPACE to return to menu. Press ENTER to re-search.');
end;
if menu = 'edit_note' then begin
draw_window(4, 1);
TextColor(yellow);
writeln('Write and press ENTER to edit note''s field.');
writeln('write nothing and press ENTER to leave the field unchanged');
writeln;
edit_note(note, cursor_position, menu);
end;
if menu = 'load' then begin
draw_window(4, 1);
TextColor(yellow);
writeln('Enter filename to load.');
draw_window(4, 3);
TextColor(white);
readln(filename);
writeln;
TextColor(yellow);
if filename = '' then begin
writeln('Press BACKSPACE to return to menu. Press ENTER to re-enter filename.');
end
else begin
load(filename, note);
writeln('Loaded successfully.');
writeln('Press BACKSPACE to return to menu. Press ENTER to re-enter filename.');
end;
end;
if menu = 'save' then begin
draw_window(4, 1);
TextColor(yellow);
writeln('Enter filename to save.');
draw_window(4, 3);
TextColor(white);
readln(filename);
writeln;
TextColor(yellow);
if filename = '' then begin
writeln('Press BACKSPACE to return to menu. Press ENTER to re-enter filename.');
end
else begin
save(filename, note);
writeln('Saved successfully.');
writeln('Press BACKSPACE to return to menu. Press ENTER to re-enter filename.');
end;
end;
end;
function action(key: char; var note: note_array; var cursor_position: integer; var menu: string): boolean;
var i, j: integer;
s, s1: string;
error_code: integer;
begin
action := false;
case key of
key_down: begin
if menu = 'menu' then begin
inc(cursor_position);
if cursor_position > menu_number then cursor_position := 1;
end;
if menu = 'notes' then begin
inc(cursor_position);
if cursor_position > max_notes_number then cursor_position := 1;
end;
end;
key_up: begin
if menu = 'menu' then begin
dec(cursor_position);
if cursor_position = 0 then cursor_position := menu_number;
end;
if menu = 'notes' then begin
dec(cursor_position);
if cursor_position = 0 then cursor_position := max_notes_number;
end;
end;
key_enter: begin
if menu = 'menu' then begin
case cursor_position of
1: begin
menu := 'notes';
convert(note);
end;
2: menu := 'search_by_month';
3: menu := 'load';
4: menu := 'save';
5: action := true;
end;
exit;
end;
if menu = 'notes' then begin
menu := 'edit_note';
exit;
end;
end;
key_backspace: begin
if menu = 'menu' then action := true;
if menu = 'notes' then menu := 'menu';
if menu = 'edit_note' then begin
menu := 'notes';
sort_notes(note);
end;
if menu = 'search_by_month' then menu := 'menu';
if menu = 'load' then menu := 'menu';
if menu = 'save' then menu := 'menu';
cursor_position := 1;
end;
end;
end;
var note: note_array;
i, j, cursor_position: integer;
exit_program: boolean;
key: char;
menu: string;
begin
TextBackground(black);
TextColor(black);
clrscr;
menu := 'menu';
exit_program := false;
cursor_position := 1;
draw_menu(menu, note, cursor_position);
for i := 1 to max_notes_number do begin
note[i].name := empty;
note[i].sname := empty;
note[i].tel := empty;
for j := 1 to 3 do note[i].birthday[j] := 0;
end;
repeat
if keypressed then begin
key := readkey;
exit_program := action(key, note, cursor_position, menu);
draw_menu(menu, note, cursor_position);
end;
until exit_program = true;
end. |