Hi i am a newbie pascal programmer and i havee writen a pascal program for dos.

I use this program to store dvd records for a video club, but it is bad and is need fixes.

Just like the search procedure.. Can you give me a new code for this ? Using the Handler method ? thanks

I want to give me a better code for this program and for its procedures.


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
program dvd_program; 
 
uses crt, DOS; 
 
const max=10; 
 
type cpointer =^node; 
      pointer =^treenode; 
      str20   = string[10]; 
 
 
   dvd=record 
     title      : str20; 
     datenb       : longint; 
   end; 
 
     dvdarray= array [1..max] of dvd; 
 
   node=record 
     name  : str20; 
     nb,fin: integer; 
     add   : str20; 
     dvds  : dvdarray; 
     link  : cpointer 
   end; 
 
     saved=record 
      name  : str20; 
      nb,fin: integer; 
      add   : str20; 
      dvds  : dvdarray; 
     end; 
 
     treenode=record 
       data,adr                   :str20; 
       left,right                 :pointer; 
       num,fin                    :integer; 
       dvds                       :dvdarray; 
 
     end; 
 
var head,pnew,this,before,beg     :cpointer; 
    target,i,nodenb,totaldays     :integer; 
    Month,Day,Year,DoW            :word; 
    Clientfile                    :file of saved; 
    found1, found2                :boolean; 
    choise                        :char; 
    client                        :saved; 
    p                             :pointer; 
 
 
procedure setwindow(ux,uy,lx,ly:byte;backgr,foregr:byte);      {gia xrwmatisto menu} 
begin 
     window(ux,uy,lx,ly); 
     textbackground(backgr); 
     textcolor(foregr); 
     clrscr 
end; 
 
procedure Get_Date(var year, month, day, dow: Word); 
                                                          {vriskei to date} 
begin 
     GetDate(year,month,day,dow); 
     Writeln('Today is ',day:0, '/', month:0, '/', year:0 ); 
     readln; 
end; 
 
 
procedure display_one(dis:cpointer); 
var found1 : boolean; 
    k:integer; 
begin 
     readln; 
     found1:=false; 
     found2:=false; 
     write('The Client With Number: '); 
     writeln(dis^.nb); 
     write(' Is Called: '); 
     writeln(dis^.name); 
     write('Lives In: '); 
     writeln(dis^.add); 
     if dis^.fin<>0 then 
      for k:=1 to dis^.fin do 
       if dis^.dvds[k].title<>'' then 
         begin 
             write  ('And Has Rented DVD: '); 
             writeln(dis^.dvds[k].title,' nb ',dis^.dvds[k].datenb); 
             found1:=true; 
         end 
     else writeln('The Client Hasnt Rented Any DVDs'); 
end; 
 
procedure display(head:cpointer); 
var dis:cpointer; 
    begin 
         clrscr; 
 
         dis:=head; 
         while dis<>nil do 
               begin 
                    display_one(dis); 
                    dis:=dis^.link; 
               end; 
         readln; 
    end; 
 
function search(head:cpointer; target:integer; VAR this,before:cpointer):boolean; 
 
var  found:boolean;             {psaxnei top linked list gia} 
                                  {ena sigekrimeno node (target)} 
    begin 
         clrscr; 
         found:=false; 
         this:=head; 
    If this=nil then 
       writeln('List is empty') 
    else 
        Repeat 
              If this^.nb=target then 
              found:=true 
        else 
            begin 
                 before:=this; 
                 this:=this^.link; 
            end; 
        until 
             (found=true) or (this=nil); 
        if found=true then 
           writeln('found') 
        else 
            writeln('not found'); 
            search:=found; 
 
end; 
 
 
procedure create(var head:cpointer; pnew:cpointer; var i:integer); 
 
    begin 
         clrscr;            {ftiaxnei apo tin arxh} 
         head:=nil; 
         i:=0; 
              repeat 
                    i:=i+1; 
                    new(pnew); 
                    pnew^.nb:=i; 
 
                                if (pnew^.name<> 's') and (pnew^.name<> 'S') then 
                                   begin 
                                        writeln; 
                                        writeln(' Give Clients Name:  '); 
                                        readln(pnew^.name); 
                                        writeln(' Give Clients Address:  '); 
                                        readln(pnew^.add); 
                                        pnew^.fin:=0; 
                                        writeln(' To Stop Press s'); 
                                        writeln; 
                                        Pnew^.link:=head; 
                                        head:=pnew; 
                                   end; 
              until (pnew^.name='s') or (pnew^.name='S'); 
              head:=head^.link; 
              i:=i-1                                    {just did it, dunno if it works} 
    end; 
 
procedure inorder(p:pointer); 
begin 
     if p <> nil then 
        begin 
             inorder(p^.left); 
             writeln(p^.data);       {vgainei alfavitika} 
             inorder(p^.right); 
        end; 
end; 
 
