C Primer Plus Sixth Edition Programming Exercise
Selected Answers
PE 13---9
/* Programming Exercise 13-9 */
/* to simplify accounting, stores one number and word per line */ #include
int main(void) {
FILE *fp; char words[MAX]; int wordct = 0;
if ((fp = fopen(\ {
fprintf(stderr,\exit(EXIT_FAILURE); }
// determine current number of lines rewind(fp);
while (fgets(words, MAX, fp) != NULL) wordct++; rewind(fp); puts(\
the #\while ((fscanf(stdin,\fprintf(fp, \contents:\
rewind(fp); // go back to beginning of file while (fgets(words, MAX, fp) != NULL) // read line including number fputs(words, stdout); if (fclose(fp) != 0)
fprintf(stderr,\puts(\
return 0; }
PE 13---11
/* Programming Exercise 13-11 */
#include
#define SLEN 256
const char *errmesg[] = {\ \
int main(int argc, char *argv[]) {
FILE *fp; char line[SLEN];
if (argc != 3) {
fprintf(stderr, errmesg[0], argv[0]); exit(EXIT_FAILURE); }
if ((fp = fopen(argv[2], \ {
36
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
fprintf(stderr, errmesg[1], argv[2]); exit(EXIT_FAILURE); }
while (fgets(line, SLEN, fp) != NULL)
{ if (strstr(line, argv[1]) != NULL) fputs(line, stdout); } fclose(fp); return 0; }
PE 13---12: Sample Input Text
0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 8 1 9 8 5 4 5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 0 4 5 2 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 4 5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 1 8 5 0 0 0 4 5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 4 5 2 0 0 0 0 0 5 5 5 5 5 5 5 5 5 5 5 5 5 8 9 9 8 5 5 5 5 5 5 5 5 5 5 5 5 5 8 8 8 8 8 8 8 8 8 8 8 8 5 8 9 9 8 5 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 3 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 8 8 8 8 5 8 9 9 8 5 8 8 8 8 8 8 8 8 8 8 8 8 5 5 5 5 5 5 5 5 5 5 5 5 5 8 9 9 8 5 5 5 5 5 5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 6 6 0 0 0 0 0 0 0 0 0 0 2 2 0 0 0 0 0 0 5 8 9 9 8 5 0 0 5 6 0 0 6 5 0 0 0 0 0 0 0 0 3 3 0 0 0 0 0 0 5 8 9 9 8 5 0 5 6 1 1 1 1 6 5 0 0 0 0 0 0 0 4 4 0 0 0 0 0 0 5 8 9 9 8 5 0 0 5 6 0 0 6 5 0 0 0 0 0 0 0 0 5 5 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 6 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
PE 13---12
/* Programming Exercise 13-12 */ #include
#define ROWS 20 #define COLS 30 #define LEVELS 10
const char trans[LEVELS + 1] = \
void MakePic(int data[][COLS], char pic[][COLS], int rows); void init(char arr[][COLS], char ch); int main() { int row, col; int
picIn[ROWS][COLS]; char picOut[ROWS][COLS]; char fileName[81];
FILE * infile; init(picOut, 'S');
printf(\file: \fileName);
37
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
if ((infile = fopen(fileName, \ {
fprintf(stderr, \exit(EXIT_FAILURE);
} for (row = 0; row < ROWS; row++) for (col = 0; col < COLS; col++) fscanf(infile, \ &picIn[row][col]); if (ferror(infile)) {
fprintf(stderr, \exit(EXIT_FAILURE); }
MakePic(picIn, picOut, ROWS); for (row = 0; row < ROWS; row++)
{ for (col = 0; col < COLS; col++)
putchar(picOut[row][col]); putchar('\\n'); } return 0; }
void init(char arr[][COLS], char ch) { int r, c; for (r = 0; r < ROWS; r++) for (c = 0; c < COLS; c++) arr[r][c] = ch; }
void MakePic(int data[][COLS], char pic[][COLS], int rows) { int row, col; for (row = 0; row < rows;
row++) for (col = 0; col < COLS; col++) pic[row][col] = trans[data[row][col]]; }
Chapter 14
PE 14---1
Programming Exercises
/* pe14-1.c */ #include
name[10]; char abbrev[4]; int days; int monumb;
}; const struct month months[12] = { {\1},
{\ {\ {\ {\ {\ {\ {\
38
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
{\ {\ {\ {\}; int days(char * m); int main(void) { char
input[20]; int daytotal;
printf(\(scanf(\ { daytotal = days(input); if (daytotal > 0)
printf(\else
printf(\printf(\ }
puts(\return 0; }
int days(char * m)
{ int total = 0; int
mon_num = 0; int i; m[0] = toupper(m[0]); for (i = 1; m[i] != '\\0'; i++) m[i] = tolower(m[i]); for (i = 0; i < 12; i++)
if (strcmp(m, months[i].name) == 0) {
mon_num = months[i].monumb; break; } if (mon_num == 0) total = -1; else
for (i = 0; i < mon_num; i++) total +=months[i].days;
return total; }
PE 14---3
/* pe14-3.c */
#include
char * s_gets(char * st, int n); #define MAXTITL 40 #define MAXAUTL 40
#define MAXBKS 100 /* maximum number of books */ struct book { /* set up book template */ char title[MAXTITL]; char author[MAXAUTL]; float value; };
void sortt(struct book * pb[], int n); void sortv(struct book * pb[], int n);
int main(void)
{ struct book library[MAXBKS]; /* array of book structures */ struct book * pbk[MAXBKS]; /* pointers for sorting */ int count = 0; int index;
39
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
printf(\
printf(\
while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\\0') {
printf(\s_gets(library[count].author, MAXAUTL); printf(\scanf(\
pbk[count] = &library[count]; count++; while (getchar() != '\\n')
continue; /* clear input line */ if (count < MAXBKS)
printf(\ }
printf(\(index = 0; index < count; index++) printf(\by %s: $%.2f\\n\library[index].author, library[index].value);
printf(\sortt(pbk, count);
for (index = 0; index < count; index++) printf(\pbk[index]->author, pbk[index]->value); sortv(pbk, count);
printf(\for (index = 0; index < count; index++) printf(\
by %s: $%.2f\\n\pbk[index]->value);
return 0; }
void sortt(struct book * pb[], int n) { int top, search; struct book * temp;
for (top = 0; top < n -1; top++)
for (search = top + 1; search < n; search++) if (strcmp(pb[search]->title, pb[top]->title) < 0) {
temp = pb[search]; pb[search] = pb[top]; pb[top] = temp;
} }
void sortv(struct book * pb[], int n) {
int top, search; struct book * temp;
for (top = 0; top < n -1; top++) for
(search = top + 1; search < n; search++) if (pb[search]->value < pb[top]->value) {
temp = pb[search]; pb[search] = pb[top]; pb[top] = temp;
}
40
(完整word版)CPrimerPlus第6版编程练习答案(已下载)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)