hall.zip contains the start of a program to
read seating data for a small concert hall. The following input (available
as in.1
)
1 $Jane 2 Paula 3 Eddie 4 $Shawna 5 $FriendlyGhost 6 BookItNowTravel 0 END 5 8 . . 2 - . . 1 1 3 3 6 6 6 6 6 6 - 1 . 4 . 2 . 4 . . . . . . . . - - 6 6 6 6 - - r 0,1,2 o 1,4 r 3,6,5first lists the patrons by id (1-6), terminated by the line
0
END
. Next it captures the currently reserved
seats. The 5 8 indicates the hall has 5 rows with 8 seating
positions in each row. This is followed by the information for each row where
$
, this indicates the patron is a donor.
The hall seating is followed by a sequence of commands reflecting actions at the ticket booth. The 'r' command reserves seats; r 0,1,3 means reserving seat 1 in row 0 for patron #3. Both seats and rows start at 0. The 'o' commands open seats back up; o 1,4 means to open seat 4 in row 1.
The initial program contains skeleton code for reading the original seating, including a structure to hold the data for the hall (including a list of patrons). You will extend the program to read and process the 'r' and 'o' commands and print the final seating setup. Running the program on the above input will generate
Original hall seating: . . 2 - . . 1 1 3 3 6 6 6 6 6 6 - 1 . 4 . 2 . 4 . . . . . . . . - - 6 6 6 6 - - Reserving row 0, seat 1 for Paula Releasing row 1, seat 4 for BookItNowTravel Reserving row 3, seat 6 for FriendlyGhost Final hall seating: . 2 2 - . . 1 1 3 3 6 6 . 6 6 6 - 1 . 4 . 2 . 4 . . . . . . 5 . - - 6 6 6 6 - - Seats for donors: row 0, seat 6 row 0, seat 7 row 2, seat 1 row 2, seat 3 row 2, seat 7 row 3, seat 6This output is available as
out.1
. Note
this program follows the model of generating output in a format that is
similar to the input. There are one or two blank spaces before each seat
including the first one on each row. The program also describes which seats
are being reserved and opened. In an effort to make the changes more
visible in this writeup, the seats that have changed status are marked
in bold text. Your solution will not use bold output. The donors
are listed by seat position starting at row 0 and seat 0. This list would
be used to place small gifts before the performance.
The provided code checks for several errors:
exit(0);
to do
this. (Often you would exit with a non-zero status, but esubmit will
report that as an error.)
Note that each error message ends with a period. Also note that only one
error message is printed for each command when there is a problem. You can
add additional checks, but they are not required.
Makefile
by typing
makeat the command prompt. Be sure to save all files before doing it! After it is built, to run your code on (say) in1.txt, type
a.exe in1.txtor
a.exe < in1.txt
scanf
format
" %s %d, %d\n"means skip any leading whitespace, read a string until a whitespace character is found, read a number, read a command, then another number, and finally the newline.
scanf
returns the number of
elements read.
int sum = 0, x; while ( !feof(stdin) ) { if ( scanf("%d", &x) == 1 ) sum += x; }
int this_seat_status = BLOCKED; ... if ( this_seat_status == OPEN ) ...illustrates using these values. The declaration
enum PatronStatus { GENERAL, DONOR, BLOCKED };effectively declares three constants with GENERAL having the value 0, DONOR having the value 1, and BLOCKED having the value 2. The code
enum { ROWS = 20 };creates a constant with the value 20; this is one way to get named constants in C.
scanf("%d %s", &int, status)
, then it
skips any leading whitespace. So then you can compare the string
against "-" or "." for the blocked or open cases. If the string is a
number, you can
use atoi
to convert it to an integer:
char num[] = "42"; int value = atoi(num);sets
value
to 42. If the string starts with some other
character, such as the % in one of the tests, atoi
will
return 0:
strcpy(num, "%1"); value = atoi(num); // sets value to 0Since there is no patron with id 0, you can use this return value to check for invalid seat characters.
tickets.c
as main and submit hall.h
and hall.c
as additional files.
If your instructor sets it up, you can receive up to 5 bonus points for this assignment by submitting a solution which can read the following data:
1 someone 0 END 3 3 . . . - - - . . .and displaying the concert hall with no donors. A full solution will also work on this, but you do not have to implement the full solution. Submit it to esubmit as mini6.