📄 selsolutionsc5.txt
字号:
exit(EXIT_FAILURE);
}
ch1 = getc(f1);
ch2 = getc(f2);
while (ch1 != EOF || ch2 != EOF)
{
while (ch1 != EOF && ch1 != '\n') /* skipped after EOF reached */
{
putchar(ch1);
ch1 = getc(f1);
}
if (ch1 != EOF)
{
if (ch2 == EOF)
putchar('\n');
else
putchar(' ');
ch1 = getc(f1);
}
while (ch2 != EOF && ch2 != '\n') /* skipped after EOF reached */
{
putchar(ch2);
ch2 = getc(f2);
}
if (ch2 != EOF)
{
putchar('\n');
ch2 = getc(f2);
}
}
if (fclose(f1) != 0)
printf("Could not close file %s\n", argv[1]);
if (fclose(f2) != 0)
printf("Could not close file %s\n", argv[2]);
return 0;
}
PE 13-9
/* Programming Exercise 13-9 */
/* to simplify accounting, store one number and word per line */
#include <stdio.h>
#include <stdlib.h>
#define MAX 40
int main(void)
{
FILE *fp;
char words[MAX];
int wordct = 0;
if ((fp = fopen("wordy", "a+")) == NULL)
{
fprintf(stderr,"Can't open \"words\" file.\n");
exit(1);
}
/* determine current number of entries */
rewind(fp);
while (fgets(words, MAX - 1, fp) != NULL)
wordct++;
rewind(fp);
puts("Enter words to add to the file. Enter one word per line, and ");
puts("press the Enter key at the beginning of a line to terminate.");
while (gets(words) != NULL && words[0] != '\0')
fprintf(fp, "%d: %s\n", ++wordct, words);
puts("File contents:");
rewind(fp); /* go back to beginning of file */
while (fgets(words, MAX - 1, fp) != NULL)
fputs(words, stdout);
if (fclose(fp) != 0)
fprintf(stderr,"Error closing file\n");
return 0;
}
PE 13-11
/* Programming Exercise 13-11 */
#include <stdio.h>
#include <stdlib.h>
#include <console.h> /* Macintosh adjustment */
#define SLEN 256
const char *errmesg[] = {"Usage: %s string filename]\n",
"Can't open file %s\n" };
int main(int argc, char *argv[])
{
FILE *fp;
char line[SLEN];
argc = ccommand(&argv); /* Macintosh adjustment */
if (argc != 3)
{
fprintf(stderr, errmesg[0], argv[0]);
exit(EXIT_FAILURE);
}
if ((fp = fopen(argv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 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
/* Programming Exercise 13-12 */
#include <stdio.h>
#include <stdlib.h>
#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[40];
FILE * infile;
init(picOut, 'S');
printf("Enter name of file: ");
scanf("%s", fileName);
if ((infile = fopen(fileName, "r")) == NULL)
{
fprintf(stderr, "Could not open data file.\n");
exit(EXIT_FAILURE);
}
for (row = 0; row < ROWS; row++)
for (col = 0; col < COLS; col++)
fscanf(infile, "%d", &picIn[row][col]);
if (ferror(infile))
{
fprintf(stderr, "Error getting data from file.\n");
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
/* pe14-1.c */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct month {
char name[10];
char abbrev[4];
int days;
int monumb;
};
const struct month months[12] = {
{"January", "Jan", 31, 1},
{"February", "Feb", 28, 2},
{"March", "Mar", 31, 3},
{"April", "Apr", 30, 4},
{"May", "May", 31, 5},
{"June", "Jun", 30, 6},
{"July", "Jul", 31, 7},
{"August", "Aug", 31, 8},
{"September", "Sep", 30, 9},
{"October", "Oct", 31, 10},
{"November", "Nov", 30, 11},
{"December", "Dec", 31, 12}
};
int days(char * m);
int main(void)
{
char input[20];
int daytotal;
printf("Enter the name of a month: ");
while (gets(input) != NULL && input[0] != '\0')
{
daytotal = days(input);
if (daytotal > 0)
printf("There are %d days through %s.\n", daytotal, input);
else
printf("%s is not valid input.\n", input);
printf("Next month (empty line to quit): ");
}
puts("bye");
return 0;
}
int days(char * m)
{
int total = 0;
int mon_num = 0;
int i;
if (m[0] == '\0')
total = -1;
else
{
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 <stdio.h>
#include <string.h>
#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;
printf("Please enter the book title.\n");
printf("Press [enter] at the start of a line to stop.\n");
while (count < MAXBKS && gets(library[count].title) != NULL
&& library[count].title[0] != '\0')
{
printf("Now enter the author.\n");
gets(library[count].author);
printf("Now enter the value.\n");
scanf("%f", &library[count].value);
pbk[count] = &library[count];
count++;
while (getchar() != '\n')
continue; /* clear input line */
if (count < MAXBKS)
printf("Enter the next title.\n");
}
printf("Here is the list of your books:\n");
for (index = 0; index < count; index++)
printf("%s by %s: $%.2f\n", library[index].title,
library[index].author, library[index].value);
printf("Here is the list of your books sorted by title:\n");
sortt(pbk, count);
for (index = 0; index < count; index++)
printf("%s by %s: $%.2f\n", pbk[index]->title,
pbk[index]->author, pbk[index]->value);
sortv(pbk, count);
printf("Here is the list of your books sorted by value:\n");
for (index = 0; index < count; index++)
printf("%s by %s: $%.2f\n", pbk[index]->title,
pbk[index]->author, 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;
}
}
PE 14-5
/* pe14-5.c */
#include <stdio.h>
#include <string.h>
#define LEN 14
#define CSIZE 4
#define SCORES 3
struct name {
char first[LEN];
char last[LEN];
};
struct student {
struct name person;
float scores[SCORES];
float mean;
};
void get_scores(struct student ar[], int lim);
void find_means(struct student ar[], int lim);
void show_class(const struct student ar[], int lim);
void show_ave(const struct student ar[], int lim);
int main(void)
{
struct student class[CSIZE] ={
{ "Flip", "Snide"},
{ "Clare", "Voyans"},
{ "Bingo", "Higgs"},
{ "Fawn", "Hunter"}
};
get_scores(class, CSIZE);
find_means(class, CSIZE);
show_class(class, CSIZE);
show_ave(class, CSIZE);
return 0;
}
void get_scores(struct student ar[], int lim)
{
int i,j;
for (i = 0; i < lim; i++)
{
printf ("Please enter %d scores for %s %s:\n", SCORES,
ar[i].person.first, ar[i].person.last);
for (j = 0; j < SCORES; j++)
{
while (scanf("%f", &ar[i].scores[j]) != 1)
{
scanf("%*s");
puts("Please use numeric input.");
}
}
}
}
void find_means(struct student ar[], int lim)
{
int i, j;
float sum;
for (i = 0; i < lim; i++)
{
for (sum = 0, j = 0; j < SCORES; j++)
sum += ar[i].scores[j];
ar[i].mean = sum / SCORES;
}
}
void show_class(const struct student ar[], int lim)
{
int i, j;
char wholename[2*LEN];
for (i = 0; i < lim; i++)
{
strcpy(wholename, ar[i].person.first);
strcat(wholename, " ");
strcat(wholename, ar[i].person.last);
printf("%27s: ", wholename);
for (j = 0; j < SCORES; j++)
printf("%6.1f ", ar[i].scores[j]);
printf(" Average = %5.2f\n", ar[i].mean);
}
}
void show_ave (const struct student ar[], int lim)
{
int i, j;
float total;
printf("\n%27s: ", "QUIZ AVERAGES");
for (j = 0; j < SCORES; j++)
{
for (total = 0, i = 0; i < lim; i++)
total += ar[i].scores[j];
printf("%6.2f ", total / lim);
}
putchar('\n');
}
PE 14-7
/* pe14-7.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 10 /* maximum number of books */
#define CONTINUE 0
#define DONE 1
#define YES 1
#define NO 0
struct book { /* set up book template */
char title[MAXTITL];
char author[MAXAUTL];
float value;
int delete;
};
int getlet(const char * s);
int getbook(struct book * pb);
void update(struct book * item);
int main(void)
{
struct book library[MAXBKS]; /* array of structures */
int count = 0;
int deleted = 0;
int index, filecount, open;
FILE * pbooks;
int size = sizeof (struct book);
if ((pbooks = fopen("book.dat", "r")) != NULL)
{
while (count < MAXBKS && fread(&library[count], size,
1, pbooks) == 1)
{
if (count == 0)
puts("Current contents of book.dat:");
printf("%s by %s: $%.2f\n",library[count].title,
library[count].author, library[count].value);
printf("Do you wish to change or delete this entry?<y/n> ");
if (getlet("yn") == 'y')
{
printf("Enter c to change, d to delete entry: ");
if (getlet("cd") == 'd')
{
library[count].delete = YES;
deleted++;
puts("Entry marked for deletion.");
}
else
update(&library[count]);
}
count++;
}
fclose(pbooks);
}
filecount = count - deleted;
if (count == MAXBKS)
{
fputs("The book.dat file is full.", stderr);
exit(2);
}
puts("Please add new book titles.");
puts("Press [enter] at the start of a line to stop.");
open = 0;
while (filecount < MAXBKS)
{
if (filecount < count)
{
while (library[open].delete == NO)
open++;
if (getbook(&library[open]) == DONE)
break;
}
else if (getbook(&library[filecount]) == DONE)
break;
filecount++;
if (filecount < MAXBKS)
puts("Enter the next book title.");
}
puts("Here is the list of your books:");
for (index = 0; index < filecount; index++)
if (library[index].delete == NO)
printf("%s by %s: $%.2f\n",library[index].title,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -