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

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

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

  • 62 次阅读
  • 3 次下载
  • 2025/5/4 4:27:36

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

items[top++] = item; return true; } else

return false; }

bool Stack::pop(Item & item) {

if (top > 0) {

item = items[--top]; return true; } else

return false; }

// pe10-5.cpp

#include #include

#include \// link with pe10stack.cpp

void get_customer(customer & cu); int main(void) {

using namespace std;

Stack st; // create a stack of customer structures customer temp;

double payments = 0; char c;

cout << \

<< \ while (cin >> c && (c = toupper(c)) != 'Q') {

while (cin.get() != '\\n') continue;

if (c != 'A' && c != 'P') {

cout << \ continue; }

switch (c) {

case 'A' : if (st.isfull())

cout << \ else {

get_customer(temp); st.push(temp); }

break;

case 'P' : if (st.isempty())

cout << \

SP 25 of 73 September 2, 2004

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

else {

st.pop(temp);

payments += temp.payment;

cout << temp.fullname << \ cout << \ << payments << \ }

break;

default : cout << \ }

cout << \

<< \ }

cout << \ return 0; }

void get_customer(customer & cu) {

using namespace std;

cout << \ cin.getline(cu.fullname,35);

cout << \ cin >> cu.payment;

while (cin.get() != '\\n') continue; }

PE 10-8

// pe10-8arr.h -- header file for a simple list class

#ifndef SIMPLEST_ #define SIMPLEST_

// program-specific declarations

const int TSIZE = 45; // size of array to hold title struct film {

char title[TSIZE]; int rating; };

// general type definitions typedef struct film Item;

const int MAXLIST = 10; class simplist {

private:

Item items[MAXLIST]; int count; public:

simplist(void); bool isempty(void); bool isfull(void);

SP 26 of 73 September 2, 2004

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

int itemcount();

bool additem(Item item);

void transverse( void (*pfun)(Item item)); };

#endif

// pe10-8arr.cpp -- functions supporting simple list operations #include \

simplist::simplist(void) {

count = 0; }

bool simplist::isempty(void) {

return count == 0; }

bool simplist::isfull(void) {

return count == MAXLIST; }

int simplist::itemcount() {

return count; }

bool simplist::additem(Item item) {

if (count == MAXLIST) return false; else

items[count++] = item; return true; }

void simplist::transverse( void (*pfun)(Item item)) {

for (int i = 0; i < count; i++) (*pfun)(items[i]); }

// pe10-8.cpp -- using a class definition

#include

#include // prototype for exit()

#include \ // array version

void showmovies(Item item); // to be used by transverse()

int main(void) {

using namespace std;

SP 27 of 73 September 2, 2004

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

simplist movies; // creates an empty list Item temp;

if (movies.isfull()) // invokes isfull() member function {

cout << \ exit(1); }

cout << \

while (cin.getline(temp.title,TSIZE) && temp.title[0] != '\\0') {

cout << \ cin >> temp.rating;

while(cin.get() != '\\n') continue;

if (movies.additem(temp) == false) {

cout << \ break; }

if (movies.isfull()) {

cout << \ break; }

cout << \ }

if (movies.isempty())

cout << \ else {

cout << \ movies.transverse(showmovies); }

cout << \ return 0; }

void showmovies(Item item) {

std::cout << \ << item.rating << std::endl; }

Chapter 11

PE 11-2

// pe11-2.h -- Vector class with <<, mode state // modified implementation #ifndef MODVECTOR_H_ #define MODVECTOR_H_ #include namespace VECTOR

SP 28 of 73 September 2, 2004

搜索更多关于: C++Primer - Plus(第五版)习题解答 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition items[top++] = item; return true; } else return false; } bool Stack::pop(Item & item) { if (top > 0) { item = items[--top]; return true; } else return false; } // pe10-5.cpp #include #in

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价: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