📄 selsolutionsc5.txt
字号:
{
int r;
r = x % base;
if (x >= 2)
to_base_n(x / base, base);
putchar('0' + r);
return;
}
Chapter 10
PE 10-1
/* Programming Exercise 10-1 */
#include <stdio.h>
#define MONTHS 12 /* number of months in a year */
#define YRS 5 /* number of years of data */
int main(void)
{
/* initializing rainfall data for 1990 - 1994 */
const float rain[YRS][MONTHS] = {
{10.2, 8.1, 6.8, 4.2, 2.1, 1.8, 0.2, 0.3, 1.1, 2.3, 6.1, 7.4},
{9.2, 9.8, 4.4, 3.3, 2.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 5.2},
{6.6, 5.5, 3.8, 2.8, 1.6, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 4.2},
{4.3, 4.3, 4.3, 3.0, 2.0, 1.0, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6},
{8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.2}
};
int year, month;
float subtot, total;
printf(" YEAR RAINFALL (inches)\n");
for (year = 0, total = 0; year < YRS; year++)
{ /* for each year, sum rainfall for each month */
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += *(*(rain + year) + month);
printf("%5d %15.1f\n", 1990 + year, subtot);
total += subtot; /* total for all years */
}
printf("\nThe yearly average is %.1f inches.\n\n", total/YRS);
printf("MONTHLY AVERAGES:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
printf(" Nov Dec\n");
for (month = 0; month < MONTHS; month++)
{ /* for each month, sum rainfall over years */
for (year = 0, subtot =0; year < YRS; year++)
subtot += *(*(rain + year) + month);
printf("%4.1f ", subtot/YRS);
}
printf("\n");
return 0;
}
PE 10-3
/* Programming Exercise 10-3 */
#include <stdio.h>
#define LEN 10
int max_arr(const int ar[], int n);
void show_arr(const int ar[], int n);
int main(void)
{
int orig[LEN] = {1,2,3,4,12,6,7,8,9,10};
int max;
show_arr(orig, LEN);
max = max_arr(orig, LEN);
printf("%d = largest value\n", max);
return 0;
}
int max_arr(const int ar[], int n)
{
int i;
int max = ar[0];
/* don't use 0 as initial max value -- fails if all array values are neg */
for (i = 1; i < n; i++)
if (max < ar[i])
max = ar[i];
return max;
}
void show_arr(const int ar[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", ar[i]);
putchar('\n');
}
PE 10-5
/* Programming Exercise 10-5 */
#include <stdio.h>
#define LEN 10
float max_diff(const float ar[], int n);
void show_arr(const float ar[], int n);
int main(void)
{
float orig[LEN] = {1.1,2,3,4,12,6,7,8,9,10};
float max;
show_arr(orig, LEN);
max = max_diff(orig, LEN);
printf("%g = maximum difference\n", max);
return 0;
}
float max_diff(const float ar[], int n)
{
int i;
float max = ar[0];
float min = ar[0];
for (i = 1; i < n; i++)
{
if (max < ar[i])
max = ar[i];
else if (min > ar[i])
min = ar[i];
}
return max - min;
}
void show_arr(const float ar[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%g ", ar[i]);
putchar('\n');
}
PE 10-7
/* Programming Exercise 10-7 */
#include <stdio.h>
#define LEN1 7
#define LEN2 3
void copy_arr(int ar1[], const int ar2[], int n);
void show_arr(const int ar[], int n);
int main(void)
{
int orig[LEN1] = {1,2,3,4,5,6,7};
int copy[LEN2];
show_arr(orig, LEN1);
copy_arr(copy, orig + 2, LEN2);
show_arr(copy, LEN2);
return 0;
}
void copy_arr(int ar1[], const int ar2[], int n)
{
int i;
for (i = 0; i < n; i++)
ar1[i] = ar2[i];
}
void show_arr(const int ar[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", ar[i]);
putchar('\n');
}
PE 10-10
/* Programming Exercise 10-10 */
#include <stdio.h>
#define ROWS 3
#define COLS 5
void times2(int ar[][COLS], int r);
void showarr2(int ar[][COLS], int r);
int main(void)
{
int stuff[ROWS][COLS] = { {1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15}
};
showarr2(stuff, ROWS);
putchar('\n');
times2(stuff, ROWS);
showarr2(stuff, ROWS);
return 0;
}
void times2(int ar[][COLS], int r)
{
int row, col;
for (row = 0; row < r; row++)
for (col = 0; col < COLS; col++)
ar[row][col] *= 2;
}
void showarr2(int ar[][COLS], int r)
{
int row, col;
for (row = 0; row < r; row++)
{
for (col = 0; col < COLS; col++)
printf("%d ", ar[row][col]);
putchar('\n');
}
}
PE 10-13
/* Programming Exercise 10-13 */
#include <stdio.h>
#define ROWS 3
#define COLS 5
void store(double ar[], int n);
double average2d(int rows, int cols, double ar[rows][cols]);
double max2d(int rows, int cols, double ar[rows][cols]);
void showarr2(int rows, int cols, double ar[rows][cols]);
double average(const double ar[], int n);
int main(void)
{
double stuff[ROWS][COLS];
int row;
for (row = 0; row < ROWS; row++)
{
printf("Enter %d numbers for row %d\n", COLS, row + 1);
store(stuff[row], COLS);
}
printf("array contents:\n");
showarr2(ROWS, COLS, stuff);
for (row = 0; row < ROWS; row++)
printf("average value of row %d = %g\n", row + 1, average(stuff[row], COLS));
printf("average value of all rows = %g\n", average2d(ROWS, COLS, stuff));
printf("largest value = %g\n", max2d(ROWS, COLS, stuff));
printf("Bye!\n");
return 0;
}
void store(double ar[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("Enter value #%d: ", i + 1);
scanf("%lf", & ar[i]);
}
}
double average2d(int rows, int cols, double ar[rows][cols])
{
int r, c;
double sum = 0.0;
for (r = 0; r < rows; r++)
for (c = 0; c < cols; c++)
sum += ar[r][c];
if (rows * cols > 0)
return sum / (rows * cols);
else
return 0.0;
}
double max2d(int rows, int cols, double ar[rows][cols])
{
int r, c;
double max = ar[0][0];
for (r = 0; r < rows; r++)
for (c = 0; c < cols; c++)
if (max < ar[r][c])
max = ar[r][c];
return max;
}
void showarr2(int rows, int cols, double ar[rows][cols])
{
int row, col;
for (row = 0; row < rows; row++)
{
for (col = 0; col < cols; col++)
printf("%g ", ar[row][col]);
putchar('\n');
}
}
double average(const double ar[], int n)
{
int i;
double sum = 0.0;
for (i = 0; i < n; i++)
sum += ar[i];
if (n > 0)
return sum / n;
else
return 0.0;
}
Chapter 11
PE 11-1
/* Programming Exercise 11-1 */
#include <stdio.h>
#define LEN 10
char * getnchar(char * str, int n);
int main(void)
{
char input[LEN];
char *chk;
chk = getnchar(input, LEN - 1);
if (chk == NULL)
puts("Input failed.");
else
puts(input);
puts("Done.\n");
return 0;
}
char * getnchar(char * str, int n)
{
int i;
int ch;
for (i = 0; i < n; i++)
{
ch = getchar();
if (ch != EOF)
str[i] = ch;
else
break;
}
if (ch == EOF)
return NULL;
else
{
str[i] = '\0';
return str;
}
}
PE 11-3
/* Programming Exercise 11-3 */
#include <stdio.h>
#define LEN 80
char * getword(char * str);
int main(void)
{
char input[LEN];
char *chk;
while (getword(input) != NULL)
puts(input);
puts("Done.\n");
return 0;
}
#include <ctype.h>
char * getword(char * str)
{
int i;
int ch;
while ((ch = getchar()) != EOF && !isspace(ch))
*str++ = ch;
*str = '\0';
if (ch == EOF)
return NULL;
else
{
while (ch != '\n')
ch = getchar();
return str;
}
}
PE 11-5
/* Programming Exercise 11-5 */
#include <stdio.h>
#define LEN 80
int is_within(const char * str, char c);
int main(void)
{
char input[LEN];
char ch;
int found;;
printf("Enter a string: ");
while (gets(input) && input[0] != '\0')
{
printf("Enter a character: ");
ch = getchar();
while (getchar() != '\n')
continue;
found = is_within(input, ch);
if (found == 0)
printf("%c not found in string.\n", ch);
else
printf("%c found in string %s\n", ch, input);
printf("Next string: ");
}
puts("Done.\n");
return 0;
}
int is_within(const char * str, char ch)
{
while (*str != ch && *str != '\0')
str++;
return *str; /* = 0 if \0 reached, non-zero otherwise */
}
PE 11-7
/* Programming Exercise 11-7 */
#include <stdio.h>
#define LEN 20
char * string_in(const char * s1, const char * s2);
int main(void)
{
char orig[LEN] = "transportation";
char * find;
puts(orig);
find = string_in(orig, "port");
if (find)
puts(find);
else
puts("Not found");
find = string_in(orig, "part");
if (find)
puts(find);
else
puts("Not found");
return 0;
}
#include <string.h>
char * string_in(const char * s1, const char * s2)
{
int l2 = strlen(s2);
int tries; /* maximum number of comparisons */
int nomatch = 1; /* set to 0 if match is found */
tries = strlen(s1) + 1 - l2;
if (tries > 0)
while (( nomatch = strncmp(s1, s2, l2)) && tries--)
s1++;
if (nomatch)
return NULL;
else
return (char *) s1; /* cast const away */
}
PE 11-9
/* Programming Exercise 11-9 */
#include <stdio.h>
#define LEN 81
int drop_space(char * s);
int main(void)
{
char orig[LEN];
while (gets(orig) && orig[0] != '\0')
{
drop_space(orig);
puts(orig);
}
puts("Bye!");
return 0;
}
int drop_space(char * s)
{
int ct = 0;
char * pos;
while (*s) /* or while (*s != '\0') */
{
if (*s == ' ')
{
pos = s;
do
{
*pos = *(pos + 1);
pos++;
} while (*pos);
}
else
s++;
}
}
PE 11-11
/* pe11-11.c -- counts words and certain characters */
/* Programming Exercise 11-11 */
#include <stdio.h>
#include <ctype.h> // for isspace()
#include <stdbool.h> // for bool, true, false
int main(void)
{
char c; // read in character
int low_ct = 0; // number of lowercase characters
int up_ct = 0; // number of uppercase characters
int dig_ct = 0; // number of digits
int n_words = 0; // number of words
int punc_ct = 0; // number of punctuation marks
bool inword = false; // == true if c is in a word
printf("Enter text to be analyzed (EOF to terminate):\n");
while ((c = getchar()) != EOF)
{
if (islower(c))
low_ct++;
else if (isupper(c))
up_ct++;
else if (isdigit(c))
dig_ct++;
else if (ispunct(c))
punc_ct++;
if (!isspace(c) && !inword)
{
inword = true; // starting a new word
n_words++; // count word
}
if (isspace(c) && inword)
inword = false; // reached end of word
}
printf("\nwords = %d, lowercase = %d, uppercase = %d, "
"digits = %d, punctuation = %d\n",
n_words,low_ct,up_ct, dig_ct, punc_ct);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -