2024年2月25日发(作者:)

C Primer Plus第六版中文版习题答案

Github: /zhayujie/C-Primer-Plus

第一章

1.

#include

int main(void) {

double inch, cm;

printf("Please input the inches: ");

scanf("%lf", &inch);

cm = inch * 2.54;

printf("%g cmn", cm);

return 0;

}

第二章

3.

#include

int main(void)

{

int days,years=21;

days=years*365;

printf("我的年龄是%d岁,%d天n",years,days);

return 0;

}

4.

#include

void jolly(void);

void deny(void);

int main(void)

{

jolly();

jolly();

deny();

return 0;

}

void jolly(void)

{

printf("For he's a jolly good fellow!n");

}

void deny(void)

{

printf("Which nobody can deny!n");

}

5.

#include

void br(void);

void ic(void);

int main(void)

{

br();

printf(",");

ic();

printf("n");

ic();

printf("n");

br();

printf("n");

return 0;

}

void br(void)

{

printf("Brazil,Russia");

}

void ic(void)

{

printf("India,China");

}

6.

#include

int main(void)

{

int toes=10;

int toes_2,toes2;

toes_2=2*toes;

toes2=toes*toes;

printf("toes是%d,toes的两倍是%d,toes的平方是%dn",toes,toes_2,toes2);

return 0;

}

8.

#include

void one_three(void);

void two(void);

int main(void)

{

printf("starting nown");

one_three();

}

void one_three(void)

{

printf("onen");

two();

printf("threen");

printf("done!n");

}

void two(void)

{

printf("twon");

}

第三章

2.

#include

int main(void)

{

char ch;

printf("please input a number:");

scanf("%d",&ch);

printf("%cn",ch);

return 0;

}

4.

#include

int main(void)

{

float a;

printf("Enter a floating-point value: ");

scanf("%f",&a);

printf("fixed-point notation: %fn",a);

printf("exponential notation: %en",a);

return 0;

}

5.

#include

int main(void)

{

int age;

double seconds;

printf("please input your age: ");

scanf("%d",&age);

seconds=age*3.156e7;

printf("the corresponding seconds are: %en",seconds);

return 0;

}

7.

#include

int main(void)

{

float inches,cms;

printf("input your height(inch): ");

scanf("%f",&inches);

cms=inches*2.54;

printf("your height(cm): %fn",cms);

return 0;

}

8.

#include

int main(void)

