Chapter 2 Programming Exercises
PE 2---1
/* Programming Exercise 2-1 */
#include
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
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
main(void)
{ int toes;
toes = 10;
printf("toes = %d\n", toes);
printf("Twice toes = %d\n", 2 * toes);
printf("toes squared = %d\n", toes * toes); return
0;
}
/* or create two more variables, set them to 2 * toes and toes * toes */ PE 2---8
/* Programming Exercise 2-8 */
#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");
}
Chapter 3 Programming Exercises
PE 3---2
/* Programming Exercise 3-2 */
#include
main(void)
{
int ascii;
printf("Enter an ASCII code: ");
scanf("%d", &ascii);
printf("%d is the ASCII code for %c.\n", ascii, ascii);
return 0;
}
PE 3---4
/* Programming Exercise 3-4 */
#include
main(void)
{ float
num;
printf("Enter a floating-point value: ");
scanf("%f", &num);
printf("fixed-point notation: %f\n", num);
printf("exponential notation: %e\n", num);
printf("p notation: %a\n", num); return 0;
}
PE 3---6
/* Programming Exercise 3-6 */
#include
main(void)
{
float mass_mol = 3.0e-23; /* mass of water molecule in grams */
float mass_qt = 950; /* mass of quart of water in grams */
float quarts; float molecules;
printf("Enter the number of quarts of water: ");
scanf("%f", &quarts);
molecules = quarts * mass_qt / mass_mol;
printf("%f quarts of water contain %e molecules.\n", quarts, molecules); return 0;
}
Chapter 4 Programming Exercises
PE 4---1
/* Programming Exercise 4-1 */
#include
main(void)
{ char
fname[40]; char
lname[40];
printf("Enter your first name: ");
scanf("%s", fname); printf("Enter
your last name: "); scanf("%s",
lname); printf("%s, %s\n", lname,
fname); return 0;
}
PE 4---4
/* Programming Exercise 4-4 */
#include
main(void)
{ float height;
char name[40];
printf("Enter your height in inches: ");
scanf("%f", &height); printf("Enter your
name: "); scanf("%s", name);
printf("%s, you are %.3f feet tall\n", name, height / 12.0);
return 0;
}
PE 4---7
/* Programming Exercise 4-7 */
#include
#include
main(void)
{ float ot_f = 1.0 / 3.0;
double ot_d = 1.0 / 3.0;
printf(" float values: ");
printf("%.4f %.12f %.16f\n", ot_f, ot_f, ot_f);
printf("double values: ");
printf("%.4f %.12f %.16f\n", ot_d, ot_d, ot_d);
printf("FLT_DIG: %d\n", FLT_DIG);
printf("DBL_DIG: %d\n", DBL_DIG); return 0;
}
Chapter 5 Programming Exercises
PE 5---1
/* Programming Exercise 5-1 */
#include
main(void)
{ const int minperhour =
60; int minutes, hours,
mins;
printf("Enter the number of minutes to convert: ");
scanf("%d", &minutes); while (minutes > 0 )
{ hours = minutes /
minperhour; mins = minutes %
minperhour;
printf("%d minutes = %d hours, %d minutes\n", minutes, hours, mins); printf("Enter next minutes value (0 to quit): "); scanf("%d",
&minutes);
}
printf("Bye\n");
return 0;
}
PE 5---3
/* Programming Exercise 5-3 */
#include
main(void)
{ const int daysperweek =
7; int days, weeks,
day_rem;
printf("Enter the number of days: ");
scanf("%d", &days); while (days > 0)
{ weeks = days /
daysperweek; day_rem = days %
daysperweek;
printf("%d days are %d weeks and %d days.\n",
days, weeks, day_rem);
printf("Enter the number of days (0 or less to end): ");
scanf("%d", &days);
}
printf("Done!\n");
return 0;
}
PE 5---5
/* Programming Exercise 5-5 */ #include
int main(void) /* finds sum of first n integers */ {
int count, sum;
int n;
printf("Enter the upper limit: ");
scanf("%d", &n); count = 0;
sum = 0; while
(count++ < n) sum = sum + count;
printf("sum = %d\n", sum); return
0;
}
PE 5---7
/* Programming Exercise 5-7 */
#include
showCube(double x);
int main(void) /* finds cube of entered number */ { double
val;
printf("Enter a floating-point value: "); scanf("%lf", &val); showCube(val); return 0; }
void showCube(double x)
{
printf("The cube of %e is %e.\n", x, x*x*x );
}
Chapter 6 Programming Exercises
PE 6---1
/* pe6-1.c */
/* this implementation assumes the character codes */ /* are sequential, as they are in ASCII. */ #include
26 int main( void ) { char
lcase[SIZE]; int i;
for (i = 0; i < SIZE; i++)
lcase[i] = 'a' + i; for (i
= 0; i < SIZE; i++)
printf("%c", lcase[i]);
printf("\n");
return 0;
}
PE 6---3
/* pe6-3.c */
/* this implementation assumes the character codes */ /* are sequential, as they are in ASCII. */
C++ primer plus 中文版第六版源代码 第二章到第四章,后续继续更新……… 第二章 1:#include 2:#include { using namespace std; int carrots; carrots=25; cout<<"How many carrots do you have?"< 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); //******************6.15复习题************************** //*********** 6 ************************** #include { int i, j;//i控制行,j控制列计数作用 for (i = 0; i < 5; i++) { for (j = 0; j < =i ; j++) { printf("$"); } printf("\n"); } return 0; } //****************** 三 ************************** #include 注意:下面的勘误中,红色字体为修改后的文字,提请各位读者注意。 第 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 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++ //ex2.1--display your name and address #include 第二章:开始学习C++ n”; } < 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:"< 第一章概览 编程练习 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 2.编写一个程序输出您的姓名及地址。 #include //1 /* #include const int Arsize = 101; int main() { array ================================================================= *** 《C++ Primer Plus (第6 版)中文版》勘误表*** 作者:yangyang.gnu 联系:yangyang.gnu@https://www.doczj.com/doc/3e3838635.html, 时间:2013-9-24 ================================================================= P268 错误: free_throws * pt; 修正: free_throws * pt = new free_throws; P291 错误:在这两个模板函数中,recycle 1、 //tv.h #ifndef TV_H_ #define TV_H_ #include Chapter 6 PE 6-1 #include PE 6-4 #include 1 #include #include 1. #include 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); } } Chapter 2 // pe2-2.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 P268 错误: free_throws * pt; 修正: free_throws * pt = new free_throws; P291 错误:在这两个模板函数中,recycleC Primer Plus第6版编程练习答案
C Primer Plus (第六版)中文版 6.16编程练习
CPrimerPlus第6版中文版勘误表
C Primer Plus第6版中文版勘误表教学提纲
c++ primer plus(第六版)第二至第六章课后编程练习全部答案
cprimerplus第六版课后编程练习答案
C primer plus(第五版)课后编程练习答案
cprimerplus第六版第五章习题答案
《 C++ Primer Plus (第 6 版)中文版》 勘误表
C primer plus(第6版)中文版编程练习答案第15章
C PRIMER PLUS 第六章正确答案
《C Primer Plus》第六版 第十一章编程练习答案
《C Primer Plus》第六版 第十二章编程练习答案
C++_Primer_Plus(第六版)编程习题解答
C++ primer plus 第六版 勘误表