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

2019-03-22 17:31

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

Chapter 9

PE 9-1

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

char fullname[Len]; 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

SP 16 of 65 September 2, 2004

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

void handicap(golf & g, int hc) {

g.handicap = hc; }

// 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 std::strcpy(p1[0].dross, \ p1[0].slag = 13;

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

SP 17 of 65 September 2, 2004

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

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; else

std::cout << \}

void BankAccount::withdraw(double cash) {

if (cash < 0)

std::cout << \ else if (cash <= balance) balance -=cash;

SP 18 of 65 September 2, 2004

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

else

std::cout << \}

// sample use

int main() {

BankAccount bird;

BankAccount frog(\

bird.show(); frog.show();

bird = BankAccount(\ bird.show();

frog.deposit(20); frog.show();

frog.withdraw(4000); frog.show();

frog.withdraw(50); frog.show(); }

PE10-4

// pe10-4.h

#ifndef SALES__ #define SALES__

namespace SALES {

const int QUARTERS = 4; class Sales {

private:

double sales[QUARTERS]; double average; double max; double min; public:

// default constructor Sales();

// copies the lesser of 4 or n items from the array ar // to the sales member and computes and stores the

// average, maximum, and minimum values of the entered items; // remaining elements of sales, if any, set to 0 Sales(const double ar[], int n);

// gathers sales for 4 quarters interactively, stores them // in the sales member of object and computes and stores the // average, maximum, and minumum values void setSales();

// display all information in object void showSales(); }; }

#endif

SP 19 of 65 September 2, 2004

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

// pe10-4a.cpp

#include #include \

int main() {

using SALES::Sales;

double vals[3] = {2000, 3000, 5000}; Sales forFiji(vals, 3); forFiji.showSales();

Sales red;

red.showSales(); red.setSales(); red.showSales();

return 0; }

// pe10-4b.cpp

#include #include \

namespace SALES {

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

Sales::Sales(const double ar[], int n) {

if (n < 0) n = 0;

int limit = n < QUARTERS ? n : QUARTERS; double total = 0; min = 0; max = 0;

average = 0; if (limit > 0)

min = max = ar[0]; int i;

for (i = 0; i < limit; i++) {

sales[i] = ar[i]; total += ar[i]; if (ar[i] > max) max = ar[i];

else if (ar[i] < min) min = ar[i]; }

for (i = limit; i < QUARTERS; i++) sales[i] = 0; if (limit > 0)

average = total / limit; }

Sales::Sales() {

min = 0; max = 0;

average = 0;

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

SP 20 of 65 September 2, 2004


C++ - Primer - Plus(第五版)习题解答(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2019-2024年中国洗衣机行业前景预测及投资咨询报告(目录) - 图

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: