{ Ben Weir - CIS 31 Assignment 7, Program 1 Last Modified: 11-25-96 } program a7p1(input, output); uses Crt; const MaxCols='D'; {how many columns of seats in plane} MaxRows=7; {How many rows of seats} QuitStr='q'; {enter this to quit} type cols= 'A'..MaxCols; {Total number of columns to make array} rows= 1..Maxrows; {total number of rows to make array} SeatType=array[rows, cols] of char;{keeps track of the status of each seat in the plane} OneSeat=string[2]; {The seat they choose to reserve} var Seats: SeatType; {All the seats on the plane} NoMore: boolean; {true if the user wants to quit early} Seat: OneSeat; {A1, B5, C2...seat user picks} row: integer; {row portion of that seat} col: char; {column portion of that seat} (*******************************************************************) Procedure InitSeats(var Seats: SeatType; MaxCols: char; MaxRows: integer); var i: integer; {for-do looping} j: char; {for-do looping} begin ClrScr; TextColor(15); GotoXY(15,1); Writeln('Welcome to the BenAir seat reservation assistant!'); TextColor(7); for i := 1 to MaxRows do begin for j := 'A' to MaxCols do begin Seats[i, j] := j {set value of each seat equal to its} end; {for j} {respective column} end; {for i} end; {procedure initSeats} (*******************************************************************) Procedure ShowSeats(Seats: SeatType); var i: integer; {for-do looping} j: char; {for-do looping} begin GotoXY(1,3); Writeln('Current Seating:'); writeln('Row| Column'); write('---+'); for j := 'A' to MaxCols do begin write('---'); {draw the heading} end; {for j} writeln; for i := 1 to MaxRows do begin write(' ', i, ' |'); for j := 'A' to MaxCols do begin if Seats[i, j]= 'X' then TextColor(5); Write(' ', Seats[i, j]); {show the status of each seat} TextColor(7); end; {for j} writeln; end; {for i} end; {procedure ShowSeats} (*******************************************************************) Function SeatIsValid(Seats: SeatType; var Col: char; var row: integer):boolean; {check if the seat they enter is OK} var code: integer; {for val procedure} begin SeatIsValid := True; Write(' '); gotoxy(1,MaxRows + 8); val(Seat[2], row, code); col := upcase(Seat[1]); if (col in ['A'..MaxCols]) and (row >= 1) and (row <= MaxRows) then begin {^^-- check if seat exists} if Seats[row, col] = 'X' then {<--check if it's taken or not} begin TextColor(132); writeln('Seat Already Taken!'); SeatIsValid := False; TextColor(7); end; end else if col <> Upcase(QuitStr) then {if the seat doesn't exist, make sure} begin {it's not because they pressed 'q' or} TextColor(140); writeln('Not a Valid Seat! '); {whatever it is to quit the program} SeatIsValid := False; TextColor(7) end; end; {function SeatIsValid} (*******************************************************************) Procedure GetSeat(var Seat: OneSeat); begin Seat := ' '; {initialize the seat they pick to a null string} repeat gotoxy(1, Maxrows + 7); {position the cursor} Write('Enter the seat you wish to reserve (i.e. A1 or C5) or ''', QuitStr, ''' to quit: '); Readln(Seat); until SeatIsValid(Seats, col, row); {loop until they enter a valid seat} GotoXY(67, Maxrows + 7); Write(' '); NoMore := col = upcase(QuitStr); {if they press 'q' or whatever to quit} end; {GetSeat} {then NoMore is true} (*******************************************************************) Procedure ReserveSeat(var Seats: SeatType); Begin Seats[row, col] := 'X'; {reserve the seat--set it to 'X'} end; {procedure reserveseat} (*******************************************************************) Function Full(Seats: SeatType; MaxCols: char; MaxRows: integer):boolean; var i: integer; {for-do looping} j: char; {for-do looping} begin Full := true; for i := 1 to MaxRows do begin for j := 'A' to MaxCols do begin if Seats[i,j] <> 'X' then Full := false {if it finds a seat not} end; {for j} {reserved then it must not} end; {for i} {be full} end; {function full} (*******************************************************************) begin {main program} InitSeats(Seats, Maxcols, MaxRows); repeat ShowSeats(Seats); GetSeat(Seat); if not NoMore then begin ReserveSeat(Seats); end; until Full(Seats, MaxCols, MaxRows) or NoMore; ShowSeats(Seats); gotoxy(1,MaxRows + 8); if Full(Seats, MaxCols, MaxRows) then writeln('The plane is full, no more reservations :('); Writeln('Goodbye!'); Write('-=Press a Key=-'); Readkey; end. {main program}