搜档网
当前位置:搜档网 › c++ primer plus(第六版)第二至第六章课后编程练习全部答案

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

c++ primer plus(第六版)第二至第六章课后编程练习全部答案
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"<

double yd;

yd=fur2yd(fur);

cout<

return 0;

}

double fur2yd(double t)

{

return 220*t;

}

//ex2.3-每个函数都被调用两次

#include

void mice();

void see();

using namespace std;

int main()

{

mice();

mice();

see();

see();

return 0;

}

void mice()

{

cout<<"three blind mice"<

}

void see()

{

cout<<"see how they run"<

}

//ex2.4

#include

int main()

{

using namespace std;

cout<<"Enter your age:";

int age;

cin>>age;

int month;

month=age*12;

cout<

return 0;

}

//ex2.5---convert the Celsius valve to Fahrenheit value

#include

double C2F(double);

int main()

{

using namespace std;

cout<<"please enter a Celsius value:";

double C;

cin>>C;

double F;

F=C2F(C);

cout<

}

double C2F(double t)

{

return 1.8*t+32;

}

//ex2.6---convert the light years valve to astronomical units--把光年转换为天文单位#include

double convert(double);//函数原型

int main()

{

using namespace std;

cout<<"Enter the number of light years:";

double light_years;

cin>>light_years;

double astro_units;

astro_units=convert(light_years);

cout<

}

double convert(double t)

{

return 63240*t;//1 光年=63240 天文单位

}

//ex2.7--显示用户输入的小时数和分钟数

#include

void show();

main()

{

using namespace std;

show();

return 0;

}

void show()

{

using namespace std;

int h,m;

cout<<"enter the number of hours:";

cin>>h;

cout<<"enter the number of minutes:";

cin>>m;

cout<<"Time:"<

}

第三章:处理数据

//ex3.1—将身高用英尺(feet)和英寸(inch)表示

#include

const int inch_per_feet=12;// const常量--1feet=12inches--1英尺=12英寸

int main()

{

using namespace std;

cout<<"please enter your height in inches:___\b\b\b";// \b表示为退格字符int ht_inch;

cin>>ht_inch;

int ht_feet=ht_inch/inch_per_feet;//取商

int rm_inch=ht_inch%inch_per_feet;//取余

cout<<"your height is "<

<

return 0;

}

//ex3.2--计算相应的body mass index(体重指数)

#include

const int inch_per_feet=12;

const double meter_per_inch=0.0254;

const double pound_per_kilogram=2.2;

int main()

{

using namespace std;

cout<<"Please enter your height:"<

cout<<"First,enter your height of feet part(输入你身高的英尺部分):_\b";

int ht_feet;

cin>>ht_feet;

cout<<"Second,enter your height of inch part(输入你身高的英寸部分):_\b";

int ht_inch;

cin>>ht_inch;

cout<<"Now,please enter your weight in pound:___\b\b\b";

double wt_pound;

cin>>wt_pound;

int inch;

inch=ht_feet*inch_per_feet+ht_inch;

double ht_meter;

ht_meter=inch*meter_per_inch;

double wt_kilogram;

wt_kilogram=wt_pound/pound_per_kilogram;

cout<

cout<<"Your pensonal body information as follows:"<

cout<<"身高:"<

<<"体重:"<

double BMI;

BMI=wt_kilogram/(ht_meter*ht_meter);

cout<<"your Body Mass Index(体重指数) is "<

return 0;

}

//ex3.3 以度,分,秒输入,以度输出

#include

const int minutes_per_degree=60;

const int seconds_per_minute=60;

int main()

{

using namespace std;

cout<<"Enter a latitude in degrees,minutes,and seconds:\n";

cout<<"First,enter the degrees:";

int degree;

cin>>degree;

cout<<"Next,enter the minutes of arc:";

int minute;

cin>>minute;

cout<<"Fianlly,enter the seconds of arc:";

int second;

cin>>second;

double show_in_degree;

show_in_degree=(double)degree+(double)minute/minutes_per_degree+(double)second/mi nutes_per_degree/seconds_per_minute;

cout<

return 0;

}

//ex3.4

#include

const int hours_per_day=24;

const int minutes_per_hour=60;

const int seconds_per_minute=60;

int main()

{

using namespace std;

cout<<"Enter the number of seconds:";

long seconds;

cin>>seconds;

int Day,Hour,Minute,Second;

Day=seconds/seconds_per_minute/minutes_per_hour/hours_per_day;

Hour=seconds/seconds_per_minute/minutes_per_hour%hours_per_day;

Minute=seconds/seconds_per_minute%minutes_per_hour;

Second=seconds%seconds_per_minute;

cout<

return 0;

}

