Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
sales[i] =0; }
void Sales::setSales() {
double sa[QUARTERS]; int i;
for (i = 0; i < QUARTERS; i++) {
cout << \ cin >> sa[i]; }
// create temporary object, copy to invoking object *this = Sales(sa, QUARTERS); }
void Sales::showSales() {
cout << \
for (int i = 0; i < QUARTERS; i++)
cout << \ << sales[i] << endl;
cout << \ cout << \ cout << \ } }
PE 10-5
// pe10stack.h -- class definition for the stack ADT // for use with pe10-5.cpp #ifndef _STACK_H_ #define _STACK_H_
struct customer {
char fullname[35]; double payment; };
typedef customer Item;
class Stack {
private:
enum {MAX = 10}; // constant specific to class Item items[MAX]; // holds stack items int top; // index for top stack item public:
Stack();
bool isempty() const; bool isfull() const;
// push() returns false if stack already is full, true otherwise bool push(const Item & item); // add item to stack
// pop() returns false if stack already is empty, true otherwise bool pop(Item & item); // pop top into item };
#endif
SP 21 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
// pe10stack.cpp -- Stack member functions // for use with pe10-5.cpp
// exactly the same as stack.cpp in the text
#include \
Stack::Stack() // create an empty stack {
top = 0; }
bool Stack::isempty() const {
return top == 0; }
bool Stack::isfull() const {
return top == MAX; }
bool Stack::push(const Item & item) {
if (top < MAX) {
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 \// 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;
SP 22 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
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 << \ 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;
SP 23 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
const int MAXLIST = 10; class simplist {
private:
Item items[MAXLIST]; int count; public:
simplist(void); bool isempty(void); bool isfull(void); 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
#include \ // array version
void showmovies(Item item); // to be used by transverse()
SP 24 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
int main(void) {
using namespace std;
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
using std::ostream; class Vector {
SP 25 of 65 September 2, 2004