当前位置:首页 > C Primer Plus第6版编程练习答案(已下载)
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
case 'a': puts(\s_gets (copy.author, MAXAUTL); break;
case 'v : puts(\while (scanf(\ {
puts (\scanf(\
}
while( getchar() != '\\n') continue; break; }
puts(\puts(\puts(\
} if (c == 's')
item->book = copy;
} 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---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
45
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
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\
} } 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;
46
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
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]; 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)
47
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
{ 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(\ } }
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;
48
共分享92篇相关文档