您的位置:首页 > 编程语言 > C语言/C++

C++ Primer Plus第六版编程练习12.4解答

2015-05-10 16:04 330 查看
stack.h

#ifndef STACK_H_
#define STACK_H_

typedef unsigned long Item;

class Stack
{
private:
enum {MAX = 10};
Item * pitems; // holds stack items
int size; // number of elements in stack
int top; // index for top stack item
public:
Stack(int n = MAX); // creats stack with n elements
Stack(const Stack & st);
~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
Stack & operator=(const Stack & st);
void showStack() const;
};

#endif


stack.cpp
#include "stack.h"
#include <iostream>

Stack::Stack(int n)
{
pitems = new Item[MAX];
top = n;
size = n;
if (n != 0)
{
std::cout << "Please enter "<< n << "items of the stack:\n";
for (int i = 0; i < n; i++)
std::cin >> pitems[i];
}
}

Stack::Stack(const Stack & st)
{
pitems = new Item[MAX];
top = st.size;
size = st.size;
for (int i = 0; i < st.size; i++)
pitems[i] = st.pitems[i];
}

Stack::~Stack()
{
delete [] pitems;
}

bool Stack::isempty() const
{
return size == MAX;
}

bool Stack::isfull() const
{
return size == 0;
}

bool Stack::push(const Item & item)
{
if (size == MAX)
return false;
else
{
pitems[top++] = item;
size++;
return true;
}
}

bool Stack::pop(Item & item)
{
if (size == 0)
return false;
else
{
item = pitems[--top];
size--;
return true;
}
}

Stack & Stack::operator=(const Stack & st)
{
if (this == &st)
return *this;
delete [] pitems;
pitems = new Item[MAX];
top = st.size;
size = st.size;
for (int i = 0; i < st.size; i++)
pitems[i] = st.pitems[i];
return *this;
}

void Stack::showStack() const
{
if (size == 0)
{
std::cout << "The stack is empty.\n";
return;
}
else
{
for (int i = 0; i < size; i++)
std::cout << pitems[i] << "; ";
}
}


usestack.cpp
#include "stack.h"
#include <iostream>

int main()
{
using namespace std;
cout << "Use constructor to creat s1 with 5 elements.\n";
Stack s1(5);
cout << "The items in s1 is:\n";
s1.showStack();

cout << "\nTest some of the class methods.\n";
cout << "Is s1 empty?";
if (s1.isempty())
cout << " Yes.\n";
else
cout << " No.\n";
cout << "Is s1 full?";
if (s1.isfull())
cout << " Yes.\n";
else
cout << " No.\n";
cout << "Pop the top item in s1...\n";
Item it;
if (s1.pop(it))
{
cout << "Done. The top item in s1 is " << it;
cout << "\nAnd now the items in s1 is ";
s1.showStack();
}
else
cout << "Sorry, s1 is empty.\n";

cout << "\n\nUse copy constructor to creat s2.\n";
Stack s2(s1);
cout << "The items in s2 is:\n";
s2.showStack();
it = 789;
cout << "\nPush 789 to s2...\n";
if (s2.push(it))
{
cout << "Done. Now the items in s2 is ";
s2.showStack();
}
else
cout << "Sorry, s1 is already full.\n";

cout << "\n\nUse overloaded operator = to assign s2 to s3.\n";
Stack s3(0);
s3 = s2;
cout << "The items s3 is:\n";
s3.showStack();
cout << endl;
cin.get();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: