当前位置:文档之家› C primer plus课后编程练习答案

C primer plus课后编程练习答案

C primer plus课后编程练习答案
C primer plus课后编程练习答案

第一章概览

编程练习

1.您刚刚被MacroMuscle有限公司(Software for Hard Bodies)聘用。该公司要进入欧洲市场,需要一个将英寸转换为厘米(1英寸= cm)的程序。他们希望建立的该程序可提示用户输入英寸值。您的工作是定义程序目标并设计该程序(编程过程的第1步和第2步)。

1.将英寸值转化为厘米值

2.显示“输入英寸值”->得到该值->转换为厘米值->存储->告知用户已结束

第二章 C语言概述

编程练习

1.编写一个程序,调用printf()函数在一行上输出您的名和姓,再调用一次printf()函数在两个单独的行上输出您的名和姓,然后调用一对printf()函数在一行上输出您的名和姓。输出应如下所示(当然里面要换成您的姓名):

Anton Bruckner

Anton

Bruckner

Anton Bruckner

第一个输出语句

第二个输出语句

仍然是第二个输出语句

第三个和第四个输出语句

#include<>

int main(void)

{

printf("He Jin\n");

printf("He\n");

printf("Jin\n");

printf("He Jin\n");

return(0);

}

2.编写一个程序输出您的姓名及地址。

#include<>

int main(void)

{

printf("Name:He Jin\n");

printf("Address:CAUC\n");

return(0);

}

3.编写一个程序,把您的年龄转换成天数并显示二者的值。不用考虑平年( fractional year)和闰年(leapyear)的问题。

#include<>

int main(void)

{

int age=22;

printf("Age:%d\n",age);

printf("Day:%d\n",age*356);

return(0);

}

4.编写一个能够产生下面输出的程序:

For he's a jolly good fellow!

For he's a jolly good fellow!

For he's a jolly good fellow!

Which nobody can deny!

程序中除了main()函数之外,要使用两个用户定义的函数:一个用于把上面的夸奖消息输出一次:另一个用于把最后一行输出一次。

#include<>

void printf1(void);

void printf2(void);

int main(void)

{

printf1();

printf1();

printf1();

printf2();

return(0);

}

void printf1(void)

{

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

}

void printf2(void)

{

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

}

5.编写一个程序,创建一个名为toes的整数变量。让程序把toes设置为10。再让程序计算两个toes的和以及toes的平方。程序应该输出所有的3个值,并分别标识它们。

#include<>

int main(void)

{

int toes=10;

int toes_add;

int toes_square;

toes_add=toes+toes;

toes_square=toes*toes;

printf("toes=%d\ntoes_add=%d\ntoes_square=%d\n",toes,toes_add,toes_square);

return(0);

}

6.编写一个能够产生下列输出的程序:

Smile ! Smile ! Smile

Smile ! Smile !

Smile !

在程序中定义一个能显示字符串smile卜一次的函数,并在需要时使用该函数。

#include<>

void display(void);

int main(void)

{

display();

display();

display();

printf("\n");

display();

display();

printf("\n");

display();

printf("\n");

return(0);

}

void display(void)

{

printf("Smile!");

}

7.编写一个程序,程序中要调用名为one_three()的函数。该函数要在一行中显示单词"one",再调用two()函数,然后再在另一行中显示单词"three"。函数two()应该能在一行中显示单词"two"。main()函数应该在调用one_three()函数之前显示短语"starting now:",函数调用之后要显示"done!"o这样,最后的输出结果应如下所示:

starting now

one

two

three

done !

#include<>

void one_three(void);

void two(void);

int main(void)

{

printf("starting now:\n");

one_three();

printf("done!\n");

return(0);

}

void one_three(void)

{

printf("One\n");

two();

printf("Three\n");

}

void two(void)

printf("Two\n");

}

第三章数据和C

编程练习

1.通过试验的方法(即编写带有此类问题的程序)观察系统如何处理整数上溢、浮点数上溢和浮点数下溢的情况。

#include<>

int main(void)

{

unsigned int a=95;

float b=;

float c=b*10;

float d=;

printf("%u+1=%u\n",a,a+1);

printf("%e*10=%e\n",b,c);

printf("%f/10=%f\n",d,d/10);

return(0);

}

2.编写一个程序,要求输入一个ASCII码值(如66),然后输出相应的字符。

#include<>

int main(void)

{

char a;

scanf("%d",&a);

printf("%c\n",a);

return(0);

}

3.编写一个程序,发出警报声,并打印下列文字:

Startled by the sudden sound, Sally shouted, "By the Great Pumpkin, what was that!"

#include<>

int main(void)

printf("\aStartled by the sudden sound,Sally shouted,\"By the Great pumpkin,what was that!\"\n");

return(0);

}

4.编写一个程序,读入一个浮点数,并分别以小数形式和指数形式打印。输出应如同下面格式(实际显示的指数位数也许因系统而不同):

The input is or +001.

#include<>

int main(void)

{

float a;

scanf("%f",&a);

printf("The input is %f or %e\n",a,a);

return(0);

}

5.一年约有×l07S。编写一个程序,要求输入您的年龄,然后显示该年龄合多少秒。

#include<>

int main(void)