{

float pint,ounce,soupspoon,teaspoon,cup;

printf("input the number of cups: ");

scanf("%f",&cup);

pint=cup/2;

ounce=cup*8;

soupspoon=ounce*2;

teaspoon=soupspoon*3;

printf("they are equivalent of:n%f pintn%f ouncen%f soupspoonsn%f

teaspoonsn",pint,ounce,soupspoon,teaspoon);

return 0;

}

第四章

1.

#include

int main(void)

{

char firstname[40],lastname[40];

printf("Input your firstname: ");

scanf("%s",firstname);

printf("Input your lastname: ");

scanf("%s",lastname);

printf("Your name is %s,%sn",firstname,lastname);

return 0;

}

2.

#include

#include

int main(void)

{

char name[40];

int width;

printf("Input your name: ");

scanf("%s",name);

width=strlen(name)+3;

printf("%*sn",width,name); //输入的名和姓中间不能分隔

return 0;

}

4.

#include

int main(void)

{

float height;

char name[40];

printf("Input your height(cm) and name: ");

scanf("%f%s",&height,name);

height=height/100;

printf("%s, you are %.3fm talln",name,height);

return 0;

}

5.

#include

int main(void)

{

float speed,size,time;

printf("Input the download speed(Mb/s) and the file size(MB):n");

scanf("%f%f",&speed,&size);

time=size/speed*8.0;

printf("At %.2f megabits per second, a file of %.2f

megabytesn",speed,size);

printf("downloads in %.2f seconds.n",time);

return 0;

}

6.

#include

#include

int main(void)

{

char firstname[40],lastname[40];

printf("Input your firstname: ");

scanf("%s",firstname);

printf("Input your lastname: ");

scanf("%s",lastname);

printf("%s %sn",firstname,lastname);

printf("%*d %*dn",

strlen(firstname),strlen(firstname),strlen(lastname),strlen(lastname));

printf("%s %sn",firstname,lastname);

printf("%*d %*dn",

-strlen(firstname),strlen(firstname),-strlen(lastname),strlen(lastname));

return 0;

}

7.

#include

#include

int main(void)

{

double a=1.0/3.0;

float b=1.0/3.0;

printf("%.6f %.6fn",a,b); //左侧double型 右侧float型

printf("%.12f, %.12fn",a,b);

printf("%.16f, %.16fn",a,b);

printf("DBL_DIG: %dn",DBL_DIG);

printf("FLT_DIG: %dn",FLT_DIG);

return 0;

}

8.

#include

#define GALLON 3.758 //1 gallon=3.785 liters

#define MILE 1.609 //1 mile=1.609 kilometers

int main(void)

{

float gallon,mile;

printf("Input miles and gallons: ");

scanf("%f%f",&mile,&gallon);

printf("Miles per gallon: %.1fn",mile/gallon);

printf("Litre per 100 kilometers: %.1fn",

gallon*GALLON/(mile*MILE)*100);

return 0;

}

第五章

1.

#include

#define H_P_M 60 //1h=60min

int main(void)

{

int hour,min,left;

printf("Enter the number of minutes: ");

scanf("%d",&min);

while(min>0)

{

hour=min/H_P_M;

left=min%H_P_M;

printf("%d minutes is %d hours and %d minutes.n",min,hour,left);

printf("Enter your next value: ");

scanf("%d",&min);

}

printf("Good bye!n");

return 0;

}

2.

#include

int main(void)

{

int num,count;

printf("Input a integer: ");

scanf("%d",&num);

count=0;

while(count++<11)

{

printf("%d ",num);

num++;

}

printf("n");

return 0;

}

3.

#include

#define DAYS_PER_WEEK 7 //一周7天

int main(void)

{

int day,week,left;

printf("Input the number of days: ");

scanf("%d",&day);

while(day>0)

{

week=day/DAYS_PER_WEEK;

left=day%DAYS_PER_WEEK;

printf("%d days are %d weeks, %d days.n",day,week,left);

printf("Next input: ");

scanf("%d",&day);

}

return 0;

}

4.

#include

#define CM_PER_FEET 30.48 //1feet=30.48cm

#define CM_PER_INCH 2.54 //1inch=2.54cm

int main(void)

{

int feet;

float cm,inch;

printf("Enter a height in centimeters: ");

scanf("%f",&cm);

while(cm>0)

{

feet=(int)(cm/CM_PER_FEET);

inch=(cm-feet*CM_PER_FEET)/CM_PER_INCH;

printf("%.1f cm = %d feet, %.1f inchesn",cm,feet,inch);

printf("Enter a height in centimeters (<=0 to quit): ");

scanf("%f",&cm);

}

printf("byen");

return 0;

}

5.

#include

int main(void)

{

int count,sum,days;

printf("Input the number of days: ");

scanf("%d",&days);

count=sum=0;

while(count++

sum=sum+count;

printf("The money you earned: %dn",sum);

return 0;

}

6.

#include

int main(void)

{

int count,sum,days;

printf("Input the number of days: ");

scanf("%d",&days);

count=sum=0;

while(count++

sum=sum+count*count;

printf("The money you earned: %dn",sum);

return 0;

}

7.

#include

void cube(double n);

int main(void)

{

double num;

printf("Input a number: ");

scanf("%lf",&num);

cube(num);

}

void cube(double n)

{

printf("The cube of %f is %fn",n,n*n*n);

}

8.

#include

int main(void)

{

int num1,num2;

printf("This program computes moduli.n");

printf("Enter an integer to serve as the second operand: ");

scanf("%d",&num1);

printf("Now enter the first operand: ");

scanf("%d",&num2);

while(num2>0)

{

printf("%d %% %d is %dn",num2,num1,num2%num1);

printf("Enter next number for first operand (<= 0 to quit): ");

scanf("%d",&num2);

}

printf("Donen");

}

9.

#include

void Temperatures(double fah);

int main(void)

{

double fah,cel,kel;//华氏温度,摄氏温度,开氏温度

printf("Input the Fahrenheit temperature: ");

while(scanf("%lf",&fah)==1)

{

Temperatures(fah);

printf("Next input: ");

}

printf("Done.n");

}

void Temperatures(double fah)

{

const double a=5.0,b=9.0,c=32.0,d=276.13;

printf("%.2f ℉ is %.2f ℃, %.2f K.n",

fah,a/b*(fah-c),a/b*(fah-c)+d);

}

第六章

1.

#include

#define SIZE 26

int main(void)

{

char ch[SIZE];

int index;

for(index=0;index

{

ch[index]='a'+index;

printf("%c ",ch[index]);

}

printf("n");

return 0;

}

2.

#include

int main(void)

{

int i,j;

for(i=1;i<=5;i++)

{

for(j=1;j<=i;j++)

printf("$");

printf("n");

}

return 0;

}

3.

#include

int main(void)

{

int i,j;

for(i=1;i<=6;i++)

{

for(j=0;j

printf("%c",'F'-j);

printf("n");

}

return 0;

}

4.

#include

#define ROWS 6

int main(void)

{

char ch;

int i,j;

for(ch='A',i=0;i

{

for(j=0;j<=i;j++)

printf("%c",ch++);

printf("n");

}

return 0;

}

5.

#include

#define ROWS 5

int main(void)

{

char ch='A';

int i,j;

for(i=1;i<=ROWS;i++)

{

for(j=1;j<=ROWS-i;j++)

printf(" ");

for(j=0;j

printf("%c",ch+j);

for(j=i-2;j>=0;j--)

printf("%c",ch+j);

printf("n");

}

return 0;

}

6.

#include

int main(void)

{

int max,min,num;

printf("Input the min and max: ");

scanf("%d%d",&min,&max);

printf("%10s%10s%10sn","number","square","cube");

for(num=min;num<=max;num++)

printf("%10d%10d%10dn",num,num*num,num*num*num);

return 0;

}

7.

//与题目不同 打印的是句子

#include

#include

#define SIZE 40

int main(void)

{

int i,index=-1;

char ch[SIZE];

printf("Input a word: ");

do

{ index++;

scanf("%c",&ch[index]);}

while(ch[index]!='n');

for(i=index+1;i<=40;i++)

ch[i]='0';

for(index=strlen(ch);index>=0;index--)

printf("%c",ch[index]);

printf("n");

return 0;

}

8.

#include

int main(void)

{

double n1,n2;

printf("Input two numbers: ");

while(2==scanf("%lf%lf",&n1,&n2))

{

printf("%fn",(n1-n2)/n1*n2);

printf("Input your next pair of numbers: ");

}

printf("Bye!n");

return 0;

}

9.

#include

double calculate(double n1, double n2);

int main(void)

{

double num1, num2;

printf("Input two numbers: ");

while (2 == scanf("%lf%lf", &num1, &num2)) //输入两个浮点数

{

printf("%fn", calculate(num1, num2)); //函数调用

printf("Input your next pair of numbers: ");

}

printf("Bye!n");

return 0;

}

double calculate(double n1, double n2)

{

return ((n1 - n2) / (n1 * n2)); //返回运算结果

}

10.

#include

int main(void)

{

int lower, upper;

int num, sum;

printf("Enter lower and upper integer limits: ");

scanf("%d%d", &lower, &upper);

while (lower < upper)

{

for (sum=0, num=lower; num <= upper; num++)

sum = sum + num * num; //计算平方和

printf("The sums of the squares from %d to %d is %dn",

lower * lower, upper * upper, sum); //输出结果

printf("Enter next set of limits: ");

scanf("%d%d", &lower, &upper); //下一次输入

}

printf("Donen");

return 0;

}

11.

#include

#define SIZE 8

int main(void)

{

int num[SIZE];

int index;

printf("Enter 8 integers: ");

for (index=0; index

scanf("%d", &num[index]);

for (index=SIZE-1; index >= 0; index--) //倒序输出

printf("%d ", num[index]);

printf("n");

return 0;

}

12.

#include

int main(void)

{

double sum1=0, sum2=0;

int count, items, sign;

printf("Enter the items: ");

scanf("%d", &items); //输入序列的项数

for (count=1, sign=1; count <= items; count++, sign *= -1)

{

sum1 += 1.0 / count;

sum2 += 1.0 * sign / count;

} //分别计算两序列的和

printf("1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ... = %fn", sum1);

printf("1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 + ... = %fn", sum2);

return 0;

}

13.

#include

#define SIZE 8

int main(void)

{

int index, count, num[SIZE];

for (index = 0, count = 1; index < SIZE; index++)

{

count *= 2;

num[index] = count;

}

//for循环将数组元素设为2的前8次幂

index=0; //初始化index的值

do

printf("%d ", num[index++]);

while (index < SIZE); //do while循环显示数组元素的值

printf("n");

return 0;

}

14.

#include

#define SIZE 8

int main()

{

double num1[SIZE], num2[SIZE];

int index1, index2, index;

printf("Enter 8 numbers to the first array:n");

for (index1 = 0; index1 < SIZE; index1++)

scanf("%lf", &num1[index1]); //向第一个数组输入8个数

num2[0] = num1[0];

for (index1 = 1, index2 = 1; index1 < SIZE; index1++, index2++)

num2[index2] = num2[index2-1] + num1[index1];

//为第二个数组赋值(是第一个数组对应的元素之和)

printf("The first array: ");

for (index=0; index < SIZE; index++) {

printf("%6.2f", num1[index]);

}

printf("nThe second array: ");

for (index=0; index < SIZE; index++) {

printf("%6.2f", num2[index]);

}

printf("n");

return 0;

}

15.

#include

#include

#define SIZE 255

int main(void)

{

//输出第一个数组的内容

//输出第二个数组的内容

int index;

char ch[SIZE];

printf("Enter a line: ");

for(index = 0, scanf("%c", &ch[0]); ch[index] != 'n';)

{

index++;

scanf("%c", &ch[index]);

} //输入内容到字符数组中,回车时结束

for(index += 1; index < SIZE; index++)

ch[index] = '0'; //将数组剩余空间补充为'0'

for(index = strlen(ch); index >=0; index--)

printf("%c", ch[index]);

printf("n");

return 0;

}

16.

#include

#define RATE_DAPHNE 0.1

#define RATE_DEIRDRE 0.05

#define MONEY 100

int main(void)

{

int year;

//倒序输出内容

//两人的利率

double daphne = MONEY, deirdre = MONEY; //两人的初始投资额相同

for (year = 1; daphne >= deirdre; year++)

{

daphne += MONEY * RATE_DAPHNE;

deirdre += deirdre * RATE_DEIRDRE;

}

//计算Deirdre投资额超过Daphne需要的年数和当时的金额

printf("After %d year, Deirdre's investment will be more than

Daphne's,n"

"Daphne's investment will be $%lf,nand Deirdre's investment will

be $%lf.n",

year, daphne, deirdre);

return 0;

}

//输出结果

17.

#include

#define INITIAL_MONEY 100 //账户初始金额为100万元

#define ANNUAL_RATE 0.08 //年利率为8%

int main(void)

{

int year;

double money;

for(year = 1, money=INITIAL_MONEY; money>0; year++)

money += money * ANNUAL_RATE - 10; //计算每年年终的账户余额

printf("After %d years, Chuckie will draw all money from his account.n",

year);

return 0;

}

18.

#include

#define INITIAL_NUMBER 5 //初始朋友数为5人

#define DUNBAR_NUMBER 150 //邓巴数

int main(void)

{

int week;

int number = INITIAL_NUMBER;

for (week = 1; number <= DUNBAR_NUMBER; week++)

{

number = (number - week) * 2; //计算每周的朋友数量

printf("After %d week, the number of Rabnud's friends is %dn", week,

number);

}

return 0;

}

第七章

1.

#include

int main(void)

{

char ch;

int n_space = 0; //空格数

int n_newline = 0; //换行数

int n_others = 0; //其他字符数

printf("Enter some text; Enter # to quit.n");

while ((ch = getchar()) != '#')

{

if (ch == ' ')

n_space++;

else if (ch == 'n')

n_newline++;

else

n_others++;

}

printf("Spaces: %d, newlines: %d, others: %dn",

n_space, n_newline, n_others);

return 0;

}

2.

#include

#define CHARS_PER_LINE 8 //每行字符数

int main(void)

{

char ch;

int n_chars = 1; //字符数

printf("Enter some characters(# to quit):n");

while ((ch = getchar()) != '#')

{

printf("%3c(%3d) ", ch, ch);

if (n_chars++ % CHARS_PER_LINE == 0)

printf("n");

}

printf("n");

return 0;

}

3.

#include

int main(void)

{

int num;

int n_even = 0, n_odd = 0; //偶数和奇数个数

int sum_even = 0, sum_odd = 0; //偶数和奇数和

printf("Enter some integers(0 to quit):n");

scanf("%d", &num);

while (num != 0)

{

if (num % 2 == 0)

{

n_even++;

sum_even += num;

}

else

{

n_odd++;

sum_odd +=num;

//计算偶数个数和偶数和

} //计算奇数个数和奇数和

scanf("%d",&num);

}

printf("The number of even numbers is %d, "

"and the everage of even numbers is %.2fn",

n_even, (n_even == 0) ? 0 : (float)sum_even / n_even);

printf("The number of odd numbers is %d, "

"and the everrage of odd numers is %.2fn",

n_odd, (n_odd == 0) ? 0 : (float)sum_odd / n_odd);

return 0;

}

4.

#include

int main(void)

{

char ch;

int n_repl = 0; //替换次数

printf("Enter some texts(# to quit):n");

while ((ch = getchar()) != '#') {

if (ch == '.')

{

ch = '!';

n_repl++;

} //替换句号

else if (ch == '!')

{

printf("!");

n_repl++;

} //替换感叹号

printf("%c", ch);

}

printf("n%d substitutions were made.n", n_repl);

return 0;

}

5.

#include

int main(void)

{

char ch;

int n_repl = 0; //替换次数

printf("Enter some texts(# to quit):n");

while ((ch = getchar()) != '#') {

switch (ch)

{

case '.': ch = '!';

n_repl++;

break;

case '!': printf("!");

n_repl++;

break;

default: break;

} //利用switch语句进行替换

printf("%c",ch);

}

printf("n%d substitutions were made.n", n_repl);

return 0;

}

6.

#include

int main(void)

{

char ch;

char last_ch = 0; //前一个字符

int count=0;

printf("Enter some texts(# to quit):n");

while ((ch = getchar()) != '#')

{

if ((ch == 'i') && (last_ch == 'e'))

count++;

last_ch = ch; //出现ei时,计数+1

}

printf(""ei" appeared %d times.n", count);

return 0;

}

7.

#include

#define BASE 1000 //基本工资 100美元/h

#define TIME 40 //超过40h为加班

#define MUL 1.5 //加班时间算作平时的1.5倍

#define RATE1 0.15 //前300美元的税率

#define RATE2 0.2 //300-450美元的税率

#define RATE3 0.25 //大于450美元的税率

#define BREAK1 300 //税率的第一个分界点

#define BREAK2 450 //税率的第二个分界点

int main(void)

{

double hour, tax, gross;

printf("Input your work hours in a week: ");

scanf("%lf", &hour);

if (hour <= TIME)

gross = hour * BASE;

else

gross = TIME * BASE + (hour - TIME) * MUL * BASE;

//计算总收入

if (gross <= BREAK1)

tax = gross * RATE1;

else if (gross <= BREAK2)

tax = BREAK1 * RATE1 + (gross - BREAK1) * RATE2;

else

tax = BREAK1 * RATE1 + (BREAK2 - BREAK1) * RATE2

+ (gross - BREAK2) * RATE3;

//计算税金

printf("Your gross income is $%.2lfnYour tax is $%.2lfn"

"Your net income is $%.2lfn",

gross, tax, (gross - tax));

return 0;8.

#include

#define BASE1 8.75

#define BASE2 9.33

#define BASE3 10.00

#define BASE4 11.20

//四种等级的基本工资

#define TIME 40 //超过40h为加班

#define MUL 1.5 //加班时间算作平时的1.5倍

#define RATE1 0.15 //前300美元的税率

#define RATE2 0.2 //300-450美元的税率

#define RATE3 0.25 //大于450美元的税率

#define BREAK1 300 //税率的第一个分界点

#define BREAK2 450 //税率的第二个分界点

int main(void)

{

double base, hour, tax, gross;

int count, num;

const int LENGTH = 65; //*的长度

printpart: for (count = 0; count < LENGTH; count++)

printf("*");

printf("nEnter the number corresponding to the desired pay rate or

action:n");

printf("%-36s%s","1) $8.75/hr", "2) $9.33/hrn");

printf("%-36s%s","3) $10.00/hr", "4) $11.20/hrn");

printf("%sn", "5) quit");

for (count = 0; count < LENGTH; count++)

printf("*");

printf("n");

//打印表格

while (scanf("%d", &num) == 1) {

switch (num)

{

case 1: base = BASE1;

break;

case 2: base = BASE2;

break;

case 3: base = BASE3;

break;

case 4: base = BASE4;

break;

case 5: printf("quit.n");

return 0;

default: printf("Please input the right option.n");

goto printpart;

} //选择基本工资等级

printf("Input your work hours in a week: ");

scanf("%lf", &hour);

if (hour <= TIME)

gross = hour * base;

else

gross = TIME * base + (hour - TIME) * MUL * base;

//计算总收入

if (gross <= BREAK1)

tax = gross * RATE1;

else if (gross <= BREAK2)

tax = BREAK1 * RATE1 + (gross - BREAK1) * RATE2;

else

tax = BREAK1 * RATE1 + (BREAK2 - BREAK1) * RATE2

+ (gross - BREAK2) * RATE3;

//计算税金

printf("Your gross income is $%.2lfnYour tax is $%.2lfn"

"Your net income is $%.2lfn",

gross, tax, (gross - tax));

printf("nYour next choice:n");

}

return 0;

}

9.

#include

int main(void)

{

int div, prime;

int num, count;

int flag;

printf("Input a positive integer: ");

scanf("%d", &num);

printf("The prime numbers in range:n");

for (prime = 2; prime <= num; prime++) //外层循环显示所有素数

{

flag = 1;

for (div = 2; (div * div) <= prime; div++)

{

if (prime % div == 0)

flag = 0;

}

if (flag)

printf("%d ",prime);

}

printf("n");

return 0;

}

10.

#include

#define RATE1 0.15

#define RATE2 0.28

//内层循环检验是否为素数

//利用标记flag判断

#define SINGLE 17850 //单身人群的税率分界点

#define HOST 23900 //户主人群的税率分界点

#define MAR_SHA 29750 //已婚共有人群的分界点

#define MAR_DEV 14875 //已婚离异人群的分界点

int main(void)

{

int num;

double income, tax_break, tax;

printpart: printf("Please enter Corresponding"

"figures to select the typen");

printf("1 single, 2 host, 3 married and shared, "

"4 married but devoced and 5 to quit.n");

scanf("%d", &num);

switch (num)

{

case 1: tax_break = SINGLE;

break;

case 2: tax_break = HOST;

break;

case 3: tax_break = MAR_SHA;

break;

case 4: tax_break = MAR_DEV;

break;

case 5: printf("quit.n");

return 0;

default: printf("Please input right number.");

goto printpart;

}

//回到输入阶段

printf("Enter your income: "); //指定种类和收入

while (scanf("%lf", &income) == 1)

{

if (income <= tax_break)

tax = income * RATE1;

else

tax = tax_break * RATE1 + (income - tax_break) * RATE2;

//计算税金

printf("The tax is $%.2lf.n", tax);

printf("Your next input: n");

goto printpart;

}

return 0;

}

11.

#include

#include

//回到输入阶段

#define ARTICHOKE 2.05 //洋蓟2.05美元/磅

#define BEET 1.15 //甜菜1.15美元/磅

#define CARROT 1.09 //胡萝卜1.09美元/磅

#define DISCOUNT_LIMIT 100

//包装费和运费打折要求订单100美元

#define DISCOUNT_RATE 0.05 //折扣为%5

#define BREAK1 5

#define BREAK2 20

#define FEE1 6.5

#define FEE2 14

#define FEE3_RATE 0.5

//装运费的分界点

//不同重量区间的装运费,其中超过20磅的每续重一磅

//增加0.5元

int main(void)

{

double weight;

double weight_artichoke = 0;

double weight_beet = 0;

double weight_carrot = 0; //购买三种蔬菜的重量

double total_weight; //总重量

double veg_cost; //三种蔬菜总共花费

double order_cost; //订单总额

double total_cost; //费用总额

double pack_tran_fee; //装运费

double discount;

int count = 0;

char ch;

printf("Please select the vegetables you want to buy:n");

printf("a: artichoke $%.2f/lbn", ARTICHOKE);

printf("b: beet $%.2f/lbn", BEET);

printf("c: carrot $%.2f/lbn", CARROT);

printf("q: quit.n");

//打印选择信息

while ((ch = tolower(getchar())) != 'q')

{

// if (ch == 'n')

// continue; //滤掉回车

switch (ch)

{

case 'a': printf("Input the weight of artichoke in pound: ");

scanf("%lf", &weight);

weight_artichoke += weight;

count++;

printf("Continue entering a, b, c or q: ");

break;

case 'b': printf("Input the weight of beet in pound: ");

scanf("%lf", &weight);

weight_beet += weight;

count++;

printf("Continue entering a, b, c or q: ");

break;

case 'c': printf("Input the weight of carrot in pound: ");

scanf("%lf", &weight);

weight_carrot += weight;

count++;

printf("Continue entering a, b, c or q: ");

break;

default: printf("Please enter the right character.");

}

while (getchar () != 'n')

continue;

}

if (!count)

{

printf("Bye.n");

return 0;

//滤掉输入重量后面的所有字符

} //开始输出q,直接退出

total_weight = weight_artichoke + weight_beet + weight_carrot;

veg_cost = weight_artichoke * ARTICHOKE + weight_beet * BEET

+ weight_carrot * CARROT;

discount = 0;

if (veg_cost >= DISCOUNT_LIMIT)

{

discount = veg_cost * DISCOUNT_RATE;

order_cost = veg_cost - discount;

}

else

order_cost = veg_cost; //折扣计算

if (total_weight <= BREAK1)

pack_tran_fee = FEE1;

else if (total_weight <= BREAK2)

pack_tran_fee = FEE2;

else

pack_tran_fee = FEE2 + (total_weight - BREAK2) * FEE3_RATE;

//装运费计算

total_cost = order_cost + pack_tran_fee;

printf("nHere is what you choose:n");

if (weight_artichoke) {

printf("artichoke Price: $%.2f/lb weight: %.2f pounds cost:

$%.2fn",

ARTICHOKE, weight_artichoke, weight_artichoke * ARTICHOKE);

}

if (weight_beet) {

printf("beet Price: $%.2f/lb weight: %.2f pounds cost:

$%.2fn",

BEET, weight_beet, weight_beet * BEET);

}

if (weight_carrot) {

printf("carrot Price: $%.2f/lb weight: %.2f pounds cost:

$%.2fn",

CARROT, weight_carrot, weight_carrot * CARROT);

}

printf("The order cost: $%.2fn", veg_cost);

if (discount)

printf ("You have %%%.f discount, and the reduced money is $%.2fn",

DISCOUNT_RATE * 100, discount);

printf("The packing and transpoting fee is $%.2fn", pack_tran_fee);

printf("The total cost is $%.2fn", total_cost);

//输出订单信息

return 0;

}

第八章

1.

#include

int main(void)

{

int ch;

int count = 0;

while ((ch = getchar()) != EOF)

count++;

printf("The number of characters is %dn", count);

return 0;

}

2.

#include

#define SPACE 32

#define CTRL 64

#define COL 10

int main(void)

//每行打印10对

{

char ch;

int count = 0;

while ((ch = getchar()) != EOF)

{

count++;

//打印字符输出

if (ch >= SPACE)

printf("%c ", ch);

//换行符和制表符的输出

else if (ch == 'n' || ch == 't')

printf("%s", ch == 'n' ? "n" : "t");

//一般控制字符的输出

else

printf("^%c", ch + CTRL);

printf("%-5d", ch);

if (ch == 'n')

count = 0;

if (count % COL == 0) //10对换行

printf("n");

}

return 0;

}

3.

#include

#include

int main(void)

{

int lower = 0, upper = 0;

char ch;

printf("Input some texts:n");

while ((ch = getchar()) != EOF)

{

if (islower(ch))

lower++;

if (isupper(ch))

upper++;

}

printf("lower:%d, upper:%dn", lower, upper);

return 0;

}

4.

#include

#include

int main(void)

{

char ch;

int inword = 0; //在单词中为1否则为0

int words = 0; //单词数

int chars = 0; //字符数

printf("Please enter some words:n");

while ((ch = getchar()) != EOF)

{

if (!isspace(ch) && !ispunct(ch))

chars++;

//开始一个新的单词

if (!isspace(ch) && !ispunct(ch) && !inword)

{

inword = 1;

words++;

}

//单词末尾

if (isspace(ch) || ispunct(ch) && inword)

inword = 0;

}

printf("There are %d words and %d lettersn",

words, chars);

printf("The average number of letters of ");

printf("each word is %.2f.n",

((double)chars / (double)words));

return 0;

}

5.

#include

#define LOW 1

#define HIGH 100

//初始下限

//初始上限

int get_guess(void);

int main(void)

{

char response;

int guess;

int low = LOW;

int high = HIGH;

guess = (low + high) / 2;

printf("Pick an integer from %d to %d. I will try to guess ",

LOW, HIGH);

printf("nd with a y if my guess is right, with an s if itn");

printf("is small and with a b if it is big.n");

printf("Uh...is your number %d?n", guess);

while ((response = getchar()) != 'y')

{

if (response == 's')

{

low = guess;

guess = (guess + high) / 2;

}

else if (response == 'b')

{

high = guess;

guess = (guess + low) / 2;

}

else

printf("Sorry, I understand only y, s and b.n");

printf("Well, then, is it %d?n", guess);

//跳过剩余输入行

while (getchar() != 'n')

continue;

//使取值能到达上限

if (guess == HIGH - 1)

guess++;

}

printf("I knew i could do it!n");

return 0;

}

6.

#include

#include

char get_first(void);

int main(void)

{

char ch;

while ((ch = get_first()) != EOF)

{

putchar(ch);

printf("n");

}

return 0;

}

char get_first(void)

{

char ch;

while (isspace(ch = getchar()))

continue;

while (getchar() != 'n')

continue;

return ch;

}

7.

#include

#define BASE1 8.75

#define BASE2 9.33

#define BASE3 10.00

#define BASE4 11.20

//四种等级的基本工资

#define TIME 40 //超过40h为加班

#define MUL 1.5 //加班时间算作平时的1.5倍

#define RATE1 0.15 //前300美元的税率

#define RATE2 0.2 //300-450美元的税率

#define RATE3 0.25 //大于450美元的税率

#define BREAK1 300 //税率的第一个分界点

#define BREAK2 450 //税率的第二个分界点

#define LENGTH 65 //*的长度

char get_choice(void);

void calculate(double base);

int main(void)

{

double base;

char choice;

while ((choice = get_choice()) != 'q')

{

switch (choice)

{

case 'a': base = BASE1;

break;

case 'b': base = BASE2;

break;

case 'c': base = BASE3;

break;

case 'd': base = BASE4;

break;

default: printf("Please respond with a, b, c, d or q.n");

break;

}

if (choice >= 'a' && choice <= 'd')

calculate(base);

}

return 0;

}

//计算和输出结果的函数

void calculate(double base)

{

double hour, gross, tax;

printf("Input your work hours in a week: ");

scanf("%lf", &hour);

while (getchar() != 'n')

continue; //跳过回车

if (hour <= TIME)

gross = hour * base;

else

gross = TIME * base + (hour - TIME) * MUL * base;

//计算总收入

if (gross <= BREAK1)

tax = gross * RATE1;

else if (gross <= BREAK2)

tax = BREAK1 * RATE1 + (gross - BREAK1) * RATE2;

else

tax = BREAK1 * RATE1 + (BREAK2 - BREAK1) * RATE2

+ (gross - BREAK2) * RATE3;

//计算税金

printf("Your gross income is $%.2lfnYour tax is $%.2lfn"

"Your net income is $%.2lfn",

gross, tax, (gross - tax));

printf("n");

}

//打印选择界面并让用户输入的函数

char get_choice(void)

{

char ch;

int count;

for (count = 0; count < LENGTH; count++)

printf("*");

printf("nEnter the letter corresponding to the desired pay rate or

action:n");

printf("%-36s%s","a) $8.75/hr", "b) $9.33/hrn");

printf("%-36s%s","c) $10.00/hr", "d) $11.20/hrn");

printf("%sn", "q) quit");

for (count = 0; count < LENGTH; count++)

printf("*");

printf("n");

ch = getchar();

while (getchar() != 'n')

continue;

return ch;

}

8.

#include

float get_float(void);

char get_choice(void);

char get_first(void);

int main(void)

{

char choice;

//跳过输出行剩余内容

float num1, num2;

while ((choice = get_choice()) != 'q')

{

printf("Enter first number: ");

num1 = get_float();

printf("Enter second number: ");

num2 = get_float();

switch (choice)

{

case 'a': printf("%.2f + %.2f = %.2fn",

num1, num2, num1 + num2);

break;

case 's': printf(".2%f - %.2f = %.2fn",

num1, num2, num1 - num2);

break;

case 'm': printf("%.2f * %.2f = %.2fn",

num1, num2, num1 * num2);

break;

case 'd':

if (!num2)

{

printf("Enter a number other than 0: ");

num2 = get_float();

}

printf("%.2f / %.2f = %.2fn",

num1, num2, num1 / num2);

break;

default: printf("Program error!n");

break;

}

}

printf("Bye.n");

return 0;

}

//打印界面和让用户选择的函数

char get_choice(void)

{

char ch;

printf("Enter the operation of your choice:n");

printf("%-36s%sn", "a. add", "s. subtract");

printf("%-36s%sn", "m. multiply", "d. divide");

printf("q. quitn");

ch = get_first();

while (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd'

&& ch != 'q')

{

printf("Please enter a, s, m, d or q.n");

ch = get_first();

}

return ch;

}

//获得输入值并处理错误输入的函数

float get_float(void)

{

float input;

char ch;

while (scanf("%f", &input) != 1)

{

while ((ch = getchar()) != 'n')

putchar(ch);

printf(" is not an number.n");

printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");

}

while (getchar() != 'n')

continue;

return input;

}

//读取输入行的第一个字符

char get_first(void)

{

char ch;

ch = getchar();

while (getchar() != 'n')

continue;

return ch;

}

第九章

1.

#include

double min(double x, double y);

int main(void)

{

double num1, num2;

printf("Enter two numbers: ");

scanf("%lf%lf", &num1, &num2);

printf("The smaller one is: %fn", min(num1, num2));

return 0;

}

double min(double n1, double n2)

{

return (n1 < n2 ? n1 : n2);

}

2.

#include

void chline(char ch, int i, int j);

int main(void)

{

int row, col;

char ch;

printf("Input a character you want to print: ");

ch = getchar();

printf("Input the number of rows and columns: ");

scanf("%d%d", &row, &col);

chline(ch, row, col);

return 0;

}

void chline(char ch, int i , int j)

{

int row, col;

for (row = 1; row <= i; row++)

{

for(col = 1; col <=j; col++)

putchar(ch);

putchar('n');

}

return;

}

4.

#include

double har_mean(double num1, double num2);

int main(void)

{

double num1, num2;

printf("Enter two numbers: ");

scanf("%lf%lf", &num1, &num2);

printf("The harmonic mean is: %fn", har_mean(num1, num2));

}

double har_mean(double num1, double num2)

{

return 1 / (1 / num1 + 1 / num2);

}

5.

#include

void larger_of(double * x, double * y);

int main(void)

{

double num1, num2;

printf("Input two numbers: ");

scanf("%lf%lf", &num1, &num2);

printf("Originally, num1 = %f, num2 = %fn",

num1, num2);

larger_of(&num1, &num2);

printf("Now, num1 = %f, num2 = %fn", num1, num2);

return 0;

}

void larger_of(double * x, double * y)

{

*x > *y ? (*y = *x) : (*x = *y);

}

6.

#include

void sort(double * x, double * y, double * z);

int main(void)

{

double num1, num2 ,num3;

printf("Enter 3 numbers: ");

scanf("%lf%lf%lf", &num1, &num2, &num3);

printf("Originallynnum1 = %.2f, num2 = %.2f, num3 = %.2fn",

num1, num2, num3);

sort(&num1, &num2, &num3);

printf("Nownnum1 = %.2f, num2 = %.2f, num3 = %.2fn",

num1, num2, num3);

return 0;

}

void sort(double * x, double * y, double * z)

{

double temp;

if (*y < *x)

{

temp = *y;

*y = *x;

*x = temp;

}

if (*z < *x)

{

temp = *z;

*z = *x;

*x = temp;

}

if (*z < *y)

{

temp = *z;

*z = *y;

*y = temp;

}

}

7.

#include

#include

int get_ch(char ch);

int main(void)

{

char ch;

printf("Input some texts:n");

while ((ch = getchar()) != EOF)

printf("%d ", get_ch(ch));

return 0;

}

int get_ch(char ch)

{

if (isalpha(ch))

return tolower(ch) - 'a' +1;

else

return -1;

}

8.

#include

double power(double n, int p);

int main(void)

{

double x, xpow;

int exp;

printf("Enter a number and the interger power (q to quit):n");

while (scanf("%lf%d", &x, &exp) == 2)

{

xpow = power(x, exp);

printf("%.3g to the power %d is %gn", x, exp, xpow);

printf("Enter the next pair of numbers or q to quit.n");

}

printf("Bye.n");

}

double power(double n, int p)

{

int count;

double pow = 1;

if (p == 0)

if (n == 0)

{

pow = 1;

printf("It is not defined, so the value is treated as 1n");

}

else

pow = 1;

else if (p > 0)

for (count = 0; count < p; count++)

pow *= n;

else

if (n != 0)

for (count = 0; count > p; count--)

pow /= n;

else

pow = 0;

return pow;

}

9.

#include

double power(double n, int p);

int main(void)

{

double x, xpow;

int exp;

printf("Enter a number and the interger power (q to quit):n");

while (scanf("%lf%d", &x, &exp) == 2)

{

xpow = power(x, exp);

printf("%.3g to the power %d is %gn", x, exp, xpow);

printf("Enter the next pair of numbers or q to quit.n");

}

printf("Bye.n");

}

double power(double n, int p)

{

int count;

double pow;

if (p > 0)

pow = n * power(n, p - 1);

else if (p < 0)

if (n != 0)

pow = (1.0 / n) * power(n, p + 1);

else

pow = 0;

else

pow = 1;

return pow;

}

10.

#include

void to_base_n(unsigned long n, unsigned int base);

int main(void)

{

unsigned long num;

unsigned int base;

printf("Enter an integer and the base from 2 to 10 (q to quit): ");

while (scanf("%lu%u", &num, &base) == 2)

{

printf("Equivalent number on the base of %u: ", base);

to_base_n(num, base);

printf("nEnter your next pair of numbers(q to quit): ");

}

printf("Done.n");

return 0;

}

void to_base_n(unsigned long n, unsigned int base)

{

int r;

r = n % base;

if (n >= base)

to_base_n(n / base, base);

printf("%d", r);

}

11.

#include

unsigned long Fibonacci(unsigned n);

int main(void)

{

unsigned int num;

int count;

printf("Input the number of the items (q to quit): ");

while (scanf("%u", &num) == 1)

{

for (count = 1; count <= num; count++)

printf("%lu ", Fibonacci(count));

printf("nYour next input(q to quit): ");

}

printf("Bye.n");

return 0;

}

unsigned long Fibonacci(unsigned n)

{

int count, temp;

int n1 = 1, n2 = 1;

if (n > 2)

for (count = 3; count <= n; count++)

{

temp = n1 + n2;

n1 = n2;

n2 = temp;

}

else

n2 = 1;

return n2;

}

第十章

1.

#include

#define MONTHS 12

#define YEARS 5

int main(void)

{

//一年的月份数

//一年的年数

//用2010~2014年的降水量数据初始化数组

const float rain[YEARS][MONTHS] =

{

{4.3,4.3,4.3,3.0,2.0,1.2,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.3},

{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},

{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},

{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}

};

int year, month;

float subtot, total;

printf(" YEAR RAINFALL (inches)n");

for (year = 0, total = 0; year < YEARS; year++)

{ //每一年,个月降水量总和

for (month = 0, subtot = 0; month < MONTHS; month++)

subtot += *(*(rain + year) + month);

printf("%5d %15.1fn", 2010 + year, subtot);

total += subtot; //5年的总降水量

}

printf("n The yearly average is %.1f ", total / YEARS);

printf(" MONTHLY AVERAGES:nn");

printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");

printf(" Nov Decn");

for (month = 0; month < MONTHS; month++)

{

for (year = 0, subtot = 0; year < YEARS; year++)

subtot += *(*(rain + year) +month);

printf("%4.1f ", subtot / YEARS);

}

printf("n");

return 0;

}

2.

#include

#define SIZE 5

void copy_arr(double ar1[], double ar[], int n);

void copy_ptr(double * ar2, double * ar, int n);

void copy_ptrs(double * ar3, double * ar, double * end);

void print_array(double ar[], int n); //打印数组的函数

int main(void)

{

double source[SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5};

double target1[SIZE];

double target2[SIZE];

double target3[SIZE];

copy_arr(target1, source, SIZE);

copy_ptr(target2, source, SIZE);

copy_ptrs(target3, source, source + SIZE);

print_array(target1, SIZE);

print_array(target2, SIZE);

print_array(target3, SIZE);

return 0;

}

void copy_arr(double ar1[], double ar[], int n)

{

int index;

for (index = 0; index < n; index++)

ar1[index] = ar[index];

}

void copy_ptr(double * ar2, double * ar, int n)

{

int i;

for (i = 0; i < n; i++)

*(ar2++) = *(ar++);

}

void copy_ptrs(double * ar3, double * ar, double * end)

{

int i;

for (i = 0; ar < end; i++)

*(ar3++) = *(ar++);

}

void print_array(double ar[], int n)

{

int index;

for (index = 0; index < n; index++)

printf("%g ", ar[index]);

printf("n");

}

3.

#include

int max(int ar[], int n);

int main(void)

{

int num[] = {23,2,32,66,3,5,25,7,3,20};

printf("The biggest number is %dn", max(num, 10));

return 0;

}

int max(int ar[], int n)

{

int i, max;

for (i = 0, max = ar[0]; i < n; i++)

{

if (max < ar[i])

max = ar[i];

}

return max;

}