/
pascal
504 lines (456 loc) · 19.4 KB
/
pascal
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
// Long sample Pascal Program referenced from
// http://sandbox.mc.edu/~bennet/cs404/doc/sched.pas
(**************************************************************************
*
* This program maintains a small work schedule covering the hours 8-6.
* It processes the following commands:
*
* sched employee startday endday starthour endhour
* The employee is added to the schedule in the range of days and
* hours indicated.
*
* clear startday endday starthour endhour
* The part of the schedule indicated is cleared any assignment
* of an employee during those hours is removed.
*
* unsched employee
* Remove the employee from all places in the schedule to which
* s/he has been assigned.
*
* print
* Print the schedule. Show a table with days along the top, and
* hours down the side, giving the scheduled employee in each
* position.
*
* total employee
* Print the total hours for which the employee is scheduled. If
* the employee does not appear in the table, the total is 0.
*
* quit
* Terminate the program.
**************************************************************************)
PROGRAM a1 (input,output);
USES dayio;
CONST
{ Open positions in the schedule. }
NotScheduled = ' ';
{ Max length of an employee name. }
EmployeeMaxLen = 8;
{ Hours in a day. }
FirstHour = 8;
LastHour = 17; { 5:00 PM in 24-hour time }
PastLastHour = 18; { One past, for while loops. }
{ How much room to allow for each day in the table. }
TableDayWidth = 9;
TYPE
{ The employee name type. }
EmployeeType = string[EmployeeMaxLen];
{ The type of the schedule ARRAY. }
{ HourType = FirstHour..LastHour; }
HourType = 8..17;
ScheduleType = ARRAY [HourType, DayType] OF EmployeeType;
{ HourScanType = FirstHour..PastLastHour; }
HourScanType = 8..18;
(***********************************************************************
* Procedure to read the next non-blank. It skips leading blanks, then
* reads the string up to the first blank or eoln.
***********************************************************************)
PROCEDURE ReadString(VAR Str: string);
VAR
Ch: char;
BEGIN
Ch := ' ';
WHILE (Ch = ' ') AND NOT eoln DO
read(Ch);
IF Ch = ' ' THEN
{ There is no command on this line. }
Str := ''
ELSE
BEGIN
{ Read the beast. }
Str := '';
WHILE (Ch <> ' ') AND NOT eoln DO
BEGIN
Str := Str + Ch;
read(Ch)
END;
IF Ch <> ' ' THEN
{ Command ended at eoln. }
Str := Str + Ch
END
END; { ReadString }
(***********************************************************************
* Procedure to read the arguments held in common by the sched
* clear commands. Returns them through the arguments. If there
* is some error, that is reported through the argument error.
* Precondition: Following the read pointer, the input contains
* two days of the week, then two integers. If all days are present and
* correct, the integers must be present and correct.
* Postcondition: If both strings are recognized day names,
* they are read, and the integers are read as well, and their values
* are loaded into StartDay, EndDay, StartHour, and EndHour, and Error
* is set to false. The hours are mapped to 24-hour clock time under
* the rule that hours less than 6 are PM, and others are AM. If a day
* is missing or not recognized, the rest of the input line is
* discarded, and Error is set to true. If there is extra information
* on the line, it is discared. The read pointer is left at the start
* of the following line.
***********************************************************************)
PROCEDURE ReadSchedClrArgs(
VAR StartDay, EndDay: DayType; { Input days. }
VAR StartHour, EndHour: HourType; { Input hour range. }
VAR Error: boolean); { Input error indicator.}
VAR
InputHour: integer; { Input hour value. }
{ Map time to 24-hours based on the AM/PM rules. }
FUNCTION MapTo24(Hour: integer): HourType;
CONST
{ AM/PM time cut-off. }
LastPM = 5;
BEGIN
IF Hour <= LastPM THEN
MapTo24 := Hour + 12
ELSE
MapTo24 := Hour
END;
BEGIN { ReadSchedClrArgs }
{ Read the days. }
ReadDay(input, StartDay);
ReadDay(input, EndDay);
{ See if they both worked. }
IF (StartDay <> BadDay) AND (EndDay <> BadDay) THEN
BEGIN
{ It worked. Read the hours. }
read(InputHour);
StartHour := MapTo24(InputHour);
read(InputHour);
EndHour := MapTo24(InputHour);
{ Report success }
Error := FALSE
END
ELSE
(* Something went wrong, seriously wrong. *)
Error := TRUE;
(* We're done with this line. *)
readln
END; { ReadSchedClrArgs }
{****************************************************************
* PROCEDURE to print headers of each day.
* Precondition: None.
* Postcondition: A header line with the days of the week has
* been printed. The
****************************************************************}
PROCEDURE WriteDaysHeader;
CONST
{ How many spaces to move over before printing days-of
the week header. }
DaysHeadMoveOver = 6;
{ How much room to assume is needed by each day string. }
AllowForDay = 3;
VAR
Day: DayType;
BEGIN
write(' ': DaysHeadMoveOver);
FOR Day := Sun TO Sat DO
BEGIN
write('[ ');
WriteDay(output, Day);
write(' ]', ' ': TableDayWidth - AllowForDay - 4)
END;
writeln
END; { WriteDaysHeader }
{****************************************************************
* Function that tells if a pending schedule is legal.
* Its arguments are those of sched, excluding the employee name.
* Precondition: FirstHour and LastHour are in range.
* Postcondition: If the indicated area of the schedule contains
* blanks in each entry, then return true, else false.
* Note: Schedule is sent by VAR for efficiency -- it is not
* changed.
****************************************************************}
FUNCTION SchedLegal(
VAR Schedule: ScheduleType; { Schedule to check. }
StartDay, EndDay: DayType; { Days in question. }
FirstHour, LastHour: { Hours in question. }
HourType): boolean;
VAR
ConflictFound: boolean; { Tell if one found. }
DayScan: DayType; { Go through the days. }
HourScan: HourScanType; { Go through the hours. }
BEGIN
{ Scan the days. }
DayScan := StartDay;
ConflictFound := FALSE;
REPEAT
{ For this day, scan the times. }
HourScan := FirstHour;
WHILE NOT ConflictFound AND
(HourScan <= LastHour) DO BEGIN
{ Conflict? }
ConflictFound :=
Schedule[HourScan, DayScan] <> NotScheduled;
{ Next one. }
HourScan := HourScan + 1
END;
{ Next Day. }
DayScan := succ(DayScan)
UNTIL ConflictFound or (DayScan > EndDay);
{ And the answer is.. }
SchedLegal := not ConflictFound
END; { SchedLegal }
{****************************************************************
* This takes care of most of the work of the clear and sched
* commands. Its arguments are those of sched, with blanks in
* Employee for the clear. It places this name in each indicated
* postion.
* Precondition: FirstHour and LastHour are in range.
* Postcondition: The area of the schedule is changed to show
* the indicated employee.
* Note: This will replace any old entry, so the sched command
* should call SchedLegal above to make sure the operation
* is legal before calling this routine.
****************************************************************}
PROCEDURE SetSchedPart(
VAR Schedule: ScheduleType; { Set me! Set me! }
Employee: EmployeeType; { Who gets to work. }
StartDay, EndDay: DayType; { Days in question. }
FirstHour, LastHour: { Hours in question. }
HourType);
VAR
DayScan: DayType; { Go through the days. }
HourScan: HourType; { Go through the hours. }
BEGIN
for DayScan := StartDay to EndDay do
for HourScan := FirstHour to LastHour do
Schedule[HourScan, DayScan] := Employee
END; { SetSchedPart }
{****************************************************************
* Perform the sched command.
* Precondition: The read pointer is followed by the arguments
* for the sched command.
* Postcondition: The arguments have been read and echoed, and the
* read pointer is on the next line. The sched command has been
* performed with appropriate messages.
* Note: DayMap is passed by VAR for efficiency -- it is not
* changed.
****************************************************************}
PROCEDURE DoSched(
VAR Schedule: ScheduleType); { Change this. }
VAR
Employee: EmployeeType; { Input employee name. }
StartDay, EndDay: DayType; { Input days. }
StartHour, EndHour: HourType; { Input hour range. }
Error: boolean; { Input error indicator.}
BEGIN
{ Read the employee name }
ReadString(Employee);
{ Read all the other arguments, and recieve error
indication. }
ReadSchedClrArgs(StartDay, EndDay, StartHour, EndHour, Error);
{ For errors, let 'em know. Otherwise, do it. }
IF Error THEN
writeln('*** Un-recognized day code. ',
'Command not performed. ***')
ELSE
{ See if the scheduling is legal. }
IF SchedLegal(Schedule, StartDay, EndDay,
StartHour, EndHour) THEN
BEGIN
{ Legal. Do it and admit it. }
SetSchedPart(Schedule, Employee,
StartDay, EndDay, StartHour, EndHour);
writeln('>>> ', Employee, ' scheduled. <<<')
END
ELSE
{ Not legal. }
writeln('*** Conflicts with existing schedule. ',
'Command not performed. ***')
END; { DoSched }
{****************************************************************
* Perform the clear command.
* Precondition: The read pointer is followed by the arguments
* for the clear command.
* Postcondition: The arguments have been read and echoed, and the
* read pointer is on the next line. The clear command has been
* performed with appropriate messages.
* Note: DayMap is passed by VAR for efficiency -- it is not
* changed.
****************************************************************}
PROCEDURE DoClear(
VAR Schedule: ScheduleType); { Change this. }
VAR
StartDay, EndDay: DayType; { Input days. }
StartHour, EndHour: HourType; { Input hour range. }
Error: boolean; { Input error indicator.}
BEGIN
{ Read the arguments, and recieve error indication. }
ReadSchedClrArgs(StartDay, EndDay, StartHour, EndHour, Error);
{ For errors, let 'em know. Otherwise, do it. }
IF Error THEN
writeln('*** Un-recognized day code. ',
'Command not performed. ***')
ELSE
BEGIN
SetSchedPart(Schedule, NotScheduled, StartDay, EndDay,
StartHour, EndHour);
writeln('>>> Clear performed. <<<');
END { DoClear }
END;
{****************************************************************
* Peform the unsched command.
* Precondition: The read pointer is followed by an employee
* name.
* Postcondition: The argument has been read and echoed, and the
* read pointer is on the next line. The employee read has been
* removed from Schedule.
****************************************************************}
PROCEDURE DoUnsched(
VAR Schedule: ScheduleType); { Remove from. }
VAR
Employee: EmployeeType; { To remove. }
Day: DayType; { Column scanner. }
Hour: integer; { Row scanner. }
Found: boolean; { Presence indicator }
BEGIN
{ Read the employee. }
readln(Employee);
{ Remove! Remove! }
Found := FALSE;
FOR Day := Sun TO Sat DO
FOR Hour := FirstHour TO LastHour DO
IF Schedule[Hour, Day] = Employee THEN
BEGIN
{ Remove. }
Schedule[Hour, Day] := NotScheduled;
{ Note. }
Found := TRUE
END;
{ Warn if not found. Else just state. }
IF Found THEN
write('>>> ', Employee, ' removed from schedule. <<<')
ELSE
write('>>> ', Employee,
' was not on the schedule. <<<')
END; { DoUnsched }
{****************************************************************
* Peform the print command.
* Precondition: None.
* Postcondition: Schedule has been printed to output.
****************************************************************}
PROCEDURE DoPrint(
VAR Schedule: ScheduleType); { Print me. }
VAR
Hour: HourType; { Hour scan. }
Day: DayType; { Day scan. }
{ Map from 24-hour time to 12-hour time. Arguments less than
13 are simply returned, arguments greater than 12 are
reduced by 12 and returned. }
FUNCTION Map24to12(HourType: HourType): integer;
BEGIN
IF Hour < 13 THEN
Map24to12 := Hour
ELSE
Map24to12 := Hour - 12
END;
BEGIN
readln;
WriteDaysHeader;
FOR Hour := FirstHour TO LastHour DO
BEGIN
write(Map24to12(Hour):2, ':00 ');
FOR Day := Sun TO Sat DO
write(Schedule[Hour, Day],
' ': TableDayWidth - length(Schedule[Hour, Day]));
writeln
END
END;
{****************************************************************
* Peform the total command.
* Precondition: The read pointer is followed by an employee
* name.
* Postcondition: The argument has been read and echoed, and the
* read pointer is on the next line. The total scheduled hours
* for the employee read has been printed.
****************************************************************}
PROCEDURE DoTotal(
VAR Schedule: ScheduleType); { The schedule. }
VAR
Employee: EmployeeType; { To remove. }
Day: DayType; { Column scanner. }
Hour: integer; { Row scanner. }
Total: integer; { Total intgers. }
BEGIN
{ Read the employee. }
readln(Employee);
{ Do the sum. }
Total := 0;
FOR Day := Sun TO Sat DO
FOR Hour := FirstHour TO LastHour DO
IF Schedule[Hour, Day] = Employee THEN
Total := Total + 1;
{ Write the total. }
writeln('>>> ', Employee,
' is scheduled for ', Total:1, ' hours. <<<<')
END; { DoTotal }
{*****************************************************************
* Main line.
*****************************************************************}
VAR
{ The schedule. }
Schedule: ScheduleType;
{ Main loop continue flag. }
KeepRunning: boolean;
{ Command input local to main. }
Command: string;
BEGIN
{ Clear the schedule. }
SetSchedPart(Schedule, NotScheduled, Sun, Sat, FirstHour, LastHour);
{ Do the commands. }
write('==> ');
ReadString(Command);
KeepRunning := TRUE;
WHILE KeepRunning DO
BEGIN
IF Command = 'sched' THEN
DoSched(Schedule)
ELSE IF Command = 'clear' THEN
DoClear(Schedule)
ELSE IF Command = 'unsched' THEN
DoUnsched(Schedule)
ELSE IF Command = 'print' THEN
DoPrint(Schedule)
ELSE IF Command = 'total' THEN
DoTotal(Schedule)
ELSE IF Command = 'quit' THEN
BEGIN
writeln;
writeln('>>> Program terminating. <<<');
KeepRunning := FALSE
END
ELSE
{ Command not recognized. }
BEGIN
readln;
writeln;
writeln('*** Command ', Command,
' not recognized. ***');
END;
{ Go to a new page for next'n. }
write('==> ');
ReadString(Command)
END
END.
-- Hex numbers
function UnicodeToISO_8859_9(Unicode: cardinal): integer;
begin
case Unicode of
0..255: Result:=Unicode;
$011E: Result:= $D0;
$0130: Result:= $DD;
$015E: Result:= $DE;
$011F: Result:= $F0;
$0131: Result:= $FD;
$015F: Result:= $FE;
else Result:=-1;
end;
end;