当前位置:文档之家› C++ Primer Plus(6) 第八章 程序清单

C++ Primer Plus(6) 第八章 程序清单

C++ Primer Plus(6) 第八章 程序清单
C++ Primer Plus(6) 第八章 程序清单

8.1 inline.cpp

#include

inline double square(double x)

{

return x*x;

}

int main()

{

using namespace std;

double a,b;

double c=3.0;

a=square(5.0);

b=square(4.5+7.5);

cout<<"a="<

cout<<"c="<

cout<<",c aqured="<

cout<<"Now c="<

return 0;

}

8.2 firstref.cpp

#include

int main()

{

using namespace std;

int rats=101;

int& rodents=rats;

cout<<"rats="<

cout<<",rodents="<

rodents++;

cout<<"rats="<

cout<<",rodents="<

cout<<"rats address="<<&rats;

cout<<",rodents address="<<&rodents;

return 0;

}

8.3 sceref.cpp

#include

int main()

{

using namespace std;

int rats=101;

int& rodents=rats;

cout<<"rats="<

cout<<",rodents="<

cout<<"rats address="<<&rats;

cout<<",rodents address="<<&rodents<

int bunnies=50;

rodents=bunnies;

cout<<"bunnies="<

cout<<",rats="<

cout<<",rodents="<

cout<<"bunnies address="<<&bunnies;

cout<<",rodents address="<<&rodents<

return 0;

}

8.4 swaps.cpp

#include

void swapr(int& a, int& b);

void swapp(int *p,int *q);

void swapv(int a,int b);

int main()

{

using namespace std;

int wallet1=300;

int wallet2=350;

cout<<"wallet1=$"<

cout<<", wallet2=$"<

cout<<"Using references to swap contents:"<

swapr(wallet1,wallet2);

cout<<"wallet1=$"<

cout<<", wallet2=$"<

cout<<"Using pointers to swap contents again:"<

swapp(&wallet1,&wallet2);

cout<<"wallet1=$"<

cout<<", wallet2=$"<

cout<<"Trying to use passing by value:"<

swapv(wallet1,wallet2);

cout<<"wallet1=$"<

cout<<"wallet2=$"<

return 0;

}

void swapr(int& a,int& b)

{

int temp;

temp=a;

a=b;

b=temp;

}

void swapp(int *p,int *q)

{

int temp;

temp=*p;

*p=*q;

*q=temp;

}

void swapv(int a,int b)

{

int temp;

temp=a;

a=b;

b=temp;

}

8.5 cubes.cpp

#include

double cube(double a);

double refcube(double &ra);

int main()

{

using namespace std;

double x=3.0;

cout<

cout<<"=cube of "<

cout<

cout<<"=cube of "<

return 0;

}

double cube(double a)

{

a *=a*a;

return a;

}

double refcube(double &ra)

{

ra *=ra*ra;

return ra;

}

8.6 strtref.cpp

#include

#include

struct free_throws

{

std::string name;

int made;

int attempts;

float percent;

};

void display(const free_throws &ft);

void set_pc(free_throws &ft);

free_throws &accumulate(free_throws &target,const free_throws &source); int main()

{

free_throws one={"Ifelsa Branch",13,14};

free_throws two={"Andor Knott",10,16};

free_throws three={"Minnie Max",7,9};

free_throws four={"Whily Looper",5,9};

free_throws five={"Long Long",6,14};

free_throws team={"Throwgoods",0,0};

free_throws dup;

set_pc(one);

display(one);

accumulate(team,one);

display(team);

display(accumulate(team,two));

accumulate(accumulate(team,three),four);

display(team);

dup=accumulate(team,five);

std::cout<<"Displaying team:"<

display(team);

std::cout<<"Displaying dup after assignment:"<

display(dup);

set_pc(four);

accumulate(dup,five)=four;

std::cout<<"Displaying dup after ill-advised assignment:"<

display(dup);

return 0;

}

void display(const free_throws &ft)

{

using std::cout;

cout<<"Name:"<

cout<<" Made:"<

cout<<"Attempts:"<

cout<<"Percent:"<

}

void set_pc(free_throws &ft)

{

if(ft.attempts!=0)

ft.percent=100.0f*float(ft.made)/float(ft.attempts);

else

ft.percent=0;

}

free_throws &accumulate(free_throws &target,const free_throws &source) {

target.attempts+=source.attempts;

target.made+=source.made;

set_pc(target);

return target;

}

8.7 strquote.cpp

#include

#include

using namespace std;

string version1(const string &s1,const string &s2);

const string &version2(string &s1,const string &s2);

const string &version3(string &s1,const string &s2);

int main()

{

string input;

string copy;

string result;

cout<<"Enter a string: ";

getline(cin,input);

copy=input;

cout<<"Your string as entered: "<

result=version1(input,"***");

cout<<"Your string enhanced: "<

cout<<"Your original string: "<

result=version2(input,"###");

cout<<"Your string enhanced: "<

cout<<"Your original string: "<

cout<<"Resetting original string."<

input=copy;

result=version3(input,"@@@");

cout<<"Your string enhanced: "<

cout<<"Your original string: "<

return 0;

}

string version1(const string &s1,const string &s2)

{

string temp;

temp=s2+s1+s2;

return temp;//可用因为按值时,要先把temp的内容放在一个临时存储单元中,temp被释放,也不影响}

const string &version2(string &s1,const string &s2)

{

s1=s2+s1+s2;

return s1;

}

const string &version3(string &s1,const string &s2)

{

string temp;

temp=s2+s1+s2;

return temp; //不可用,因为用引用的话,是直接使用?temp,但是temp在函数调结束时已经被释放了}

8.8 filefunc.app

#include

#include

#include

using namespace std;

void file_it(ostream &os,double fo,const double fe[],int n);

const int LIMIT=5;

int main()

{

ofstream fout;

const char *fn="ep-data.txt";

fout.open(fn);

if(!fout.is_open())

{

cout<<"Can't open "<

exit(EXIT_FAILURE);

}

double objective;

cout<<"Enter the focal length of your telescope objective in mm:";

cin>>objective;

double eps[LIMIT];

cout<<"Enter the focal lengths,in mm,of "<

for(int i=0;i

{

cout<<"Eyepiece #"<

cin>>eps[i];

}

file_it(fout,objective,eps,LIMIT);

file_it(cout,objective,eps,LIMIT);

cout<<"Done"<

return 0;

}

void file_it(ostream &os,double fo,const double fe[],int n)

{

ios_base::fmtflags initial;

initial=os.setf(ios_base::fixed);

os.precision(0);

os<<"Focal length of objective: "<

os.setf(ios::showpoint);

os.precision(1);

os.width(12);

os<<"f.l. eyepiece";

os.width(15);

os<<"magnification"<

for(int i=0;i

{

os.width(12);

os<

os.width(15);

os<

}

os.setf(initial);

}

8.9 left.cpp

#include

const int ArSize=80;

char *left(const char *str,int n=1);

int main()

{

using namespace std;

char sample[ArSize];

cout<<"Enter a string:"<

cin.get(sample,ArSize);

char *ps=left(sample,4);

cout<

delete [] ps;//free old string

ps=left(sample);

cout<

delete [] ps;

return 0;

}

char *left(const char *str,int n)

{

if(n<0)

n=0;

char *p=new char[n+1];

int i;

for(i=0;i

p[i]=str[i];

while(i<=n)

p[i++]='\0';

return p;

}

8.10 leftover.cpp

#include

unsigned long left(unsigned long num,unsigned ct); char *left(const char *str,int n=1);

int main()

{

using namespace std;

char *trip="Havaii!!";

unsigned long n=12345678;

int i;

char *temp;

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

{

cout<

temp=left(trip,i);

cout<

delete [] temp;

}

return 0;

}

unsigned long left(unsigned long num,unsigned ct) {

unsigned digits=1;

unsigned long n=num;

if(ct==0||num==0)

return 0;

while(n/=10)

digits++;

if(digits>ct)

{

ct=digits-ct;

while(ct--)

num/=10;

return num;

}

else

return num;

}

char *left(const char *str,int n)

{

if(n<0)

n=0;

char *p=new char[n+1];

int i;

for(i=0;i

p[i]=str[i];

while(i<=n)

p[i++]='\0';

return p;

}

8.11 funtemp.cpp

#include

template //T可以作为函数变量类型和参数变量类型void Swap(T &a,T &b);

int main()

{

using namespace std;

int i=10;

int j=20;

cout<<"i,j="<

cout<<"Using compiler-generated int swapper:"<

Swap(i,j);

cout<<"Now i,j="<

char x='A';

char y='B';

cout<<"x,y="<

cout<<"Using compilter-generated double swapper:"<

Swap(x,y);

cout<<"Now x,y="<

return 0;

}

template

void Swap(T &a,T &b)

{

T temp;

temp=a;

a=b;

b=temp;

}

8.12 twotemps.cpp

#include

template

void Swap(T &a,T &b);

template

void Swap(T *a,T *b,int n);

void Show(int a[]);

const int Lim=8;

int main()

{

using namespace std;

int i=10,j=20;

cout<<"i,j="<

cout<<"Using compilter-generated int swapper:"<

Swap(i,j);

cout<<"Now i,j="<

int d1[Lim]={0,7,0,4,1,7,7,6};

int d2[Lim]={0,7,2,0,1,9,6,9};

cout<<"Original arrays:"<

Show(d1);

Show(d2);

Swap(d1,d2,Lim);

cout<<"Swapped arrays:"<

Show(d1);

Show(d2);

return 0;

}

template

void Swap(T &a,T &b)

{

T temp;

temp=a;

a=b;

b=temp;

}

template

void Swap(T a[],T b[],int n)

{

T temp;

for(int i=0;i

{

temp=a[i];

a[i]=b[i];

b[i]=temp;

}

}

void Show(int a[])

{

using namespace std;

cout<

cout<

for(int i=4;i

cout<

cout<

}

8.13 twoswap.cpp

#include

template

void Swap(T &a,T &b);

struct job

{

char name[40];

double salary;

int floor;

};

template<>void Swap(job &j1,job &j2); void Show(job &j);

int main()

{

using namespace std;

cout.precision(2);

cout.setf(ios::fixed,ios::floatfield);

int i=10,j=20;

cout<<"i,j="<

cout<<"Using compilter-generated int swapper:"<

Swap(i,j);

cout<<"Now i,j="<

job sue={"Susan Yaffee",73000.60,7};

job sidney={"Sidney Taffee",78060.72,9};

cout<<"Before job swapping:"<

Show(sue);

Show(sidney);

Swap(sue,sidney);

cout<<"After job swapping:"<

Show(sue);

Show(sidney);

return 0;

}

template

void Swap(T &a,T &b)

{

T temp;

temp=a;

a=b;

b=temp;

}

template<>void Swap(job &j1,job &j2)

{

double t1;

int t2;

t1=j1.salary;

j1.salary=j2.salary;

j2.salary=t1;

t2=j1.floor;

j1.floor=j2.floor;

j2.floor=t2;

}

void Show(job &j)

{

using namespace std;

cout<

8.14 temptempover.cpp

#include

template

void ShowArray(T arr[],int n);

template

void ShowArray(T *arr[],int n);

struct debts

{

char name[50];

double amount;

};

int main()

{

using namespace std;

int things[6]={13,31,103,301,310,130};

struct debts mr_E[3]=

{

{"Ima Wolfe",2400.0},

{"Ura Foxe",1300.0},

{"Iby Stout",1800.0}

};

double *pd[3];

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

pd[i]=&mr_E[i].amount;

cout<<"Listing Mr.E's counts of things:"<

ShowArray(things,6);

cout<<"Listing Mr.E's debts:"<

ShowArray(pd,3);

return 0;

}

template

void ShowArray(T arr[],int n)

{

using namespace std;

cout<<"template A"<

for(int i=0;i

cout<

cout<

}

template

void ShowArray(T *arr[],int n)

{

using namespace std;

cout<<"template B"<

for(int i=0;i

cout<<*arr[i]<<' ';

cout<

}

8.15 choices.cpp

#include

template

T lesser(T a,T b)

{

return a

}

int lesser(int a,int b)

{

a=a<0?-a:a;

b=b<0?-b:b;

return a

}

int main()

{

using namespace std;

int m=20;

int n=-30;

double x=15.5;

double y=25.9;

cout<

cout<

cout<(m,n)<

cout<(x,y)<

}

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

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