procedure treeinsert(var p:pointer; this:cpointer); 
begin 
     if p=nil then 
        begin                            {to vazw sto dentro gia na to kanw} 
             new(p); 
             p^.data:=this^.name; 
             p^.adr:=this^.add; 
             p^.dvds:=this^.dvds; 
             p^.fin:=this^.fin; 
             p^.num:=this^.nb; 
             p^.left:=nil; 
             p^.right:=nil; 
        end 
     else if this^.name < p^.data then 
          treeinsert(p^.left,this) 
          else if this^.name > p^.data then 
          treeinsert(p^.right,this); 
end; 
 
procedure add_mid(var head:cpointer; before:cpointer; this:cpointer; nb:integer); 
 
var pnew:cpointer; 
 
begin 
     new(pnew); 
     writeln(' Give Clients You Want To Add Name:  '); 
     readln(pnew^.name); 
     writeln(' Give Clients You Want To Add Address:  '); 
     readln(pnew^.add); 
     pnew^.fin:=0; 
     pnew^.nb:=nb; 
     before^.link:=pnew; 
     pnew^.link:=this; 
end; 
 
procedure add_by_nb; 
 
var     nb  :integer; 
        done:boolean; 
 
begin 
     this:=head; 
     done:=false; 
     nodenb:=0; 
               if this^.nb>1 then 
                  begin 
                       repeat 
                             before :=this; 
                             this :=this^.link; 
                                  if this^.nb < before^.nb-1 then 
                                     begin 
                                          nb:=before^.nb-1; 
                                          done:=true; 
                                          add_mid(head,before,this,nb); 
                                     end; 
                       until (this=nil) OR (this^.nb < before^.nb-1); 
                  end; 
               if done = false then 
                  begin 
                       writeln(head^.nb); 
                       readln; 
                       nb:=head^.nb+1; 
                       new(pnew); 
                       writeln(' Give Clients You Want To Add Name:  '); 
                       readln(pnew^.name); 
                       writeln(' Give Clients You Want To Add Address:  '); 
                       readln(pnew^.add); 
                       pnew^.nb:=nb; 
                       pnew^.link:=head; 
                       head:=pnew; 
                  end; 
end; 
 
 
 
procedure calculate( VAR totaldays:integer; year, month, day, dow: word); 
 
var m,days:integer; 
 
 
  begin 
    writeln(day); 
    totaldays:=0; 
    m:=0; 
    repeat 
          m:=m+1; 
               case m of 
                    1,3,5,7,8,10,12:  days:=31; 
                    4,6,9,11:         days:=30; 
                    2:                begin 
                                           if year mod 4=0 then 
                                              days:=28 
                                           else 
                                              days:=29; 
                                      end; 
                 end; 
          totaldays:=totaldays+days; 
          writeln(totaldays); 
    until m=month; 
          totaldays:=totaldays-days+day; 
          writeln(totaldays); 
  end; 
 
 
 
 
procedure rent_dvd(target,totaldays:integer; year, month, day, dow: word); 
var k:integer; 
begin 
     writeln('Which Client Wants To Rent A DVD? (Enter Clients Nb)  '); 
     readln(target); 
     if search(head,target,this,before)=true then 
        begin 
             if this^.fin=max then 
               begin 
                    writeln('You Already Have The Maximun DVD Possible'); 
                    writeln('press any key to continue...'); 
                    readkey; 
               end 
              else 
                  begin 
                       this^.fin:=this^.fin+1; 
                       write ('Which DVD to rent ?  '); 
                       readln(this^.dvds[this^.fin].title); 
                       this^.dvds[this^.fin].datenb:=totaldays; 
                  end 
 
          end 
     else writeln('Client Does Not Exist'); 
end; 
 
{procedure give_dvd(target:integer); 
var n:char; 
    c:char; 
 
begin 
     c:=n; 
     writeln('Which Client Wants To Give A DVD Back? (Enter Clients Nb)  '); 
     readln(target); 
     if search(head,target,this,before)=true then 
        begin 
              if this^.nbdvd=0 then 
               writeln('This Client hasnt rented any dvds') 
             else 
             if this^.nbdvd=1 then 
                begin 
                     writeln('Do you want to give back this DvD? (y/n) ',this^.dvd2); 
                     readln(c); 
                              if c = 'y' then 
                                 begin 
                                      this^.nbdvd:=0; 
                                      writeln('Job Done...'); 
                                 end 
                end 
             else 
                 if this^.nbdvd=2 then 
                    repeat 
                          begin 
                               writeln('You Have Rented Two dvds, '); 
                               writeln('If You Want To Give This dvd Back, ',this^.dvd1, 'press "a" '); 
                               writeln('If You Want To Give This dvd Back, ',this^.dvd2, 'press "b" '); 
                               readln(n); 
                          end; 
                    until n in ['a','A','b','B']; 
                    case n of 
                    'a','A': 
                            begin 
                                 this^.dvd1:=this^.dvd2; 
                                 this^.nbdvd:=1; 
                                 writeln('Job Done...'); 
                            end; 
                    'b','B': 
                            begin 
                                 this^.nbdvd:=1; 
                                 writeln('Job Done...'); 
                            end; 
                    end; 
        end 
     else writeln('Client Does Not Exist'); 
end; 
} 
 
 
 
