好文档 - 专业文书写作范文服务资料分享网站

C Primer Plus第6版编程练习答案

天下 分享 时间: 加入收藏 我要投稿 点赞

discount = DISCOUNT_RATE * cost_total; else discount = ;

final_total = cost_total + shipping - discount; printf(\

printf(\lb_artichokes, price_artichokes, cost_artichokes);

printf(\lb_beets, price_beets, cost_beets); printf(\carrots at $%.2f per pound: $%.2f\\n\price_carrots, cost_carrots); printf(\

vegetables: $%.2f\\n\ printf(\printf(\

printf(\return 0; }

Chapter 8 Programming Exercises

PE 8--‐1

/* Programming Exercise 8-1 */ #include <>

int main(void) { int ch; int ct = 0; while ((ch = getchar()) != EOF) ct++;

printf(\ return 0; }

PE 8--‐3

/* Programming Exercise 8-3 */

/* Using eliminates need to assume consecutive coding */ #include <> #include <> int main(void) { int ch; unsigned long uct = 0; unsigned long lct = 0; unsigned long oct = 0;

while ((ch = getchar()) != EOF) if (isupper(ch)) uct++; else if (islower(ch)) lct++; else oct++;

printf(\

printf(\other characters read\\n\ return 0; }

/* or you could use if

(ch >= 'A' && ch <= 'Z') uct++;

else if (ch >= 'a' && ch <= 'z') lct++; else oct++;

*/

PE 8--‐5

/* Programming Exercise 8-5 */

/* -- an improved number-guesser */

/* but relies upon truthful, correct responses */ #include <> #include <> int

main(void) { int high = 100; int low = 1; int guess = (high + low) / 2; char response;

printf(\

printf(\h if it is high, and with an l if it is low.\\n\number %d?\\n\

while ((response = getchar()) != 'y') /* get response */ {

if (response == '\\n') continue;

if (response != 'h' && response != 'l') {

printf(\printf(\ }

if (response == 'h') high = guess - 1; else if (response == 'l') low = guess + 1; guess = (high + low) / 2;

printf(\ }

printf(\return 0; }

PE 8--‐7

/* Programming Exercise 8-7 */ #include <> #include <> #include <>

#define BASEPAY1 */ switch (response) {

case 'a': pay = BASEPAY1; break; case 'b': pay = BASEPAY2; break; case 'c': pay = BASEPAY3; break; case 'd': pay = BASEPAY4; break;

default : printf(\menu();

continue; f; taxes: $%.2f; net: $%.2f\\n\gross, taxes, net); menu(); } printf(\ return 0; }

void menu(void) {

printf(\

\

printf(\ \

printf(\BASEPAY2);

printf(\BASEPAY4); printf(\

printf(\ \}

int getfirst(void) { int ch;

ch = getchar(); while (isspace(ch)) ch = getchar(); while (getchar() != '\\n') continue; return ch; }

Chapter 9 Programming Exercises

PE 9--‐1

/* Programming Exercise 9-1 */ #include <>

double min(double, double); int main(void) {

double x, y; printf(\numbers (q to quit): \(scanf(\

{ printf(\

min(x,y)); printf(\\ }

printf(\ return 0; }

double min(double a, double b) {

return a < b ? a : b; }

/* alternative implementation double min(double a, double b) { if (a < b) return a; else return b; } */

PE 9--‐3

/* Programming Exercise 9-3 */ #include <>

void chLineRow(char ch, int c, int r); int main(void)

{ char ch; int col, row;

printf(\while ( (ch = getchar()) != '#') { if (ch == '\\n') continue;

printf(\if (scanf(\chLineRow(ch, col, row);

printf(\ }

printf(\ return 0; }

n\ }

printf(\ return 0; }

void larger_of(double *p1, double *p2) { if (*p1 > *p2) *p2 = *p1; else *p1 = *p2; }

Enter q\&x, &n) == 2)

{ xpow = power(x,n); /* function call */ printf(\xpow); printf(\quit.\\n\

} printf(\bye!\\n\

} double power(double a, int b) /* function definition */

{ double pow = 1; int i; if (b == 0) { if (a == 0)

printf(\pow = ; } else if (a == 0) pow = ; else if (b > 0) for(i = 1; i <= b; i++) pow *= a; else /* b < 0 */ pow = / power(a, - b);

return pow; /* return the value of pow */ }

PE 9--‐10

/* Programming Exercise 9-10 */ #include <> void to_base_n(int x, int base); int

main(void) { int number; int b; int count; printf(\quit):\\n\1)

{ printf(\\&b))== 1

&& (b < 2 || b > 10)) {

printf(\ } if

(count != 1) break;

printf(\to_base_n(number, b); putchar('\\n');

printf(\ }

printf(\return 0; }

void to_base_n(int x, int base) /* recursive function */ { int r; r = x % base; if (x >= base) to_base_n(x / base, base); putchar('0' + r); return; }

Chapter 10

Programming Exercises

PE 10--‐1

/* Programming Exercise 10-1 */ #include <>

#define MONTHS 12 f inches.\\n\\n\AVERAGES:\\n\\n\

printf(\printf(\

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(\ }

printf(\return 0; }

PE 10--‐3

/* Programming Exercise 10-3 */ #include <>

#define LEN 10

int max_arr(const int ar[], int n); void show_arr(const int ar[], int n);

C Primer Plus第6版编程练习答案

discount=DISCOUNT_RATE*cost_total;elsediscount=;final_total=cost_total+shipping-discount;printf(\printf(\lb_artichokes,price_artichokes,cost_artic
推荐度:
点击下载文档文档为doc格式
13cub6euwq9s4tl8lgrm6o2vt5lzj600cs5
领取福利

微信扫码领取福利

微信扫码分享