Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
String temp(ps); return temp; }
int String::has(char ch) {
int ct = 0;
char * ps = str; while (*ps) {
if (*ps++ == ch) ++ct; }
return ct; }
int operator==(const String & s1, const String & s2) {
if (s1.chars != s2.chars) return 0;
else if (std::strcmp(s1.str, s2.str) == 0) return 1; else
return 0; }
int operator<(const String & s1, const String & s2) {
if (std::strcmp(s1.str, s2.str) < 0) return 1; else
return 0; }
int operator>(const String & s1, const String & s2) {
if (std::strcmp(s1.str, s2.str) > 0) return 1; else
return 0; }
PE 12-4
// pe12stak.h -- class definition for the stack ADT #ifndef PE12STAK_H_ #define PE12STAK_H_
typedef unsigned long Item;
class Stack {
private:
enum {MAX = 10}; // constant specific to class Item * pitems; // holds stack items
int size; // max number of elements in stack int top; // index for top stack item
Stack(const Stack & st) { } // no copying of stacks
Stack & operator=(const Stack & st) { return *this; } // no assignment public:
Stack(int n = MAX); ~Stack();
bool isempty() const; bool isfull() const;
SP 36 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
// 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
// pe12stak.cpp -- Stack member functions #include \
Stack::Stack(int n) // create an empty stack {
size = n;
pitems = new Item [size]; top = 0; }
Stack::~Stack() { delete [] pitems; }
bool Stack::isempty() const {
return top == 0 ? true: false; }
bool Stack::isfull() const {
return top == size ? true: false; }
bool Stack::push(const Item & item) {
if (top < size) {
pitems[top++] = item; return true; } else
return false; }
bool Stack::pop(Item & item) {
if (top > 0) {
item = pitems[--top]; return true; } else
return false; }
// pe12-4.cpp
#include
#include \// link with pe12stak.cpp int main(void) {
using namespace std;
Stack st(3); // create a stack of po numbers unsigned long temp; char c;
cout << \
<< \
SP 37 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
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 {
cout << \ cin >> temp; st.push(temp); }
break;
case 'P': if (st.isempty())
cout << \ else {
st.pop(temp);
cout << \ }
break;
default: cout << \ }
cout << \ << \ }
cout << \ return 0; }
PE 12-6
// pe12que.h -- interface for a queue #ifndef _QUEUE_H_ #define _QUEUE_H_
// This queue will contain Customer items class Customer {
private:
long arrive; // arrival time for customer int processtime; // processing time for customer public:
Customer() { arrive = processtime = 0; } void set(long when);
long when() const { return arrive; }
int ptime() const { return processtime; } };
typedef Customer Item;
class Queue {
private:
// class scope definitions
// Node is a nested structure definition local to this class struct Node { Item item; struct Node * next;}; enum {Q_SIZE = 10};
SP 38 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
// private class members
Node * front; // pointer to front of Queue Node * rear; // pointer to rear of Queue
int items; // current number of items in Queue const int qsize; // maximum number of items in Queue // preemptive definitions to prevent public copying Queue(const Queue & q) : qsize(0) { }
Queue & operator=(const Queue & q) { return *this;} public:
Queue(int qs = Q_SIZE); // create queue with a qs limit ~Queue();
bool isempty() const; bool isfull() const; int queuecount() const;
bool enqueue(const Item &item); // add item to end
bool dequeue(Item &item); // remove item from front };
#endif
// pe12que.cpp -- Queue and Customer methods #include \
#include
// Queue methods
Queue::Queue(int qs) : qsize(qs) {
front = rear = NULL; items = 0; }
Queue::~Queue() {
Node * temp;
while (front != NULL) // while queue is not yet empty {
temp = front; // save address of front item front = front->next;// reset pointer to next item delete temp; // delete former front } }
bool Queue::isempty() const {
return items == 0; }
bool Queue::isfull() const {
return items == qsize; }
int Queue::queuecount() const {
return items; }
// Add item to queue
bool Queue::enqueue(const Item & item) {
if (isfull())
return false;
Node * add = new Node; // create node if (add == NULL)
SP 39 of 65 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
return false; // quit if none available add->item = item; // set node pointers add->next = NULL; items++;
if (front == NULL) // if queue is empty, front = add; // place item at front else
rear->next = add; // else place at rear
rear = add; // have rear point to new node return true; }
// Place front item into item variable and remove from queue bool Queue::dequeue(Item & item) {
if (front == NULL) return false;
item = front->item; // set item to first item in queue items--;
Node * temp = front; // save location of first item front = front->next; // reset front to next item delete temp; // delete former first item if (items == 0) rear = NULL; return true; }
// customer method
// when is the time at which the customer arrives
// the arrival time is set to when and the processing // time set to a random value in the range 1 - 3 void Customer::set(long when) {
processtime = std::rand() % 3 + 1; arrive = when; }
// pe12-6.cpp -- use the Queue interface // link to pe12que.cpp
// modify Listing 12.10 by adding a second queue #include
#include
#include
const long MIN_PER_HR = 60L;
bool newcustomer(double x); // is there a new customer?
int main(void) {
using std::cin; using std::cout; using std::endl;
using std::ios_base;
// setting things up
std::srand(std::time(0)); // random initializing of rand()
cout << \ cout << \ int qs; cin >> qs;
SP 40 of 65 September 2, 2004