📄 selsolutionsc5.txt
字号:
else
{
sumeven += n;
++ct_even;
}
}
printf("Number of evens: %d", ct_even);
if (ct_even > 0)
printf(" average: %g", sumeven / ct_even);
putchar('\n');
printf("Number of odds: %d", ct_odd);
if (ct_odd > 0)
printf(" average: %g", sumodd / ct_odd);
putchar('\n');
printf("\ndone\n");
return 0;
}
PE 7-5
/* Programming Exercise 7-5 */
#include <stdio.h>
int main(void)
{
char ch;
int ct1 = 0;
int ct2 = 0;
while ((ch = getchar()) != '#')
{
switch(ch)
{
case '.' : putchar('!');
++ct1;
break;
case '!' : putchar('!');
putchar('!');
++ct2;
break;
default : putchar(ch);
}
}
printf("%d replacements of . with !\n", ct1);
printf("%d replacements of ! with !!\n", ct2);
return 0;
}
PE 7-7
/* Programming Exercise 7-7 */
#include <stdio.h>
#define BASEPAY 10 /* $10 per hour */
#define BASEHRS 40 /* hours at basepay */
#define OVERTIME 1.5 /* 1.5 time */
#define AMT1 300 /* 1st rate tier */
#define AMT2 150 /* 2st rate tier */
#define RATE1 0.15 /* rate for 1st tier */
#define RATE2 0.20 /* rate for 2nd tier */
#define RATE3 0.25 /* rate for 3rd tier */
int main(void)
{
double hours;
double gross;
double net;
double taxes;
printf("Enter the number of hours worked this week: ");
scanf("%lf", &hours);
if (hours <= BASEHRS)
gross = hours * BASEPAY;
else
gross = BASEHRS * BASEPAY + (hours - BASEHRS) * BASEPAY * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3;
net = gross - taxes;
printf("gross: $%.2f; taxes: $%.2f; net: $%.2f\n", gross, taxes, net);
return 0;
}
PE 7-9
/* Programmming Exercise 7-9 */
#include <stdio.h>
#define NO 0
#define YES 1
int main(void)
{
long num; /* value to be checked */
long div; /* potential divisors */
long lim; /* limit to values */
int prime;
printf("Please enter limit to values to be checked; ");
printf("Enter q to quit.\n");
while (scanf("%ld", &lim) == 1 && lim > 0)
{
for (num = 2; num <= lim; num++)
{
for (div = 2, prime = YES; (div * div) <= num; div++)
if (num % div == 0)
prime = NO; /* number is not prime */
if (prime == YES)
printf("%ld is prime.\n", num);
}
printf("Please enter another limit; ");
printf("Enter q to quit.\n");
}
return 0;
}
PE 7-11
/* pe7-11.c */
/* Programming Exercise 7-11 */
#include <stdio.h>
#include <ctype.h>
int main(void)
{
const double price_artichokes = 1.25;
const double price_beets = 0.65;
const double price_carrots = 0.89;
const double DISCOUNT_RATE = 0.05;
char ch;
double lb_artichokes;
double lb_beets;
double lb_carrots;
double lb_total;
double cost_artichokes;
double cost_beets;
double cost_carrots;
double cost_total;
double final_total;
double discount;
double shipping;
printf("Enter a to buy artichokes, b for beets, ");
printf("c for carrots, q to quit: ");
while ((ch = getchar()) != 'q' && ch != 'Q')
{
if (ch == '\n')
continue;
while (getchar() != '\n')
continue;
ch = tolower(ch);
switch (ch)
{
case 'a' : printf("Enter pounds of artichokes: ");
scanf("%lf", &lb_artichokes);
break;
case 'b' : printf("Enter pounds of beets: ");
scanf("%lf", &lb_beets);
break;
case 'c' : printf("Enter pounds of carrots: ");
scanf("%lf", &lb_carrots);
break;
default : printf("%c is not a valid choice.\n");
}
printf("Enter a to buy artichokes, b for beets, ");
printf("c for carrots, q to quit: ");
}
cost_artichokes = price_artichokes * lb_artichokes;
cost_beets = price_beets * lb_beets;
cost_carrots = price_carrots * lb_carrots;
cost_total = cost_artichokes + cost_beets + cost_carrots;
lb_total = lb_artichokes + lb_beets + lb_carrots;
if (lb_total <= 0)
shipping = 0.0;
else if (lb_total < 5.0)
shipping = 3.50;
else if (lb_total < 20)
shipping = 10.0;
else
shipping = 8.0 + 0.1 * lb_total;
if (cost_total > 100.0)
discount = DISCOUNT_RATE * cost_total;
else
discount = 0.0;
final_total = cost_total + shipping - discount;
printf("Your order:\n");
printf("%.2f lbs of artichokes at $%.2f per pound:$ %.2f\n",
lb_artichokes, price_artichokes, cost_artichokes);
printf("%.2f lbs of beets at $%.2f per pound: $%.2f\n",
lb_beets, price_beets, cost_beets);
printf("%.2f lbs of carrots at $%.2f per pound: $%.2f\n",
lb_carrots, price_carrots, cost_carrots);
printf("Total cost of vegetables: $%.2f\n", cost_total);
if (cost_total > 100)
printf("Volume discount: $%.2f\n", discount);
printf("Shipping: $%.2f\n", shipping);
printf("Total charges: $%.2f\n", final_total);
return 0;
}
Chapter 8
PE 8-1
/* Programming Exercise 8-1 */
#include <stdio.h>
int main(void)
{
int ch;
int ct = 0;
while ((ch = getchar()) != EOF)
ct++;
printf("%d characters read\n", ct);
return 0;
}
PE 8-3
/* Programming Exercise 8-3 */
/* Using ctype.h eliminates need to assume ASCII coding */
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
int uct = 0;
int lct = 0;
while ((ch = getchar()) != EOF)
if (isupper(ch))
uct++;
else if (islower(ch))
lct++;
printf("%d uppercase characters read\n", uct);
printf("%d lowercase characters read\n", lct);
return 0;
}
/*
or you could use
if (ch >= 'A' && ch <= 'Z')
uct++;
else if (ch >= 'a' && ch <= 'z')
lct++;
*/
PE 8-5
/* Programming Exercise 8-5 */
/* binaryguess.c -- an improved number-guesser */
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int high = 100;
int low = 1;
int guess = (high + low) / 2;
char response;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right, with");
printf("\na h if it is high, and with an l if it is low.\n");
printf("Uh...is your number %d?\n", guess);
while ((response = getchar()) != 'y') /* get response */
{
if (response == '\n')
continue;
if (response != 'h' && response != 'l')
{
printf("I don't understand that response. Please enter h for\n");
printf("high, l for low, or y for correct.\n");
continue;
}
if (response == 'h')
high = guess - 1;
else if (response == 'l')
low = guess + 1;
guess = (high + low) / 2;
printf("Well, then, is it %d?\n", guess);
}
printf("I knew I could do it!\n");
return 0;
}
PE 8-7
/* Programming Exercise 8-7 */
#include <stdio.h>
#include <ctype.h>
#define BASEPAY1 8.75 /* $8.75 per hour */
#define BASEPAY2 9.33 /* $9.33 per hour */
#define BASEPAY3 10.00 /* $10.00 per hour */
#define BASEPAY4 11.20 /* $11.20 per hour */
#define BASEHRS 40 /* hours at basepay */
#define OVERTIME 1.5 /* 1.5 time */
#define AMT1 300 /* 1st rate tier */
#define AMT2 150 /* 2st rate tier */
#define RATE1 0.15 /* rate for 1st tier */
#define RATE2 0.20 /* rate for 2nd tier */
#define RATE3 0.25 /* rate for 3rd tier */
int getfirst(void);
void menu(void);
int main(void)
{
double hours;
double gross;
double net;
double taxes;
double pay;
char response;
menu();
while ((response = getfirst()) != 'q')
{
if (response == '\n') /* skip over newlines */
continue;
response = tolower(response); /* accept A as a, etc. */
switch (response)
{
case 'a' : pay = BASEPAY1; break;
case 'b' : pay = BASEPAY2; break;
case 'c' : pay = BASEPAY3; break;
case 'd' : pay = BASEPAY4; break;
default : printf("Please enter a, b, c, d, or q.\n");
menu();
continue; /* go to beginning of loop */
}
printf("Enter the number of hours worked this week: ");
scanf("%lf", &hours);
if (hours <= BASEHRS)
gross = hours * pay;
else
gross = BASEHRS * pay + (hours - BASEHRS) * pay * OVERTIME;
if (gross <= AMT1)
taxes = gross * RATE1;
else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2;
else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) *
RATE3;
net = gross - taxes;
printf("gross: $%.2f; taxes: $%.2f; net: $%.2f\n", gross, taxes,
net);
menu();
}
printf("Done.\n");
return 0;
}
void menu(void)
{
printf("********************************************************"
"*********\n");
printf("Enter the number corresponding to the desired pay rate"
" or action:\n");
printf("a) $%4.2f/hr b) $%4.2f/hr\n", BASEPAY1,
BASEPAY2);
printf("c) $%5.2f/hr d) $%5.2f/hr\n", BASEPAY3,
BASEPAY4);
printf("q) quit\n");
printf("********************************************************"
"*********\n");
}
int getfirst(void)
{
int ch;
ch = getchar();
while (isspace(ch))
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
Chapter 9
PE 9-1
/* Programming Exercise 9-1 */
#include <stdio.h>
double min(double a, double b);
int main(void)
{
double x, y;
printf("Enter two numbers (q to quit): ");
while (scanf("%lf %lf", &x, &y) == 2)
{
printf("The smaller number is %f.\n", min(x,y));
printf("Next two values (q to quit): ");
}
printf("Bye!\n");
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 <stdio.h>
void chLineRow(char ch, int c, int r);
int main(void)
{
char ch;
int col, row;
printf("Enter a character (# to quit): ");
while ( (ch = getchar()) != '#')
{
if (ch == '\n')
continue;
printf("Enter number of columns and number of rows: ");
if (scanf("%d %d", &col, &row) != 2)
break;
chLineRow(ch, col, row);
printf("\nEnter next character (# to quit): ");
}
printf("Bye!\n");
return 0;
}
void chLineRow(char ch, int c, int r)
{
int col, row;
for (row = 0; row < r ; row++)
{
for (col = 0; col < c; col++)
putchar(ch);
putchar('\n');
}
return;
}
PE 9-5
/* Programming Exercise 9-5 */
#include <stdio.h>
void larger_of(double *p1, double *p2);
int main(void)
{
double x, y;
printf("Enter two numbers (q to quit): ");
while (scanf("%lf %lf", &x, &y) == 2)
{
larger_of(&x, &y);
printf("The modified values are %f and %f.\n", x, y);
printf("Next two values (q to quit): ");
}
printf("Bye!\n");
return 0;
}
void larger_of(double *p1, double *p2)
{
double temp = *p1 > *p2 ? *p1 : *p2;
*p1= *p2 = temp;
}
PE 9-7
/* Programming Exercise 9-7 */
#include <stdio.h>
double power(double a, int b); /* ANSI prototype */
int main(void)
{
double x, xpow;
int n;
printf("Enter a number and the integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while (scanf("%lf%d", &x, &n) == 2)
{
xpow = power(x,n); /* function call */
printf("%.3g to the power %d is %.5g\n", x, n, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double a, int b) /* function definition */
{
double pow = 1;
int i;
if (b == 0)
{
if (a == 0)
printf("0 to the 0 undefined; using 1 as the value\n");
pow = 1.0;
}
else if (a == 0)
pow = 0.0;
else if (b > 0)
for(i = 1; i <= b; i++)
pow *= a;
else /* b < 0 */
pow = 1.0 / power(a, - b);
return pow; /* return the value of pow */
}
PE 9-9
/* Programming Exercise 9-9 */
#include <stdio.h>
void to_base_n(int x, int base);
int main(void)
{
int number;
int b;
printf("Enter an integer (q to quit):\n");
while (scanf("%d", &number) == 1)
{
printf("Enter number base (2-10): ");
scanf("%d", &b);
printf("Base %d equivalent: ", b);
to_base_n(number, b);
putchar('\n');
printf("Enter an integer (q to quit):\n");
}
return 0;
}
void to_base_n(int x, int base) /* recursive function */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -