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

1、答案:

#include <iostream>

#include <string>

int main()

{

using namespace std;

char* fname = new char[10];

char* lname = new char[6];

char grade;

int age;

cout<< "What is your first name? ";

cin.getline(fname,10);

cout<< "What is your last name? ";

cin.getline(lname,6);

cout<< "What letter grade do you deserve? ";

cin>>grade;

cout<< "What is your age? ";

cin>>age;

cout <<"Name: "<<lname<<", "<<fname<<endl;

cout <<"Grade: "<<char(grade+1)<<endl;

cout <<"Age: "<<age;

delete [] fname;

delete [] lname;

return 0;

}

2、答案:

#include <iostream>

#include <string>

int main()

{

using namespace std;

string  name;

string  dessert;

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

getline(cin,name);

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

getline(cin,dessert);

cout<<"I have some delicious " << dessert;

cout <<" for you, "<< name << " .\n";

return 0;

}

3、答案:

#include <iostream>

#include <cstring>

int main()

{

using namespace std;

char* fname = new char [15];

char* lname = new char [8];

cout << "Enter your first name: ";

cin.getline(fname,5);

cout<<"Enter your last name:";

cin.getline(lname,8);

strcat(fname,",");

strcat(fname,lname);

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

<<fname;

delete [] fname;

delete [] lname;

return 0;

}

4、答案:

#include <iostream>

#include <string>

int main()

{

using namespace std;

string fname;

string lname;

cout << "Enter your first name: ";

getline(cin,fname);

cout<<"Enter your last name:";

getline(cin,lname);

fname +=", ";

fname += lname;

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

<<fname;

return 0;

}

5、答案

#include <iostream>

#include <string>

using namespace std;

struct CandyBar

{

string brand;

float weight;

int carlo;

};

int main()

{

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

cout<<"brand = "<<snack.brand<<endl

<<"weight = "<<snack.weight<<endl

<<"carlo = "<<snack.carlo<<endl;

return 0;

}

6、答案

#include <iostream>

#include <string>

using namespace std;

struct CandyBar

{

string brand;

float weight;

int carlo;

};

int main()

{

CandyBar snack[3] = {{"Mocha Munch",2.3,350},{"Mocha Middle",2.0,300},{"Mocha Little",3.2,600}};

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

cout<<"snack[i].brand = "<<snack[i].brand<<endl

<<"snack[i].weight = "<<snack[i].weight<<endl

<<"snack[i].carlo = "<<snack[i].carlo<<endl;

return 0;

}

7、答案

#include <iostream>

#include <string>

using namespace std;

struct pizaInfo

{

string coname;

int diameter;

float weight;

};

int main()

{

pizaInfo  p1;

cout << "Enter the infomation:\n";

cout << "Corpotion name:";

getline(cin,p1.coname);

cout <<"piza diameter: ";

cin>>p1.diameter;

cout << "Piza weight: ";

cin >>p1.weight;

cout<<"\n\n"<<"Corpotion name:"<<p1.coname<<endl

<<"piza diameter:"<<p1.diameter<<endl

<<"Piza weight: "<<p1.weight;

}

8、答案

#include <iostream>

#include <string>

using namespace std;

struct pizaInfo

{

string coname;

int diameter;

float weight;

};

int main()

{

pizaInfo* ps = new pizaInfo;

cout << "Enter the infomation:\n";

cout <<"piza diameter: ";

cin>>ps->diameter;

cout << "Corpotion name:";

cin>>ps->coname;

cout << "Piza weight: ";

cin >>ps->weight;

cout<<"\n\n"<<"Corpotion name:"<<ps->coname<<endl

<<"piza diameter:"<<ps->diameter<<endl

<<"Piza weight: "<<ps->weight;

delete ps;

}

9、答案

#include <iostream>

#include <string>

using namespace std;

struct CandyBar

{

string brand;

float weight;

int carlo;

};

int main()

{

CandyBar* snack = new CandyBar[3];

snack[0].brand = "Mocha Munch";

snack[0].carlo= 2.3;

snack[0].weight = 350;

snack[1].brand = "Mocha Middle";

snack[1].carlo= 2.0;

snack[1].weight = 300;

snack[2].brand = "Mocha Little";

snack[2].carlo= 3.2;

snack[2].weight = 600;

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

cout<<"snack[i].brand = "<<snack[i].brand<<endl

<<"snack[i].weight = "<<snack[i].weight<<endl

<<"snack[i].carlo = "<<snack[i].carlo<<endl;

delete [] snack;

return 0;

}

10、答案

#include <iostream>

#include <array>

using namespace std;

int main()

{

array<float,3> scores;

cout<<"Please enter three times score of 40-meters-run: \n";

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

{

cout<<"The "<<i<<" time score:";

cin>>scores[i];

}

cout << "The average of scores is :"

<<(scores[0]+scores[1]+scores[2])/3;

return 0;

}

时间: 2024-08-10 06:12:12

C++Primer Plus第6版 4.13编程练习答案的相关文章

C++ Primer Plus 第六版 第16章 string类和标准模板库

1.string实际上是模板具体化basic_string<char> 的一个typedef,有默认参数,所以省略了初始化参数 2.size_type是一个依赖于实现的整形 string将string::npos定义为字符串的最大长度 3.string类的构造函数P656 4.对于c-风格字符串,3种输入方法:cin>>   cin.getline(),cin.get 对于string   ,2种输入方法:cin>>,getline(cin,string对象) 5.st

c++ primer(第五版)学习笔记及习题答案代码版(第十四章)重载运算与类型转换

笔记较为零散,都是自己不熟悉的知识点. 习题答案至于一个.h 和.cc 中,需要演示某一题直接修改 #define NUM****, 如运行14.30题为#define NUM1430: Alice Emma has long flowing red hair. Her Daddy says when the wind blows through her hair, it looks almost alive, like a fiery bird in flight. A beautiful f

c++ primer plus(第6版)中文版 第十三章编程练习答案

第十三章编程练习答案 13.1根据Cd基类,完成派生出一个Classic类,并测试 //13.1根据Cd基类,完成派生出一个Classic类,并测试 #include <iostream> #include <cstring> using namespace std; // base class class Cd { char performers[50]; char label[20]; int selections; // number of selections double

c++ primer plus(第6版)中文版 第八章编程练习答案

第八章编程练习答案 8.1编写一个输出字符串的函数,有一个默认参数表示输出次数,默认为1.(原题太扯啦,题意基础上小改动) //8.1编写一个输出字符串的函数,有一个默认参数表示输出次数,默认为1.(原题太扯啦,题意基础上小改动) #include <iostream> using namespace std; void show (const char* str, int time=1) { unsigned n=(time>0)?time:-time; for (unsigned i

读书笔记之:C++ Primer (第4版)及习题(ch12-ch18) [++++]

读书笔记之:C++ Primer (第4版)及习题(ch12-ch18) [++++] 第12章 类 1. 类的声明与定义:前向声明,不完全类型 2. 从const函数返回*this 3. 可变数据成员mutable 4. 用于const对象的构造函数:构造函数不能声明为const 5. 构造函数初始化式 构造函数的执行分为两个阶段:初始化阶段和普通的计算阶段 6. 构造函数初始化列表 7. 默认实参与构造函数 8. 类通常定义一个默认构造函数,不然的话使用起来会很麻烦. 9. 使用默认构造函数

c++ primer(第五版)学习笔记及习题答案代码版(第十一章)关联容器

笔记较为零散,都是自己不熟悉的知识点. 习题答案至于一个.cc 中,包含Chapter7.h头文件,读入文件包括./test ./rules .需要演示某一题直接修改 #define NUM****, 如运行11.23题为#define NUM1123: chapter 11 1.  关联容器不支持顺序容器的位置相关的操作,例如push_front或push_back.原因是关联容器中元素是根据关键字存储的,这些操作对 关联容器没有意义.而且关联容器也不支持构造函数或插入操作这些接收一个元素值和

c++ primer(第五版)学习笔记及习题答案代码版(第六章)函数

笔记较为零散,都是自己不熟悉的知识点. 习题答案至于一个.cc 中,编译需要包含Chapter6.h头文件. 需要演示某一题直接修改 #define NUM***, 如运行6.23题为#define NUM623: chapter 6 1. 形参初始化的机理与变量初始化一样. 当形参是引用类型时,它对应的实参被引用传递或者函数被传引用调用. 2. const和实参 void fcn(const int i){ /*fcn能够读取i,但是不能向i写值*/} void fcn(int i){ /*.

C++ Primer中文版 (第5版)pdf

下载地址:网盘下载 C++ Primer中文版 (第5版)是久负盛名的 C 经典教程,时隔八年之久,终于迎来重大升级.除令全球无数程序员从中受益,甚至为之迷醉的--C 大师 Stanley B. Lippman 的丰富实践经验,C 标准委员会原负责人 Josée Lajoie 对C 标准的深入理解,以及C 先驱 Barbara E. Moo 在 C 教学方面的真知灼见外,更是基于全新的 C 11标准进行了全面而彻底的内容更新.非常难能可贵的是,<C Primer 中文版(第5版)>所有示例均全

C++ Primer 第四版中文完整版 和答案完整版

前段时间下载的C++Primer 第四版中文版 缺少第十到第十三章,终于下到了完整版的电子书还找到了完整的配套答案. 全部供大家免费下载: 下面是链接: 由于CSDN限制文件的大小所以C++primer 分了两个文件 还有答案是一个文件: http://download.csdn.net/my C++ Primer 第四版中文完整版 和答案完整版,布布扣,bubuko.com