//ex3.5

#include

int main()

{

using namespace std;

cout<<"Enter the world population:";

long long world_population;

cin>>world_population;

cout<<"Enter the population of the US:";

long long US_population;

cin>>US_population;

double percentage;

percentage=(double)US_population/world_population*100;

cout<<"The population of the US is "<

return 0;

}

//ex3.6 汽车耗油量-美国(mpg)or欧洲风格(L/100Km)

#include

int main()

{

using namespace std;

cout<<"Enter the miles of distance you have driven:";

double m_distance;

cin>>m_distance;

cout<<"Enter the gallons of gasoline you have used:";

double m_gasoline;

cin>>m_gasoline;

cout<<"Your car can run "<

cout<<"Computing by European style:\n";

cout<<"Enter the distance in kilometers:";

double k_distance;

cin>>k_distance;

cout<<"Enter the petrol in liters:";

double k_gasoline;

cin>>k_gasoline;

cout<<"In European style:"<<"your can used "<<100*k_gasoline/k_distance<<" liters of petrol per 100 kilometers\n";

return 0;

}

//ex3.7 automobile gasoline consumption-耗油量--欧洲风格(L/100Km)转换成美国风格(mpg) #include

int main()

{

using namespace std;

cout<<"Enter the automobile gasoline consumption figure in\n"

<<"European style(liters per 100 kilometers):";

double Euro_style;

cin>>Euro_style;

cout<<"Converts to U.S. style(miles per gallon):"<

cout<

return 0;

}

// Note that 100 kilometers is 62.14 miles, and 1 gallon is 3.875 liters.

//Thus, 19 mpg is about 12.4 L/100Km, and 27 mpg is about 8.7 L/100Km.

Enter the automobile gasoline consumption figure in

European style(liters per 100 kilometers):12.4

Converts to U.S. style(miles per gallon):

12.4 L/100Km = 19.4187 mpg

Press any key to continue

// ex3.7 automobile gasoline consumption-耗油量--美国风格(mpg)转换成欧洲风格(L/100Km) #include

int main()

{

using namespace std;

cout<<"Enter the automobile gasoline consumption figure in\n"

<<"U.S. style(miles per gallon):";

double US_style;

cin>>US_style;

cout<<"Converts to European style(miles per gallon):"<

cout<

return 0;

}

// Enter the automobile gasoline consumption figure in

U.S. style(miles per gallon):19

Converts to European style(miles per gallon):

19 mpg = 12.6733L/100Km

Press any key to continue

第四章复合类型

//ex4.1 display the information of student

#include

const int Asize=20;

using namespace std;

struct student//定义结构描述

{

char firstname[Asize];

char lastname[Asize];

char grade;

int age;

};

void display(student);//函数原型放在结构描述后

int main()

{

cout<<"what is your first name?"<

student lcg;//创建结构变量(结构数据对象)

cin.getline(lcg.firstname,Asize);

cout<<"what is your last name?"<

cin.getline(https://www.sodocs.net/doc/432143954.html,stname,Asize);

cout<<"what letter grade do you deserve?"<

cin>>lcg.grade;

cout<<"what is your age?"<

cin>>lcg.age;

display(lcg);

return 0;

}

void display(student name)

{

cout<<"Name: "<

cout<<"Grade:"<

cout<<"Age:"<

}

//ex4.2 use the string-class instead of char-array

#include

#include

int main()

{

using namespace std;

string name,dessert;

cout<<"Enter your name: \n";

getline(cin,name);

cout<<"Enter your favorite dessert: \n";

getline(cin,dessert);

cout<<"I have some delicious "<

cout<<" for you, "<

return 0;

}

//有时候会遇到需要按下两次回车键才能正确的显示结果,这是vc++6.0的一个BUG,更改如下:else if (_Tr::eq((_E)_C, _D))

{_Chg = true;

_I.rdbuf()->sbumpc();//修改后的

break; }

ex4.3 输入其名和姓,并组合显示

#include

#include

const int Asize=20;

int main()

{

using namespace std;

char fname[Asize];

char lname[Asize];

char fullname[2*Asize+1];

cout<<"Enter your first name:";//输入名字,存储在fname[]数组中

cin.getline(fname,Asize);

cout<<"Enter your last name:";//输入姓,存储在lname[]数组中

cin.getline(lname,Asize);

strncpy(fullname,lname,Asize);//把姓lname复制到fullname空数组中

strcat(fullname,", ");//把“,”附加到上述fullname尾部

strncat(fullname,fname,Asize);//把fname名字附加到上述fullname尾部

fullname[2*Asize]='\0';//为防止字符型数组溢出,在数组结尾添加结束符

cout<<"Here's the information in a single string:"<

return 0;

}

//ex4.4 使用string对象存储、显示组合结果

#include

#include

int main()

{

using namespace std;

string fname,lname,attach,fullname;

cout<<"Enter your first name:";

getline(cin,fname);//note:将一行输入读取到string类对象中使用的是getline(cin,str)

//它没有使用句点表示法,所以不是类方法cout<<"Enter your last name:";

getline(cin,lname);

attach=", ";

fullname=lname+attach+fname;

cout<<"Here's the information in a single string:"<

return 0;

}

//ex4.5 declare a struct and initialize it 声明结果并创建一个变量

#include

const int Asize=20;

struct CandyBar

{

char brand[Asize];

double weight;

int calory;

};

int main()

{

using namespace std;

CandyBar snack={"Mocha Munch",2.3,350};

cout<<"Here's the information of snack:\n";

cout<<"brand:"<

cout<<"weight:"<

cout<<"calory:"<

return 0;

}

//ex4.6 结构数组的声明及初始化

#include

const int Asize=20;

struct CandyBar

{

char brand[Asize];

double weight;

int calory;

};

int main()

{

using namespace std;

CandyBar snack[3]={

{"Mocha Munch",2.3,350},

{"XuFuJi",1.1,300},

{"Alps",0.4,100}

};

for(int i=0;i<3;i++)//利用for循环来显示snack变量的内容{

cout<

<

<

}

return 0;

}

