第一范文网 - 专业文章范例文档资料分享平台

C++Primer - Plus(第五版)习题解答

来源:用户分享 时间:2025/5/18 15:07:21 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

{

cout << bny.str << endl; } }

void set(stringy & bny, const char * str) {

bny.ct = strlen(str);

bny.str = new char[bny.ct+1]; strcpy(bny.str, str); }

// pe8-5.cpp

#include

template T max5(T ar[]) {

int n;

T max = ar[0];

for (n = 1; n < 5; n++) if (ar[n] > max) max = ar[n]; return max; }

const int LIMIT = 5; int main( ) {

using namespace std;

double ard[LIMIT] = { -3.4, 8.1, -76.4, 34.4, 2.4}; int ari[LIMIT] = {2, 3, 8, 1, 9}; double md; int mi;

md = max5(ard); mi = max5(ari);

cout << \ cout << \

return 0; }

Chapter 9

PE 9-1

// pe9-golf.h - for pe9-1.cpp const int Len = 40; struct golf {

char fullname[Len];

SP 17 of 73 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

int handicap; };

// non-interactive version

// function sets golf structure to provided name, handicap // using values passed as arguments to the function void setgolf(golf & g, const char * name, int hc);

// interactive version

// function solicits name and handicap from user // and sets the members of g to the values entered

// returns 1 if name is entered, 0 if name is empty string int setgolf(golf & g);

// function resets handicap to new value void handicap(golf & g, int hc);

// function displays contents of golf structure void showgolf(const golf & g);

// pe9-golf.cpp - for pe9-1.cpp #include #include \#include

// function solicits name and handicap from user

// returns 1 if name is entered, 0 if name is empty string int setgolf(golf & g) {

std::cout << \ std::cin.getline(g.fullname, Len); if (g.fullname[0] == '\\0')

return 0; // premature termination

std::cout << \ while (!(std::cin >> g.handicap)) {

std::cin.clear();

std::cout << \ }

while (std::cin.get() != '\\n') continue; return 1; }

// function sets golf structure to provided name, handicap void setgolf(golf & g, const char * name, int hc) {

std::strcpy(g.fullname, name); g.handicap = hc; }

// function resets handicap to new value void handicap(golf & g, int hc) {

g.handicap = hc; }

SP 18 of 73 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

// function displays contents of golf structure void showgolf(const golf & g) {

std::cout << \ std::cout << \}

// pe9-1.cpp

#include #include \

// link with pe9-golf.cpp const int Mems = 5; int main(void) {

using namespace std; golf team[Mems];

cout << \ int i;

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

if (setgolf(team[i]) == 0) break;

for (int j = 0; j < i; j++) showgolf(team[j]);

setgolf(team[0], \ showgolf(team[0]); handicap(team[0], 3); showgolf(team[0]);

return 0; }

PE 9-3

//pe9-3.cpp -- using placement new #include #include

#include struct chaff {

char dross[20]; int slag; };

// char buffer[500]; // option 1 int main() {

using std::cout; using std::endl; chaff *p1; int i;

char * buffer = new char [500]; // option 2

p1 = new (buffer) chaff[2]; // place structures in buffer

SP 19 of 73 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

std::strcpy(p1[0].dross, \ p1[0].slag = 13;

std::strcpy(p1[1].dross, \ p1[1].slag = -39;

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

cout << p1[i].dross << \ delete [] buffer; // option 2

return 0; }

Chapter 10

PE 10-1

// pe10-1.cpp

#include #include

// class declaration class BankAccount {

private:

char name[40]; char acctnum[25]; double balance; public:

BankAccount(char * client = \ double bal = 0.0); void show(void) const; void deposit(double cash); void withdraw(double cash); };

// method definitions

BankAccount::BankAccount(char * client, char * num, double bal) {

std::strncpy(name, client, 39); name[39] = '\\0';

std::strncpy(acctnum, num, 24); acctnum[24] = '\\0'; balance = bal; }

void BankAccount::show(void) const {

using std::cout; using std:: endl;

cout << \

cout << \ cout << \}

void BankAccount::deposit(double cash) {

if (cash >= 0)

balance += cash;

SP 20 of 73 September 2, 2004

搜索更多关于: C++Primer - Plus(第五版)习题解答 的文档
C++Primer - Plus(第五版)习题解答.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c4ipnz2rizz3pebe0ilfj_5.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top