{

float a;

printf("Please input your age:");

scanf("%f",&a);

printf("Your age is %e seconds\n",a*;

return(0);

}

个水分子的质量约为×10^-23g,l夸脱水大约有950g。编写一个程序,要求输入水的夸脱数,然后显示这么多水中包含多少个水分子。

#include<>

int main(void)

{

float a;

printf("Please input how much quarts the water is:");

scanf("%f",&a);

printf("%f quarts water has %e molecules.\n",a,a*950/3E-23);

}

7. 1英寸等于。编写一个程序,要求输入您的身高(以英寸为单位),然后显示该身高值等于多少厘米。如果您愿意,也可以要求以厘米为单位输入身高,然后以英寸为单位进行显示。

#include<>

int main(void)

{

float a;

printf("Please input your height by inches:");

scanf("%f",&a);

printf("Your height is %fcm.\n",a*;

return(0);

}

第4章字符串和格式化输入/输出编程练习

1.编写一个程序,要求输入名字和姓氏,然后以“名字,姓氏”的格式打印。

#include<>

int main(void)

{

char surname[20],firstname[20];

printf("Please input your firstname and surname:");

scanf("%s",firstname);

scanf("%s",surname);

printf("You are %s,%s.\n",firstname,surname);

return(0);

}

2.编写一个程序,要求输入名字,并执行以下操作:

a.把名字引在双引号中打印出来。

b.在宽度为20个字符的字段内打印名字,并且整个字段引在引号内。

c. 在宽度为20个字符的字段的左端打印名字,并且整个字段引在引号内。

d.在比名字宽3个字符的字段内打印它。

#include<>

int main(void)

{

char name[20];

int width;

printf("Please input your name:");

scanf("%s",name);

printf("A:\"%s\"\n",name);

printf("B:\"%20s\"\n",name);

printf("C:\"%-20s\"\n",name);

width=strlen(name)+3;

printf("D:\"%*s\"\n",width,name);The input is or +001.

b. The input is + or +001.

#include<>

int main(void)

{

float a;

printf("Please input a float:");

scanf("%f",&a);

printf(" input is %.1f or %.1e\n",a,a);

printf(" input is %+.3f or %.3E\n",a,a);

return(0);

}

4.编写—个程序,要求输入身高(以英寸为单位)和名字,然后以如下形式显示:

Dabney, you are feet tall

使用float类型,使用/作为除号。如果您愿意,可以要求以厘米为单位输入身高,并以米为单位进行显示。

#include<>

int main(void)

{

float height;

char name[20];

printf("Please input your height by inches:");

scanf("%f",&height);

printf("Please input your name:");

scanf("%s",name);

printf("%s,you are %f feet tall\n",name,height/12);

return(0);

}

5.编写一个程序,首先要求用户输入名字,然后要求用户输入姓氏。在一行打印输入的姓名,在下行打印每个名字中字母的个数。把字母个数与相应名字的结尾对齐,如下所示:

Melissa Honeybee

7 8

然后打印相同的信息,但是字母个数与相应单词的开始对齐。

#include<>

#include<>

int main(void)

{

char surname[20],firstname[20];

printf("Please input your firstname and surname:");

scanf("%s",firstname);

scanf("%s",surname);

printf("%s %s.\n",firstname,surname);

printf("%*d %*d\n",strlen(firstname),strlen(firstname),strlen(surname),strlen(surname));n",f irstname,surname);

printf("%-*d %-*d\n",strlen(firstname),strlen(firstname),strlen(surname),strlen(surname));e\ n",a);

printf("%.12e\n",a);

printf("%.16e\n\n",a);

printf("float b=:\n");

printf("%.4f\n",b);

printf("%.12f\n",b);

printf("%.16f\n\n",b);

printf("FLT_DIG:%d\n",FLT_DIG);

printf("DBL_DIG:%d\n",DBL_DIG);

return(0);

}

7.编写一个程序,要求用户输入行驶的英里数和消耗汽油的加仑数。接着应该计算和显示消耗每加仑汽油行驶的英里数,显示方式是在小数点右侧显示一个数字。然后,根据l加仑约等于升,l英里约等于公里的规则,它应该把每加仑英里数转换成每100公里的升数(欧洲通用的燃料消耗表示法),并显示结果,显示方式是在小数点右侧显示一个数字(请注意,美国方案测量每单位距离消耗的燃料数,而欧洲方案测量每单位燃料的行驶距离)。用符号常量表示两个转换系数(使用const或#define)。

#define GALLON f\n",mile/gallon);

printf("Litres per 100 kilometre:%.1f\n",gallon*GALLON/(100*mile*MILE));

return(0);

}

第5章运算符、表达式和语句

编程练习

1.编写一个程序。将用分钟表示的时间转换成以小时和分钟表示的时间。使用#define或者const 来创建一个代表60的符号常量。使用while循环来允许用户重复键入值,并且当键入一个小于等于0的时间时终止循环。

#include<>

#define HOUR 60

int main(void)

{

int minutes;

printf("Please input the minutes:");

scanf("%d",&minutes);

while(minutes>0)

{

printf("%d hours and %d minutes\n",minutes/HOUR,minutes%HOUR);

printf("Please input the minutes:");

scanf("%d",&minutes);

}

return(0);

}

2.编写一个程序,此程序要求输入一整数,然后打印出从(包括)输入的值到(包括)比输入的值大10的所有整数值(也就是说,如果输入为5,那么输出就从5到15)。要求在各个输出值之间用空格、制表符或换行符分开。

#include<>

int main(void)

{

printf("Please input the number:");

scanf("%d",&num);

while(i++<11)

{

printf("%d ",num++);

}

return(0);

}

3.编写一个程序,该程序要求用户输入天数,然后将该值转换为周数和天数。例如,此程序将把18天转换成2周4天。用下面的格式显示结果:

使用一个while循环让用户重复输入天数;当用户输入一个非正数(如0或-20)时,程序将终止循环。

#include<>

#define WEEK 7

int main(void)

{

int days;

printf("Please input the days:");

scanf("%d",&days);

while(days>0)

{

printf("%d days are %d weeks,%d days.\n",days,days/WEEK,days%WEEK);

printf("Please input the days:");

scanf("%d",&days);

}

return(0);

}

4.编写一个程序让用户按厘米输入一个高度值,然后,程序按照厘米和英尺英寸显示这个高度值。允许厘米和英寸的值出现小数部分。程序允许用户继续输入,直到用户输入一个非正的数值。程序运行的示例如下面所示:

Enter a height in centimeters: 182

cm = 5 feet, inches

Enter a height in centimeters(<=O to quit): 168

cm = 5 feet, inches

Enter a height in centimeters(<=O to quit): 0

bye

#include<>

#define INCH f cm = %d feet, %.1f inches\n",cm,int(cm/INCH/12),cm/INCH-int(cm/INCH/12)*12);

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

scanf("%f",&cm);

}

printf("bye\n");

return(0);

}

5.改写用来找到前20个整数之和的程序(程序清单)(如果您愿意,可以把程序看成是一个计算如果您第一天得到$l,第二天得到$2,第三天得到$3,以此类推,您在20天里会挣多少钱的程序)。修改该程序,目的是您能交q地告诉程序计算将进行到哪里。也就是说,用一个读入的变量来代替20。

#include<>

int main(void)

{

int count,sum,max;

count=0;

sum=0;

printf("Please input the max:");

scanf("%d",&max);

while(count++ < max)

sum = sum + count;

printf("sum = %d\n",sum);

return(0);

}

6.现在修改编程练习5中的程序,使它能够计算整数平方的和(如果您喜欢,可以这样认为:如果您第一天得到$l,第二天得到$4,第三天得到$9,以此类推您将得到多少钱。这看起来像一个很好的买卖)。C没有平方函数,但是您可以利用n的平方是n*n的事实。

#include<>

int main(void)

{

int count,sum,max;

count=0;

sum=0;

printf("Please input the max:");

scanf("%d",&max);

while(count++

sum = sum + count * count;

printf("sum = %d\n",sum);

return(0);

}

7.编写一个程序,该程序要求输入一个float型数并打印该数的立方值。使用您自己设计的函数来计算该值的立方并且将它的立方打印出来。main()程序把输入的值传递给该函数。

#include<>

float cube(float);

int main(void)

{

float number;

printf("Please input the number:");

scanf("%f",&number);

printf("The cube of %f is %f\n",number,cube(number));

return(0);

}

float cube(float num)

{

return(num * num * num);

}

8.编写一个程序,该程序要求用户输入一个华氏温度。程序以double类型读入温度值,并将它作为一个参数传递给用户提供的函数Temperatures()。该函数将计算相应的摄氏温度和绝对温度,并以小数点右边有两位数字的精度显示这三种温度。它应该用每个值所代表的温度刻度来标识这3个值。下面是将华氏温度转换成摄氏温度的方程:

通常用在科学上的绝对温度的刻度是0代表绝对零,是可能温度的下界。下面是将摄氏温度转换为绝对温度的方程:

Kelvin=Celsius+

Temperatures()函数使用const来创建代表该转换里的3个常量的符号。main()函数将使用一个循环来允许用户重复地输入温度,当用户输入q或其他非数字值时,循环结束。

#include<>

void Temperatures(double);

int main(void)

{

double Fahrenheit;

printf("Please input the Fahrenheit:");

while(scanf("%lf",&Fahrenheit) == 1) f-%.2f) / (%.2f*%.2f) = %.2f\n",a,b,a,b,(a-b) / (a*b));

printf("Please input two floats:");

}

printf("end\n");

return(0);

}

