当前位置:首页 > C++Primer - Plus(第五版)习题解答
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
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
// 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
// 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
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
// 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
共分享92篇相关文档