云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > C++Primer - Plus(第五版)习题解答

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

  • 62 次阅读
  • 3 次下载
  • 2025/5/7 16:09:52

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(第五版)习题解答 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

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[]) {

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com