8.对练习7进行修改,让它使用一个函数来返回计算值。

#include<>

float calculate(float,float);

int main(void)

{

float a,b;

printf("Please input two floats:");

while(scanf("%f%f",&a,&b) ==2 )

{

printf( "(%.2f-%.2f) / (%.2f*%.2f) = %.2f\n",a,b,a,b,calculate(a,b) );

printf("Please input two floats:");

}

printf("end\n");

return(0);

}

float calculate(float x,float y)

{

return( (x-y) / (x*y) );

}

9.编写一个程序,要求用户输入下限整数和一个上限整数,然后,依次计算从下限到上限的每一个整数的平方的加和,最后显示结果。程序将不断提示用户输入下限整数和上限整数并显示出答案,直到用户输入的上限整数等于或小于下限整数为止。程序运行的结果示例应该如下所示: Enter lower and upper integer limits: 5 9

The sums of the squares from 25 t0 81 is 255

Enter next set of limits:3 25

The sums of the squares from 9 t0 625 iS 5520

Enter next set of limits:5 5

Done

#include<>

int main(void)

{

int lower,upper,sum,i;

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

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

while(lower

{

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

printf("The sums of the squates from %d to %d is %d\n",lower*lower,upper*upper,sum); printf("Enter next set of limits:");

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

}

printf("Done\n");

return(0);

}

10.编写一个程序把8个整数读入一个数组中,然后以相反的顺序打印它们。

#include<>

int main(void)

{

int num[8],i;

printf("Enter 8 numbers:");

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

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

for(i=7;i>=0;i--)

printf("%d\t",num[i]);

printf("\n");

return(0);

}

11.考虑这两个无限序列:

++++…

- + - +…

编写一个程序来计算这两个序列不断变化的总和,直到达到某个次数。让用户交互地输入这个次数。看看在20次、100次和500次之后的总和。是否每个序列都看上去要收敛于某个值?提示:奇数个-1相乘的值为-1,而偶数个-1相乘的值为1。

#include<>

int main(void)

{

double i,sum1,sum2;

int sign,count;

printf("Enter the count:");

scanf("%d",&count);

for(i=,sign=1,sum1=0,sum2=0;i<=count;i++,sign -= sign)

{

sum1 += / i;

sum2 += sign * / i;

}

printf(" + + + ...(%d terms) = %lf\n",count,sum1);

printf(" - + - ...(%d terms) = %lf\n",count,sum2);

return(0);

}

12.编写一个程序,创建一个8个元素的int数组,并且把元素分别设置为2的前8次幂,然后打印出它们的值。使用for循环来设置值;为了变化,使用do while循环来显示这些值。

#include<>

#include<>

int main(void)

{

int num[8],i;

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

num[i] = pow(2,i); 写一个程序,创建两个8元素的double数组,使用一个循环来让用户键入第一个数组的8个元素的值。程序把第二个数组的元素设置为第一个数组元素的累积和。例如,第二个数组

的第4个元素应该等于第一个数组的前4个元素的和,第二个数组的第5个元素应该等于第一个数组的前5个元素的和(使用嵌套循环可以做到这一点。不过利用第二个数组的第5个元素等于第二个数组的第4个元素加上第一个数组的第5个元素这一事实,可以避免嵌套而只使用单个循环来完成这个任务)。最后,使用一个循环来显示两个数组中的内容,第一个数组在一行中显示,而第二个数组中的每个元素在第一个数组的对应元素之下进行显示。

#include<>

int main(void)

{

double num[8],sum[8];

int i,j;

printf("Enter 8 numbers:");

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

{

scanf("%lf",&num[i]);

for(j=0,sum[i]=0;j<=i;j++)

sum[i] += num[j];

}

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

printf("%",num[i]);

printf("\n");

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

printf("%",sum[i]);

printf("\n");

return(0);

}

14.编写一个程序读入一行输入,然后反向打印该行。您可以把输入存储在一个char数组中;假定该行不超过255个字符。回忆一下,您可以使用具有%c说明符的scanf()从输入中一次读入一个字符,而且当您按下回车键时会产生换行字符(\n)。

#include<>

#define LENGTH 20

int main(void)

{

char a[255];

int i;

for(i=0;i

scanf("%c",&a[i]);

for(i=LENGTH-1;i>=0;i--)

printf("%c",a[i]);

printf("\n");

return(0);

}

15. Daphne以10%的单利息投资了100美元(也就是说,每年投资赢得的利息等于原始投资的10%)。Deirdre则以每年5%的复合利息投资了10O美元(也就是说,利息是当前结余的5%,其中包括以前的利息)。编写一个程序,计算需要多少年Deirdre的投资额才会超过Daphne,并且显示出到那时两个人的投资额。

#include<>

int main(void)

{

double Daphne=100,Deirdre=100;

int i=0;

while(Daphne>=Deirdre)

{

Daphne += 100 * ;

Deirdre += Deirdre * ;

i++;

}

printf("After %d years,Deirdre's investment is %lf,Daphne's investment is %lf\n",i,Deirdre,Daphne);

return(0);

}

16. Chuckie Lucky赢了100万美元,他把它存入一个每年赢得8%的帐户。在每年的最后一天,Chuckie取出10万美元。编写一个程序,计算需要多少年Chuckie就会清空他的帐户。

#include<>

int main(void)

{

double investment = 100;

int i=0;

while(investment > 0)

{

investment += investment * ;

investment -= 10;

i++;

}

printf("After %d years,Chuckie Lucky's investment is out\n",i,investment);

return(0);

}

第七章 C控制语句:分支和跳转

编程练习

1.编写一个程序。该程序读取输入直到遇到#字符,然后报告读取的空格数目、读取的换行符数目以及读取的所有其他字符数目。

#include<>

int main(void)

{

int space=0,newline=0,others=0;

char a;

printf("Please input a string end by #:");

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

if(a == ' ') space++;

else if (a == '\n') newline++;

else others++;

printf("space: %d,newline: %d,others: %d\n",space,newline,others);

return(0);

}

2.编写一个程序,该程序读取输入直到遇到#字符。使程序打印每个输入的字符以及它的十进制ASCII码。每行打印8个字符,编码对。建议:利用字符计数和模运算符(%)在每8个循环周期时打印一个换行符。

#include<>

int main(void)

{

char a;

int i;

printf("Please input a string end by #:");

for(i = 1; (a = getchar()) != '#'; i++)

{

printf("%c--%d\t",a,a);

if(i%8 == 0) printf("\n");

}

printf("\n");

return(0);

}

3.编写一个程序。该程序读取整数,直到输入0。输入终止后,程序应该报告输入的偶数(不包括0)总个数、偶数的平均值,输入的奇数总个数以及奇数的平均值。

#include<>

int main(void)

{

c++primerplus中文版第六版源代码

C++ primer plus 中文版第六版源代码 第二章到第四章,后续继续更新……… 第二章 1:#include void main() { using namespace std; int carrots; carrots=25; cout<<"I have "; cout<

2:#include int stonetolb(int); int main() { using namespace std; int stone; cout<<"Enter the weight in stone: "; cin>>stone; int pounds=stonetolb(stone); cout< void main()

{ using namespace std; int carrots; carrots=25; cout<<"How many carrots do you have?"<>carrots; cout<<"Here are two more."; carrots=carrots+2; cout<<"Now you have "< using namespace std; void main() { cout<<"Come up and C++ me some time.";

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

Chapter 2 Programming Exercises PE 2--‐1 /* Programming Exercise 2-1 */ #include <> int main(void) { printf("Gustav Mahler\n"); printf("Gustav\nMahler\n"); printf("Gustav "); printf("Mahler\n"); return 0; } PE 2--‐3 /* Programming Exercise 2-3 */ #include <> int main(void) { int ageyears; /* age in years */ int agedays; /* age in days */ /* large ages may require the long type */ ageyears = 101; agedays = 365 * ageyears; printf("An age of %d years is %d days.\n", ageyears, agedays); return 0; } PE 2--‐4 /* Programming Exercise 2-4 */ #include <> void jolly(void); void deny(void); int main(void) { jolly(); 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"); } PE 2--‐6 /* Programming Exercise 2-6 */ #include <> int main(void) { int toes; toes = 10; printf("toes = %d\n", toes);

C Primer Plus (第六版)中文版 6.16编程练习

//******************6.15复习题************************** //*********** 6 ************************** #include int main(void) { int i, j; for (i = 0; i < 4; i++) //外层循环控制行内层循环控制列 { for (j = 0; j < 8; j++) { printf("$"); } printf("\n"); } return 0; } //******************6.16 编程练习 ************************** //****************** 一 ************************** #include #define SIZE 26 int main(void) { char array[SIZE]; int index = 0; array[0] = 'a'; printf("%c", array[0]); for (index = 1; index < SIZE; index++) { array[index] = 'a' + index; printf("%c", array[index]); } return 0; } //****************** 二 ************************** #include int main(void)

{ int i, j;//i控制行,j控制列计数作用 for (i = 0; i < 5; i++) { for (j = 0; j < =i ; j++) { printf("$"); } printf("\n"); } return 0; } //****************** 三 ************************** #include int main(void) { int i;//外层循环控制行 int j;//内层循环控制列 char ch = 'F'; for (i = 0; i < 6; i++) { for (j = 0; j <= i; j++) printf("%c", ch-j ); printf("\n"); } return 0; } //****************** 四 ************************** #include int main(void) { int i;//外层循环控制行 int j;//内层循环控制列 char ch = 'A'; for (i = 0; i < 6; i++) { for (j = 0; j <= i; j++) printf("%c", ch++ ); printf("\n");

CPrimerPlus第6版中文版勘误表

注意:下面的勘误中,红色字体为修改后的文字,提请各位读者注意。 第 6 页,” 1.6 语言标准”中的第 3 行,将 1987 年修改为 1978 年。 第 22 页,” 2. main ()函数”中的第 1 行, int main (void ) 后面的分号( ; )删除。 第 24 页,“5. 声明”的第 10 行,也就 是一个变量、函数或其他实体的名称。 第 27 页,图 2.3 中,下划线应该只包含括号中的内容;第 2 段的第 4 行,而不是存储 在 源代码 中的指令。 第 30页,“2.5.4 打印多个值”的第 4行,双引 号后面的第 1 个变量。 第 34页,“2.7.3 程序状态”第 2段的第 4 行,要尽量忠实 于代码来模拟。 第 35页,“2.10 本章小结”第 2段的第 1句,声明 语句为变量指定变量名, 并标识该变量中存 储的数据类型;本页倒数第 2 行,即 检查程序每执行一步后所有变量的值。 第37页,“2.12编程练习”中第1题,把你的名和姓打印在一行……把你的 名和姓分别打印在 两行……把你的 名和姓打印在一行……把示例的内容换成你的 名字。 第 40 页,第 1 行,用于把英 磅常衡盎司转换为… … 第44页,“3.4 C 语言基本数据类型”的第 1句,本节将 详细介绍C 语言的基本属性类型…… 第 46页,“5. 八进制和十六进制”的第 4句,十六进制数 3的二进制数 是 0011,十六进制数 5 的二进制数 是 0101;“6. 显示八进制和十六进制”的第 1 句,既可以使用 也可以 显示不同进制 的数;将“回忆一下……程序在执行完毕后不会立即关闭执行窗口”放到一个括号里。 第 47页,“2. 使用多种整数类型的原因”第 3句,过去的一台运行 Windows 3.x 的机器上。 第 53 页,图 3.5 下面的第 4 行“上面最后一个例子( printf ( “ ” a \\ is a backslash. ” \n ” ); )” 第 56页,正文的第 2行和第 4行应该分别为 printf ( “me32 = %““d”“\n ”, me32); printf ( “me32 = %d\n ” , me32); 第 61 页,“无符号类型”的最后 1 句,相当于 unsigned int (即两者之间添加一个空格 )。 第 62 页,程序清单 3.8 中的第 1 行,将 //* typesize.c -- 打印类型大小 */ 中的第一个斜杠删 除。 第 63页,“3.6 参数和陷阱”第 2行, printf ( “ Hello,pal. ” )(即 Hello, 和 pal. 之间没有空 格)。 第 64 页,程序清单 3.10 中的第 1 行,使用 转义序列。 第 75 页,倒数第 8行, 何时使用圆括号 取决于运算对象是类型还是特定量。 第82页,第11行, . 格式字符串包含了两个待打印项 number 和pies 对应的 ..... 第83页,表4.4中的“ L”修饰符的含义介绍中,应该是示例: ” %L ”、“%10.4Le” 第 84 页,表 4.5 中的第 1 行,即,从字段的左侧开始打印该 项(即,应该只保留一个 项);在 “ 0”标记的含义中,添加一行: 示例:"%010d"和"%08.3f"。 第86页,第1段的第2行,……字段宽度是容纳 待打印数字所需的……; 倒数第4段中,根据%x 打印出1f,根据%打印出1F 第87页,“4.4.4转换说明的意义”第 2段,……读者认为原始值 被替换成转换后的值。 第89页,“参数传递”第2行,把变量n1、n2、n3和n4的值传递给程序(即,保留一个顿号)。 第 93页,第 5行的 2121.45 的字体应该与第 4行的 42 的字体保持一致;表 4.6 上面的最后一 行,对于 double 类型要使用 1 修饰符。 第 94 页,表中的第 3 行,把对应的数值存储为 unsigned short int 类型;把“ j ”转换说明的 示例 放到“ z ”转换说明中;在“ j ”转换说明的含义中添加:示例:” %jd”、” %ju”。 第95页,“3.scanf () 的返回值”上面一段的倒数第 3行,如果在格式字符串中把空格放到 %c 的前面 。 第98页,倒数第2段,strlen () 函数(声明在string.h 头文件中)可用于 ... 。 第 100 页,” 4.8 编程练习”中的第 2 题,将该题中的“名和姓”统一替换为“名字” ;并执行 以下 操作;第 3题,将 a 、 b 项中的“输入”替换为” The input is ”,将“或”替换为“ or”, 将末尾1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.

C Primer Plus第6版中文版勘误表教学提纲

C P r i m e r P l u s第6版中文版勘误表

注意:下面的勘误中,红色字体为修改后的文字,提请各位读者注意。 1.第6页,” 1.6语言标准”中的第3行,将1987年修改为1978年。 2.第22页,” 2. main()函数”中的第1行,int main (void)后面的分号(;)删除。 3.第24页,“5. 声明”的第10行,也就是一个变量、函数或其他实体的名称。 4.第27页,图2.3中,下划线应该只包含括号中的内容;第2段的第4行,而不是存储在源代 码中的指令。 5.第30页,“2.5.4 打印多个值”的第4行,双引号后面的第1个变量。 6.第34页,“2. 7.3 程序状态”第2段的第4行,要尽量忠实于代码来模拟。 7.第35页,“2.10 本章小结”第2段的第1句,声明语句为变量指定变量名,并标识该变量中存 储的数据类型;本页倒数第2行,即检查程序每执行一步后所有变量的值。 8.第37页,“2.12 编程练习”中第1题,把你的名和姓打印在一行……把你的名和姓分别打印在 两行……把你的名和姓打印在一行……把示例的内容换成你的名字。 9.第40页,第1行,用于把英磅常衡盎司转换为…… 10.第44页,“3.4 C语言基本数据类型”的第1句,本节将详细介绍C语言的基本属性类型…… 11.第46页,“5. 八进制和十六进制”的第4句,十六进制数3的二进制数是0011,十六进制数5 的二进制数是0101;“6.显示八进制和十六进制”的第1句,既可以使用也可以显示不同进制的数;将“回忆一下……程序在执行完毕后不会立即关闭执行窗口”放到一个括号里。 12.第47页,“2.使用多种整数类型的原因”第3句,过去的一台运行Windows 3.x的机器上。 13.第53页,图 3.5下面的第4行“上面最后一个例子(printf(“Gramps sez, \”a \\ is a backslash.\”\n”);)” 14.第56页,正文的第2行和第4行应该分别为printf(“me32= %“ “d” “\n”, me32); printf(“me32 = %d\n”, me32); 15.第61页,“无符号类型”的最后1句,相当于unsigned int(即两者之间添加一个空格)。 16.第62页,程序清单3.8中的第1行,将//* typesize.c -- 打印类型大小*/中的第一个斜杠删除。 17.第63页,“3.6参数和陷阱”第2行,printf(“Hello,pal.”)(即Hello,和pal.之间没有空格)。 18.第64页,程序清单3.10中的第1行,使用转义序列。 19.第75页,倒数第8行,何时使用圆括号取决于运算对象是类型还是特定量。 20.第82页,第11行,……格式字符串包含了两个待打印项number和pies对应的…… 21.第83页,表4.4中的“L”修饰符的含义介绍中,应该是示例:”%L f”、“%10.4L e” 22.第84页,表4.5中的第1行,即,从字段的左侧开始打印该项(即,应该只保留一个项); 在“0”标记的含义中,添加一行:示例:"%010d"和"%08.3f"。 23.第86页,第1段的第2行,……字段宽度是容纳待打印数字所需的……;倒数第4段中,根 据%x打印出1f,根据%X打印出1F 24.第87页,“4.4.4转换说明的意义”第2段,……读者认为原始值被替换成转换后的值。 25.第89页,“参数传递”第2行,把变量n1、n2、n3和n4的值传递给程序(即,保留一个顿 号)。 26.第93页,第5行的2121.45的字体应该与第4行的42的字体保持一致;表4.6上面的最后一 行,对于double类型要使用1修饰符。 27.第94页,表中的第3行,把对应的数值存储为unsigned short int类型;把“j”转换说明的示例 放到“z”转换说明中;在“j”转换说明的含义中添加:示例:”%jd”、”%ju”。

c++ primer plus(第六版)第二至第六章课后编程练习全部答案

第二章:开始学习C++ //ex2.1--display your name and address #include int main(void) { using namespace std; cout<<"My name is liao chunguang and I live in hunan chenzhou.\n”;} //ex2.2--convert the furlong units to yard uints-把浪单位换位码单位 #include double fur2yd(double); int main() { using namespace std; cout<<"enter the distance measured by furlong units:"; double fur; cin>>fur; cout<<"convert the furlong to yard"< void mice(); void see(); using namespace std; int main() { mice(); mice(); see(); see(); return 0; }

cprimerplus第六版课后编程练习答案

第二章:开始学习C++ n”; } < intmain() { usingnamespacestd; cout<<"Entertheautomobilegasolineconsumptionfigurein\n" <<""; doubleUS_style; cin>>US_style; cout<<"ConvertstoEuropeanstyle(milespergallon):"<

snack[0].calory=200; snack[1].brand="B"; snack[1].weight=; snack[1].calory=400; snack[2].brand="C"; snack[2].weight=; snack[2].calory=500; for(inti=0;i<3;i++) { cout<<"brand:"< intmain() { usingnamespacestd; constintSize=3; intsuccess[Size]; cout<<"Enteryoursuccessofthethreetimes40metersrunning:\n"; cin>>success[0]>>success[1]>>success[2]; cout<<"success1:"< #include intmain() { usingnamespacestd; arrayad={0}; cout<<"Enteryoursuccessofthethreetimes40metersrunning:\n"; cin>>ad[0]>>ad[1]>>ad[2]; cout<<"success1:"<

C primer plus(第五版)课后编程练习答案

第一章概览 编程练习 1.您刚刚被MacroMuscle有限公司(Software for Hard Bodies)聘用。该公司要进入欧洲市场,需要一个将英寸转换为厘米(1英寸=2.54 cm)的程序。他们希望建立的该程序可提示用户输入英寸值。您的工作是定义程序目标并设计该程序(编程过程的第1步和第2步)。 1.将英寸值转化为厘米值 2.显示“输入英寸值”->得到该值->转换为厘米值->存储->告知用户已结束 第二章C语言概述 编程练习 1.编写一个程序,调用printf()函数在一行上输出您的名和姓,再调用一次printf()函数在两个单独的行上输出您的名和姓,然后调用一对printf()函数在一行上输出您的名和姓。输出应如下所示(当然里面要换成您的姓名): Anton Bruckner Anton Bruckner Anton Bruckner 第一个输出语句 第二个输出语句 仍然是第二个输出语句 第三个和第四个输出语句 #include int main(void) { printf("He Jin\n"); printf("He\n"); printf("Jin\n"); printf("He Jin\n"); return(0); }

2.编写一个程序输出您的姓名及地址。 #include int main(void) { printf("Name:He Jin\n"); printf("Address:CAUC\n"); return(0); } 3.编写一个程序,把您的年龄转换成天数并显示二者的值。不用考虑平年( fractional year)和闰年(leapyear)的问题。 #include int main(void) { int age=22; printf("Age:%d\n",age); printf("Day:%d\n",age*356); return(0); } 4.编写一个能够产生下面输出的程序: For he's a jolly good fellow! For he's a jolly good fellow! For he's a jolly good fellow! Which nobody can deny! 程序中除了main()函数之外,要使用两个用户定义的函数:一个用于把上面的夸奖消息输出一次:另一个用于把最后一行输出一次。 #include void printf1(void); void printf2(void); int main(void) { printf1(); printf1(); printf1(); printf2(); return(0);

cprimerplus第六版第五章习题答案

//1 /* #include using namespace std; int main() { cout << "请输入一个较小的整数:"; int min; cin >> min; cout << "请输入一个较大的整数:"; int max; cin >> max; int he=0; for (int i = min; i <= max; i++) he = i + he; cout << "这两个数之间所有数相加后的和为:" << he< #include using namespace std;

const int Arsize = 101; int main() { array aa ; aa[1] = aa[0] = 1; for (int i = 2; i < Arsize; i++) aa[i] = i*aa[i - 1]; for (int i = 0; i < Arsize; i++) cout << i << "!=" << aa[i] << endl; system("pause"); return 0; }*/ //3 /* #include using namespace std; int main() { cout << "请输入一个数字" << endl; int m=0, n; do { cin >> n; m = m + n;

《 C++ Primer Plus (第 6 版)中文版》 勘误表

================================================================= *** 《C++ Primer Plus (第6 版)中文版》勘误表*** 作者:yangyang.gnu 联系:yangyang.gnu@https://www.doczj.com/doc/6c2612655.html, 时间:2013-9-24 ================================================================= P268 错误: free_throws * pt; 修正: free_throws * pt = new free_throws; P291 错误:在这两个模板函数中,recycle(blot *) 被认为是更具体的 修正:在这两个模板函数中,recycle(blot *) 被认为是更具体的 P337 错误: staticconst LIMIT = 25; 修正: staticconst unsigned LIMIT = 25; P386 错误:t4 = t1 + t2 + t3 先转换为t4 = t1.operator+(t2 + t3) 再转换为t4 = t1.operator+(t2.operator+(t3)) 修正:t4 = t1 + t2 + t3 先转换为t4 = t1.operator+(t2) + t3 再转换为t4 = t1.operator+(t2).operator+(t3) P387 错误:.*:成员指针运算符 修正:->:成员指针运算符 P428 错误:String boston("Boston"); 修正:StringBadboston("Boston"); P431 错误:然后程序使用重载运算符>>列出了这些对象 修正:然后程序使用重载运算符<<列出了这些对象 P439 错误:最简单的办法是使用标准的trcmp()函数 修正:最简单的办法是使用标准的strcmp()函数 P440 错误:means.operator[][0] = 'r'; 修正:means.operator[](0) = 'r'; P439 错误:因为内置的>运算符返回的是一个布尔值 修正:因为内置的<运算符返回的是一个布尔值 P478 错误:Cow(const Cow c& ); 修正:Cow(const Cow & c); P478 错误:提供一个Stringlow()成员函数 修正:提供一个stringlow()成员函数

C primer plus(第6版)中文版编程练习答案第15章

1、 //tv.h #ifndef TV_H_ #define TV_H_ #include using namespace std; class Tv { friend class Remote; public: enum { Off, On }; enum { MinVal, MaxVal = 20 }; enum { Antenna, Cable }; enum { TV, DVD }; enum { USUAL, EXCHANGE }; Tv(int s = Off, int mc = 125) :state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV){} ~Tv(){} void onoff(){ state = (state == On) ? Off : On; } bool ison()const{ return state == On; } bool volup(); bool voldown(); void chanup(); void chandown(); void set_mode(){ mode = (mode == Antenna) ? Cable : Antenna; } void set_input(){ input = (input == TV) ? DVD : TV; } void settings()const; void set_rmode(Remote &r); private: int state; int volume; int maxchannel; int channel; int mode; int input; }; class Remote { private:

C PRIMER PLUS 第六章正确答案

Chapter 6 PE 6-1 #include int main (void) { char az[26]; int count = 0; for (az[count] = 'a';az[count] < 'z';az[count] = az[count-1] + 1) count++; for (count = 0; count <= 25; count++) printf("%c ",az[count]); printf("\n"); return 0; }PE 6-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; } }PE 6-3 #include int main (void) { char i,j; for(i='F'; i>='A'; i--) { for(j=’F’; j>=i; j--) printf("%c",j); printf("\n"); } return 0; }

PE 6-4 #include int main (void) { char i,j,z,k,g; for(scanf("%c",&z),i='a'; i <= z; i++) { for(g = i; z - g > 0; g++) printf(" "); for(j= 'a'; j <= i; j++) printf("%c",j); for(k = i; k > 'a'; printf("%c",k)) k--; printf("\n"); } return 0; } PE 6-5 #include int main (void) { int lower, upper, index; int square, cube; printf("Enter starting integer: "); scanf("%d", &lower); printf("Enter ending integer: "); scanf("%d", &upper); printf("%5s %10s %14s\n", "num", "square", "cube"); for(;lower <= upper;lower++) { square = lower*lower; cube = square*lower; printf("%5d%10d%15d\n",lower,square,cube); } return 0; } PE 6-6 #include #include

《C Primer Plus》第六版 第十一章编程练习答案

1 #include #include #define SIZE 100 void input(char *, int ); int main(void) { char arr[SIZE]; int n; puts("input the number of n:"); scanf("%d", &n); getchar(); puts("input your string: "); input(arr, n); printf("%s\n", arr); getchar(); return 0; } void input(char *Arr, int len) { int i; for (i=0; i

#include #define SIZE 100 void input(char *, int ); int main(void) { char arr[SIZE]; int n; puts("input the number of n:"); scanf("%d", &n); getchar(); puts("input your string: "); input(arr, n); puts(arr); getchar(); return 0; } void input(char *Arr, int len) { int i; for (i=0; i #include #define SIZE 100

《C Primer Plus》第六版 第十二章编程练习答案

1. #include int critic(void ); int main(int argc, char *argv[]) { int num=56; int units; printf("How many pounds to a firkin of butter?\n"); scanf("%d",&units); while (units!=num) { units=critic(); } getchar(); return 0; } int critic() { int n; printf("No luck, my friend. Try again.\n"); scanf("%d", &n); return n; } 2. //***********pe12-2a.h*******// #include void set_mode(int mode); void get_info(); void show_info(); //**********pe12-2a.c*************// #include #include"pe12-2a.h"

int mode; float distance, fuel; void set_mode(int m) { if (m !=0 &&m !=1) { printf("Invalid mode specified. Mode 1(US) used.\n"); m=1; } mode=m; } void get_info() { if (0==mode) { printf("Enter distance traveled in kilometers: "); scanf("%f", &distance); printf("Enter fuel consumed in liters: "); scanf("%f", &fuel); } else { printf("Enter distance traveled in miles: "); scanf("%f", &distance); printf("Enter fuel consumed in gallons: "); scanf("%f", &fuel); } } void show_info() { float units; if (0==mode) { units=100* (fuel/distance); printf("Fuel consumed in liters: %.1f per 100 km\n", units); } else { units=distance/fuel; printf("Fuel consumed is %.1f miles per gallon\n", units); } }

C++_Primer_Plus(第六版)编程习题解答

Chapter 2 // pe2-2.cpp #include int main(void) { using namespace std; cout << "Enter a distance in furlongs: "; double furlongs; cin >> furlongs; double feet; feet = 220 * furlongs; cout << furlongs << " furlongs = " << feet << " feet\n"; return 0; } // pe2-3.cpp #include using namespace std; void mice(); void run(); int main() { mice(); mice(); run(); run(); return 0; } void mice() { cout << "Three blind mice\n"; } void run() { cout << "See how they run\n"; } // pe2-4.cpp #include

double C_to_F(double); int main() { using namespace std; cout << "Enter a temperature in Celsius: "; double C; cin >> C; double F; F = C_to_F(C); cout << C << " degrees Celsius = " << F << " degrees Fahrenheit\n"; return 0; } double C_to_F(double temp) { return 1.8 * temp + 32.0; } Chapter 3 // pe3-1.cpp #include const int Inch_Per_Foot = 12; int main(void) { using namespace std; // Note: some environments don't support the backspace character cout << "Please enter your height in inches: ___/b/b/b "; int ht_inch; cin >> ht_inch; int ht_feet = ht_inch / Inch_Per_Foot; int rm_inch = ht_inch % Inch_Per_Foot; cout << "Your height is " << ht_feet << " feet, "; cout << rm_inch << " inch(es).\n"; return 0; } // pe3-3.cpp #include const double MINS_PER_DEG = 60.0; const double SECS_PER_MIN = 60.0; int main() { using namespace std; int degrees; int minutes;

C++ primer plus 第六版 勘误表

P268 错误: free_throws * pt; 修正: free_throws * pt = new free_throws; P291 错误:在这两个模板函数中,recycle(blot *) 被认为是更具体的 修正:在这两个模板函数中,recycle(blot *) 被认为是更具体的 P337 错误: static const LIMIT = 25; 修正: static const unsigned LIMIT = 25; P386 错误:t4 = t1 + t2 + t3 先转为 t4 = t1.operator+(t2 + t3) 再转换为 t4 = t1.operator+(t2.operator+(t3)) 修正:t4 = t1 + t2 + t3 先转为 t4 = t1.operator+(t2) + t3 再转换为 t4 = t1.operator+(t2).operator+(t3) P387 错误:.*:成员指针运算符 修正:->:成员指针运算符 P428 错误:String boston("Boston"); 修正:StringBad boston("Boston"); P431 错误:然后程序使用重载运算符>>列出了这些对象 修正:然后程序使用重载运算符<<列出了这些对象 P439 错误:最简单的办法是使用标准的trcmp()函数 修正:最简单的办法是使用标准的strcmp()函数 P440 错误:means.operator[][0] = 'r'; 修正:means.operator[](0) = 'r'; P439 错误:因为内置的>运算符返回的是一个布尔值 修正:因为内置的<运算符返回的是一个布尔值 P478 错误:Cow(const Cow c& ); 修正:Cow(const Cow & c); P478 错误:提供一个Stringlow()成员函数 修正:提供一个stringlow()成员函数 P478 错误:提供String()成员函数 修正:提供stringup()成员函数P505 错误: 这意味着,即使基类不需要显式析构函数提供服务,也不应该依赖于默认构造函数 修正: 这意味着,即使基类不需要显式析构函数提供服务,也不应该依赖于默认构造析构 P508 错误:半长轴 修正:长半轴 P510 错误:void Move(int nx, ny) = 0 修正:virtual void Move(int nx, ny) = 0 P525 错误: Star::Star double() {...} Star::Star const char * () {...} 修正: Star::operator double() {...} Star::operator const char * () {...} P529 错误:派生类的有元函数 修正:派生类的友元函数 P532 错误:Cd(char * s1, char * s2, int n, double x); 修正:Cd(const char * s1, const char * s2, int n, double x); P532 错误:派生出一个Classic类,并添加一组char成员 修正:派生出一个Classic类,并添加一个char数组成员P532 错误:copy.Report() 修正:copy.Report(); P535 错误:所有元素度被初始化为指定值的数组 修正:所有元素都被初始化为指定值的数组 P544 错误:例如,在类声明中提出可以使用average()函数。和包含一样,要实现这样的目的,可以在公有Student::average()函数中使用私有Student::Average()函数。 修正:例如,对于类Student需要提供的Average()函数,与包含版本一样,私有继承版本同样可以借用valarray的size()和sum()方法来实现。 P549 错误:和私有私有继承一样 修正:和私有继承一样

相关主题
文本预览
相关文档 最新文档