-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign6.pas
More file actions
executable file
·386 lines (347 loc) · 11.3 KB
/
assign6.pas
File metadata and controls
executable file
·386 lines (347 loc) · 11.3 KB
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
{ Melissa Mangos }
{ May 31, 2013 }
{ This program simulates bank data in a binary tree }
program BankData;
uses wincrt;
{ Assign record }
type
TClientPtr = ^TClient;
TClient = record
FirstName: string[15];
LastName : string[30];
Phone : string[12];
Savings, Chequings, GICs, Bonds, Mutual : real;
pLeft, pRight, pParent : TClientPtr;
end; { TStudent }
{ Global Variable Declarations }
var
pRoot : TClientPtr;
{ METHODS --------------------------------------------------------------------- }
{ Determines if child is to the left or right of given parent }
function LeftOrRight (pChild, pParent : TClientPtr) : string;
{ Variable Declarations }
var
direction : string;
begin
if (pChild^.LastName < pParent^.LastName) or
((pChild^.LastName = pParent^.LastName) and (pChild^.FirstName < pParent^.FirstName)) then
{ Child is to the left }
begin
direction := 'Left';
end
else if (pChild^.LastName > pParent^.LastName) or
((pChild^.LastName = pParent^.LastName) and (pChild^.FirstName > pParent^.FirstName)) then
{ Child is to the right }
begin
direction := 'Right';
end; { if }
{ Return direction }
LeftOrRight := direction;
end; { LeftOrRight }
{ Searches for a client }
function SearchClient (pCurrent, pSearch : TClientPtr) : TClientPtr;
{ Variable Declarations }
var
direction : string;
begin
{ Finds a client or the parent if client is no found }
if (pSearch^.LastName <> pCurrent^.LastName) or (pSearch^.FirstName <> pCurrent^.FirstName) then
begin
{ Find out if child is on parents left or right }
direction := LeftOrRight (pSearch, pCurrent);
if (direction = 'Left') then
begin
{ The fuction is called when not at the bottom of the list }
if (pCurrent^.pLeft <> nil) then
begin
pCurrent := SearchClient (pCurrent^.pLeft, pSearch);
end; { if }
end
else if (direction = 'Right') then
begin
{ The fuction is called when not at the bottom of the list }
if (pCurrent^.pRight <> nil) then
begin
pCurrent := SearchClient (pCurrent^.pRight, pSearch);
end; { if }
end; { if }
end; { if }
{ Returns client if found, parent if not found }
SearchClient := pCurrent;
end; { SearchClient }
{ Inserts a new client into the binary tree }
procedure InsertClient (var pNew : TClientPtr);
{ Variable Declarations }
var
pCurrent : TClientPtr;
direction : string;
begin
{ Initializes root to first node }
if (pRoot = nil) then
begin
pRoot := pNew;
pRoot^.pParent := nil;
pRoot^.pRight := nil;
pRoot^.pLeft := nil;
end
else
begin
{ Searches for a spot to put the new client }
pCurrent := SearchClient (pRoot, pNew);
{ If the client does not already exist }
if (pCurrent^.FirstName <> pNew^.FirstName) or (pCurrent^.LastName <> pNew^.LastName) then
begin
{ Decides if child is on parents left or right }
direction := LeftOrRight (pNew, pCurrent);
{ Places the client on the left }
if (direction = 'Left') then
begin
pCurrent^.pLeft := pNew;
end
{ Places the client on the right }
else if (direction = 'Right') then
begin
pCurrent^.pRight := pNew;
end;
{ Initializes the new client }
pNew^.pParent := pCurrent;
pNew^.pLeft := nil;
pNew^.pRight := nil;
end; { if }
end; { if }
end; { InsertClient }
{ Reads clients from text file into nodes }
procedure ReadFile;
{ Variable Declarations }
var
pNew : TClientPtr;
fo : text;
begin
{ Open text file }
assign (fo, 'clients.txt');
reset (fo);
{ Initialize an empty list }
pRoot := nil;
while (not (eof (fo))) do
begin
{ Create a new node by reading text file }
new (pNew);
readln (fo, pNew^.FirstName);
readln (fo, pNew^.LastName);
readln (fo, pNew^.Phone);
readln (fo, pNew^.Savings);
readln (fo, pNew^.Chequings);
readln (fo, pNew^.GICs);
readln (fo, pNew^.Bonds);
readln (fo, pNew^.Mutual);
{ Inserts the client from text file }
InsertClient (pNew);
end; { while }
end; { ReadFile }
{ Displays a given client's information }
procedure DisplayClient (pClient : TClientPtr);
begin
writeln ('');
writeln (pClient^.FirstName);
writeln (pClient^.LastName);
writeln (pClient^.Phone);
writeln ('Savings Account: ', pClient^.Savings : 8 : 2);
writeln ('Chequing Account: ', pClient^.Chequings : 8 : 2);
writeln ('G.I.C.s: ', pClient^.GICs : 8 : 2);
writeln ('Bonds: ', pClient^.Bonds : 8 : 2);
writeln ('Mutual Funds: ', pClient^.Mutual : 8 : 2);
end; { DisplayClient }
{ Deletes a given client }
procedure DeleteClient (var pClient : TClientPtr);
{ Variable Declarations }
var
pReplace : TClientPtr;
direction : string;
begin
{ Checks if client is on left or right of parent }
direction := LeftOrRight (pClient, pClient^.pParent);
if (pClient^.pLeft = nil) and (pClient^.pRight = nil) then
{ Leaf node }
begin
if (direction = 'Left') then
{ Left side of parent }
begin
pClient^.pParent^.pLeft := nil;
end
else if (direction = 'Right') then
{ Right side of parent }
begin
pClient^.pParent^.pRight := nil;
end; { if }
end
else if (pClient^.pLeft <> nil) and (pClient^.pRight = nil) then
{ One child on left }
begin
if (direction = 'Left') then
{ Left side of parent }
begin
{ Sets parents left to child }
pClient^.pParent^.pLeft := pClient^.pLeft;
end
else if (direction = 'Right') then
{ Right side of parent }
begin
{ Sets parents right to child }
pClient^.pParent^.pRight := pClient^.pLeft;
end; { if }
{ Sets child's parent to client' s parent }
pClient^.pLeft^.pParent := pClient^.pParent;
end
else if (pClient^.pRight <> nil) and (pClient^.pLeft = nil) then
{ One child on right }
begin
if (direction = 'Left') then
{ Left side of parent }
begin
{ Sets parents left to child }
pClient^.pParent^.pLeft := pClient^.pRight;
end
else if (direction = 'Right') then
{ Right side of parent }
begin
{ Sets parents right to child }
pClient^.pParent^.pRight := pClient^.pRight;
end; { if }
{ Sets child's parent to client's parent }
pClient^.pRight^.pParent := pClient^.pParent;
end
else if (pClient^.pLeft <> nil) and (pClient^.pRight <> nil) then
{ Two children }
begin
{ Replaces node with next alphabetically }
pReplace := pClient^.pRight;
while (pReplace^.pLeft <> nil) do
begin
pReplace := pReplace^.pLeft;
end; { while }
{ Remove the replacement from its original spot }
if (pReplace^.pLeft = nil) and (pReplace^.pRight = nil) then
{ Leaf node }
begin
pReplace^.pParent^.pLeft := nil;
end
else if (pReplace^.pLeft = nil) then
{ Child on the right }
begin
pReplace^.pParent^.pLeft := pReplace^.pRight;
pReplace^.pRight^.pParent := pReplace^.pParent;
end
else if (pReplace^.pRight = nil) then
{ Child on the left }
begin
pReplace^.pParent^.pLeft := pReplace^.pLeft;
pReplace^.pLeft^.pParent := pReplace^.pParent;
end; { if }
{ Set the replacement to equal the deleted node }
if (direction = 'Left') then
{ Left side of parent }
begin
pClient^.pParent^.pLeft := pReplace;
end
else if (direction = 'Right') then
{ Right side of parent }
begin
pClient^.pParent^.pRight := pReplace;
end; { if }
{ Initializes Replacement }
pReplace^.pParent := pClient^.pParent;
pReplace^.pLeft := pClient^.pLeft;
pReplace^.pRight := pClient^.pRight;
end; { if }
{ Dispose node }
dispose (pClient);
end; { DeleteClient }
{ Displays the clients in alphabetical order }
procedure AlphaClient (pClient : TClientPtr);
begin
if (pClient^.pLeft <> nil) then
{ Displays on the left using recursion }
begin
AlphaClient (pClient^.pLeft);
end; { if }
{ Display Client }
write (pClient^.FirstName, ' ', pClient^.LastName, ' ');
if (pClient^.pRight <> nil) then
{ Displays on right using recursion }
begin
AlphaClient (pClient^.pRight);
end; { if }
end;
{ MAIN PROGRAM ---------------------------------------------------------------- }
{ Variable Decalrations }
var
pSearch, pClient, pNew : TClientPtr;
answer, alpha : string;
begin
{ Reads Text file and calls input and search functions }
ReadFile;
{ Creates a new node to be searched }
new (pSearch);
while (alpha <> 'Yes') do
begin
{ Inputs a person to search }
write ('Enter the client''s first name: ');
readln (pSearch^.FirstName);
write('Enter the client''s last name: ');
readln (pSearch^.LastName);
{ Searches for the person }
pClient := SearchClient (pRoot, pSearch);
if (pClient^.FirstName = pSearch^.FirstName) and (pClient^.LastName = pSearch^.LastName) then
{ If found }
begin
{ Displays the person's information }
DisplayClient (pClient);
{ Asks to delete client }
writeln ('');
write ('Would you like to delete the client (Yes or No)? ');
readln (answer);
if (answer = 'Yes') then
begin
{ Deletes the client }
DeleteClient (pClient);
end;
end
else { Client is not found }
begin
{ Asks to insert a new Client }
writeln ('');
write ('Would you like to insert a client (Yes or No)? ');
readln (answer);
if (answer = 'Yes') then
begin
{ Gets new clients information }
new (pNew);
pNew^.FirstName := pSearch^.FirstName;
pNew^.LastName := pSearch^.LastName;
write ('Phone number? ');
readln (pNew^.Phone);
write ('Savings account? ');
readln (pNew^.Savings);
write ('Chequing account? ');
readln (pNew^.Chequings);
write ('G.I.C.s? ');
readln (pNew^.GICs);
write ('Bonds? ');
readln (pNew^.Bonds);
write ('Mutual Funds? ');
readln (pNew^.Mutual);
{ Inserts client into binary tree }
InsertClient (pNew);
{ Displays new client's information }
DisplayClient (pNew);
end;
end; { if }
{ Asks for alphabetical order (exits the loop) }
writeln ('');
write ('Would you like to see the clients alphabetically (Yes to exit)? ');
readln (alpha);
writeln ('');
end;
{ Displays clients in alphbetical order }
AlphaClient (pRoot);
end. { program }