C Primer Plus Sixth Edition Programming Exercise
Selected Answers
PE 14---8
/* pe14-8.c */
#include
{ int seat_id; int status; char last[LEN]; char first[LEN];
}; int getmenu(void);
int getlet(const char *);
int openings(const struct planestats [], int); void show_empties(const struct planestats [], int); void list_assign(struct planestats *[], int); void assign_seat(struct planestats [], int); void delete_seat(struct planestats [], int); void
show_seats(const struct planestats [], int); void
sort(struct planestats *[], int); void makelist(const struct planestats [], char *, int); char * s_gets(char * st, int n);
int main(void) {
struct planestats plane_1[SEATS], *ps[SEATS]; int choice; int i; FILE *fp;
size_t size = sizeof(struct planestats);
for ( i = 0; i < SEATS; i++)
ps[i] = &plane_1[i]; if ((fp = fopen(\(i = 0; i < SEATS; i++) {
plane_1[i].status = EMPTY; plane_1[i].seat_id = i + 1; } else {
fread(plane_1, size, SEATS, fp); fclose(fp); } while ( (choice = getmenu() ) != 'q') {
switch (choice) {
case 'o': printf (\openings(plane_1, SEATS)); break;
case 'e': show_empties(plane_1, SEATS); break; case 'l': list_assign(ps, SEATS); break;
case 'a': assign_seat(plane_1, SEATS); break;
case 'd': delete_seat(plane_1, SEATS); break; default : puts(\trouble\
46
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
} } if((fp = fopen(\\to file.\
fwrite(plane_1, size, SEATS, fp); fclose(fp); }
puts(\return 0; }
#define CHOICES 6 int getmenu(void)
{ const char *descript[CHOICES] = { \seats\
\
\ \ \
\
const char labels[CHOICES + 1] = \int i;
puts(\for (i = 0; i < CHOICES; i++) printf(\labels[i], descript[i]); return getlet(labels); }
int getlet(const char * s) {
char c; c = getchar();
while (strchr(s, c) == NULL)
{ printf (\
list %s\\n\continue; c = getchar(); }
while (getchar() != '\\n') continue; return c; }
int openings(const struct planestats pl[], int n) { int count = 0; int seat; for (seat = 0; seat < n; seat++) if (pl[seat].status == EMPTY) count++; return count; }
void show_empties(const struct planestats pl[], int n) {
char seating[3* SEATS];
if ( openings(pl,n) == 0) puts(\else {
puts(\makelist(pl, seating, EMPTY); puts (seating) ; } }
void makelist(const struct planestats pl[], char * str, int kind) { int seat; char temp[LEN];
47
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
str[0] = '\\0';
for (seat = 0; seat < SEATS; seat++) if (pl[seat].status == kind) {
sprintf(temp,\strcat(str, temp); } }
void list_assign(struct planestats *ps[], int n) { int i;
if (openings(*ps, n) == SEATS) puts(\{ sort(ps, n); for(i = 0; i < SEATS; i++) if
( ps[i]->status == TAKEN ) printf (\
ps[i]->seat_id, ps[i]->last, ps[i]->first); }
} void assign_seat(struct planestats pl[], int n)
{ char list[3 *
SEATS]; int seat, loop;
if (openings(pl,n) == 0) puts(\else {
makelist(pl,list, EMPTY);
puts(\puts (list) ; do {
while( scanf(\
{ scanf(\);
puts(\puts (list) ; }
if (seat < 1 || seat > SEATS || pl[seat-1].status == TAKEN) {
puts(\puts (list) ; loop = CONTINUE;
} else loop = DONE; } while (loop == CONTINUE); while
(getchar() != '\\n') continue; puts(\first name:\puts(\LEN); printf(\pl[seat - 1].first, pl[seat - 1].last, seat);
puts(\if (getlet(\ {
pl[seat - 1].status = TAKEN; puts(\ } else
puts(\ } }
48
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
void delete_seat(struct planestats pl[], int n) { int seat, loop; char list[3 * SEATS];
if (openings(pl, n) == SEATS) puts(\
else { show_seats(pl, n); makelist(pl, list, TAKEN);
puts(\do {
while( scanf(\
{ scanf(\);
puts(\puts (list) ; }
if (seat < 1 || seat > SEATS || pl[seat-1].status == EMPTY) {
puts(\puts (list) ; loop = CONTINUE; } else loop = DONE; } while (loop == CONTINUE); while
(getchar() != '\\n') continue;
printf(\pl[seat - 1].first, pl[seat - 1].last, seat);
puts(\( getlet(\ {
pl[seat - 1].status = EMPTY; puts (\ } else
puts(\ } }
void show_seats(const struct planestats pl[], int n) { int i; puts(\currently taken:\i < SEATS; i++) if (pl[i].status == TAKEN)
printf(\pl[i].last, pl[i].first); }
void sort(struct planestats *array[], int limit) { int top, search; struct planestats * temp;
for (top = 0; top < limit -1; top++) for (search = top + 1; search < limit; search++) if
(strcmp(array[search]->last, array[top]->last) < 0) {
temp = array[search]; array[search] = array[top]; array[top] = temp; } }
49
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
char * s_gets(char * st, int n) { char *
ret_val; char * find; ret_val = fgets(st, n, stdin); if (ret_val)
{ find = strchr(st, '\\n'); // look for newline if (find) // if the address is not NULL, *find = '\\0'; // place a null character there else
while (getchar() != '\\n')
continue; // dispose of rest of line }
return ret_val; }
PE 14---10
/* pe14-10.c */
/* the tricky part is declaring an array of pointers to functions */ #include
#include
double (*pf[NUM])(double) = {twice, half, thrice, sqrt}; double val; double ans; int sel;
printf(\(scanf(\
{ showmenu();
while (scanf(\ {
ans = (*pf[sel])(val); // first notation printf(\
ans = pf[sel](val); // alternative notation printf(\ }
printf(\ } puts(\return 0; }
void showmenu(void)
{ puts(\double the value 1) halve the value\
triple the value 3) squareroot the value\next number\
} double twice(double x) {return 2.0 * x;} double half(double x) {return x /
2.0;} double thrice(double x) {return 3.0 * x;}
50