procedure delete(var head:cpointer;var i:integer); 
 
var this,before:cpointer; 
 
begin 
     clrscr; 
     write('Delete Client with which number? (From Number 1 to ',i,' )');  {check it} 
     readln(target); 
     if search (head,target,this,before)=true then 
        begin 
             i:=i-1; 
             if head=this then 
                head:=head^.link 
             else 
                before^.link:=this^.link; 
                dispose(this); 
        end 
     else 
         writeln('Client Does Not Exist'); 
end; 
 
{    writeln(dis^.dvds[k].title,' nb ',dis^.dvds[k].datenb); 
procedure save(var head:cpointer); 
 
var loop:integer; 
 
begin 
     beg:=head; 
               begin 
                    assign(Clientfile,'client.dat'); 
                    rewrite(Clientfile); 
               end; 
     loop:=0; 
     while beg <> nil do 
           begin 
                loop:=loop+1; 
                client.name:=beg^.name; 
                client.add:=beg^.add; 
                client.nb:=beg^.nb; 
                for i:=1 to fin do 
                    client.dvds[i]:=beg^.dvds[i]; 
 
            client.nbdvd:=beg^.nbdvd; 
            seek(Clientfile,loop); 
            write(Clientfile,client); 
            beg:=beg^.link; 
         end; 
         close(Clientfile); 
end; 
 
procedure load(var head:cpointer); 
 
var loop:integer; 
 
begin 
     head:=nil; 
      assign(Clientfile,'client.dat'); 
      reset(Clientfile); 
 
      loop:=0; 
      while not eof(Clientfile) do 
      begin 
      loop:=loop+1; 
      seek(Clientfile,loop); 
      read(Clientfile,client); 
      new(beg); 
      beg^.name:=client.name; 
      beg^.add:=client.add; 
      beg^.nb:=client.nb; 
      beg^.dvd1:=client.dvd1; 
      beg^.dvd2:=client.dvd2; 
      beg^.nbdvd:=client.nbdvd; 
      beg^.link:=head; 
      head:=beg; 
    end; 
 
    end; 
 
 
    } 
begin 
     clrscr; 
     repeat 
      setwindow(1,1,80,3,4,30); 
      writeln('   Main Menu   ':44); 
      setwindow(1,4,80,25,0,7); 
      textcolor(9); 
     writeln('to create                  press ------> [c]':40); 
      textcolor(15); 
     writeln('to display                 press ------> [d]':40); 
      textcolor(5); 
     writeln('to search                  press ------> [s]':40); 
      textcolor(11); 
     writeln('to get the date            press ------> [g]':40); 
      textcolor(5); 
     writeln('to rent a DVD              press ------> [r]':40); 
      textcolor(8); 
     writeln('to give back a DVD         press ------> [b]':40); 
      textcolor(12); 
     writeln('to add an entry            press ------> [a]':40); 
      textcolor(13); 
     writeln('to erase                   press ------> [e]':40); 
      textcolor(1); 
     writeln('to save                    press ------> [i]':40); 
      textcolor(2); 
     writeln('to treeinsert              press ------> [t]':40); 
      textcolor(7); 
     writeln('to load                    press ------> [l]':40); 
      textcolor(9); 
     writeln('to exit                    press ------> [x]':40); 
     textcolor(15); 
     repeat 
           readln(choise); 
     until choise in ['r','R','c','C','d','D','s','S','k','K','g','G','i','I','t','T','l','L','e','E','x','X','a','A','b','B']; 
     case choise of 
 
          'c','C':create(head,pnew,i); 
          'd','D':display(head); 
          's','S': begin 
                        clrscr; 
                        write('Client number to search for ? '); 
                        readln(target); 
                        search(head,target,this,before); 
                        writeln('press any key to continue..'); 
                        readkey; 
                   end; 
          'r','R':  begin 
                         Get_date(year,month,day,dow); 
                         writeln(year,month,day); 
                         calculate(totaldays,year,month,day,dow); 
                         rent_dvd(target,totaldays,year,month,day,dow); 
                    end; 
          'g','G':Get_Date(year,month,day,dow); 
          'a','A':add_by_nb; 
          't','T':begin 
                     this:=head; 
                     p:=nil;                     while this<>nil do 
                           begin 
                               treeinsert(p,this); 
                               this:=this^.link; 
                           end; 
                     writeln('the inorder is: '); 
                     inorder(p); 
                     readln; 
                 end; 
         { 'i','I':save(head); 
          'l','L':load(head); 
          'b','B':give_dvd(target);} 
          'e','E':delete(head,i); 
 
     end; 
     until choise in ['x','X']; 
  readln; 
END.

Please help me thank you