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

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值