//ex4.7 pizza披萨饼

#include

#include

const int Size=20;

struct pizza//声明结构

{

char company[Size];

double diameter;

double weight;

};

int main()

{

using namespace std;

pizza pie;//创建一个名为pie的结构变量

cout<<"What's the name of pizza company:";

cin.getline(https://www.sodocs.net/doc/432143954.html,pany,Size);

cout<<"What's the diameter of pizza:";

cin>>pie.diameter;

cout<<"What's the weight of pizza:";

cin>>pie.weight;

cout<<"company:"<

cout<<"diameter:"<

cout<<"weight:"<

return 0;

}

//ex4.8 pizza pie 披萨饼使用new创建动态结构

#include

#include

const int Size=20;

struct pizza//声明结构

{

char company[Size];

double diameter;

double weight;

};

int main()

{

using namespace std;

pizza *pie=new pizza;//使用new创建动态结构

cout<<"What's the diameter of pizza:";

cin>>pie->diameter;

cin.get();//读取下一个字符

cout<<"What's the name of pizza company:";

cin.get(pie->company,Size);

cout<<"What's the weight of pizza:";

cin>>pie->weight;

cout<<"diameter:"<diameter<<" inches"<

cout<<"company:"<company<

cout<<"weight:"<weight<<" ounches"<

delete pie;//delete释放内存

return 0;

}

//ex.4.9 使用new动态分配数组—方法1

#include

#include

using namespace std;

struct CandyBar

{

string brand;

double weight;

int calory;

};

int main()

{

CandyBar *snack= new CandyBar[3];

snack[0].brand="A";//单个初始化由new动态分配的内存snack[0].weight=1.1;

snack[0].calory=200;

snack[1].brand="B";

snack[1].weight=2.2;

snack[1].calory=400;

snack[2].brand="C";

snack[2].weight=4.4;

snack[2].calory=500;

for(int i=0;i<3;i++)

{

cout << " brand: " << snack[i].brand << endl;

cout << " weight: " << snack[i].weight << endl;

cout << " calorie: " << snack[i].calory << endl<

}

delete [] snack;

return 0;

}

//ex.4.10 数组—方法1

#include

int main()

{

using namespace std;

const int Size = 3;

int success[Size];

cout<<"Enter your success of the three times 40 meters running:\n";

cin >> success[0]>>success[1]>>success[2];

cout<<"success1:"<

cout<<"success2:"<

cout<<"success3:"<

double average=(success[0]+success[1]+success[2])/3;

cout<<"average:"<

return 0;

}

//ex.4.10 array—方法2

#include

#include

int main()

{

using namespace std;

arrayad={0};

cout<<"Enter your success of the three times 40 meters running:\n";

cin >> ad[0]>>ad[1]>>ad[2];

cout<<"success1:"<

cout<<"success2:"<

cout<<"success3:"<

ad[3]=(ad[0]+ad[1]+ad[2])/3;

cout<<"average:"<

return 0;

}

第五章循环和关系表达式

//ex.5.1

#include

int main()

{

using namespace std;

cout<<"Please enter two integers: ";

int num1,num2;

cin>>num1>>num2;

int sum=0;

for(int temp=num1;temp<=num2;++temp)//or temp++

sum+=temp;

cout<<"The sum from "<

}

//ex.5.2

#include

#include

int main()

{

using namespace std;

arrayad={0};

ad[1]=ad[0]=1L;

for(int i=2;i<101;i++)

ad[i]=i*ad[i-1];

for(int i=0;i<101;i++)

cout<

return 0;

}

//ex.5.3

#include

int main()

{

using namespace std;

cout<<"Please enter an integer: ";

int sum=0,num;

while((cin>>num)&&num!=0)

{

sum+=num;

cout<<"So far, the sum is "<

cout<<"Please enter an integer: ";

}

return 0;

}

//ex.5.4

#include

int main()

{

using namespace std;

double sum1,sum2;

sum1=sum2=0.0;

int year=0;

while(sum2<=sum1)

{

++year;

sum1+=10;

sum2=(100+sum2)*0.05+sum2;

}

cout<<"经过"<

cout<<"此时,Cleo的投资价值为"<

return 0;

}

//ex.5.5

#include

const int MONTHS = 12;

const char* months[MONTHS]={"January","February","March","April","May","June","July","August","Sept ember","October","November","December"};

int main()

{

using namespace std;

int sales[MONTHS],sum=0;

for(int i=0;i

{

cout<<"请输入在"<

cin>>sales[i];

sum+=sales[i];

}

cout<<"这一年中的C++ For Fools的总销售量为:"<

return 0;

}

//ex.5.6

#include

const int MONTHS = 12;

const char* months[MONTHS]={"January","February","March","April","May","June","July","August","Sept ember","October","November","December"};

const char* years[3]={"第一年","第二年","第三年"};

int main()

{

using namespace std;

int year_sale[3],sum=0,sales[3][MONTHS];

for(int i=0;i<3;i++)

{

int temp=0;

cout<

for(int j=0;j

{

cout<<"请输入"<

cin>>sales[i][j];

temp+=sales[i][j];

}

year_sale[i]=temp;

sum+=year_sale[i];

}

for(int i=0;i<3;i++)

cout<

return 0;

}

//ex.5.7

#include

#include

using namespace std;

struct car{

string name;

int year;

};

int main()

{

cout<<"How many cars do you wish to catalog? ";

int num;

(cin>>num).get();

car* ps=new car[num];

for(int i=0;i

{

cout<<"Car #"<

cout<<"Please enter the make: ";

getline(cin,ps[i].name);

cout<<"Please enter the year made: ";

(cin>>ps[i].year).get();

}

cout<<"Here is your collection:\n";

for(int i=0;i

cout<

delete [] ps;

return 0;

}

//ex.5.8

#include

#include

int main()

{

using namespace std;

char word[20];

int sum=0;

cout<<"Enter words (to stop,type the word done):\n"; cin>>word;

while(strcmp(word,"done"))

{

sum++;

cin>>word;

}

cout<<"You entered a total of "<

return 0;

}

//ex.5.9

#include

#include

int main()

{

using namespace std;

string word;

int sum=0;

cout<<"Enter words (to stop, type the word done):\n"; cin>>word;

while(word!="done")

{

sum++;

cin>>word;

}

cout<<"You entered a total of "<

return 0;

}

//ex.5.10

#include

int main()

{

using namespace std;

cout<<"Enter number of rows:";

int num;

cin>>num;

for(int i=0;i

{

for(int j=num-i;j>1;j--)

cout<<".";

for(int k=0;k<=i;++k)

cout<<"*";

cout<

}

return 0;

}

第六章分支语句和逻辑运算符//ex.6.1

#include

#include

int main()

{

using namespace std;

char ch;

cin.get(ch);

while(ch!='@')

{

if(isdigit(ch))

cin.get(ch);

else

{

if(islower(ch))

ch=toupper(ch);

else

ch=tolower(ch);

cout<

cin.get(ch);

}

}

return 0;

}

//ex.6.2--数组

#include

#include

int main()

{

using namespace std;

double sum=0,average=0;

double num[10];

int i=0,total=0;

double temp;

while(cin>>temp&&i<10&&!isdigit(temp))

{

num[i]=temp;

sum+=num[i];

++i;

}

if(i!=0)

average=sum/i;

for(int j=0;j

if(num[j]>average)

++total;

cout<<"这些数字的平均值为"<

cout<<"并且共有"<

return 0;

}

//ex.6.2--array

#include

#include

#include

int main()

{

using namespace std;

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.sodocs.net/doc/432143954.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 错误:和私有私有继承一样 修正:和私有继承一样

相关主题