chapter12test4

来源:互联网 发布:完美假期周晨曦淘宝店 编辑:程序博客网 时间:2024/06/08 08:38

这个程序用了我两个多小时,这能力!明天跟同学去城隍阁放松一下,希望能好玩

stack.h

#ifndef STACK_H_
#define STACK_H_
typedef unsigned long Item;
class Stack
{
private:
enum{ MAX = 3 };
Item *pitem;
int size;
int top;
public:
Stack(int n = MAX);
~Stack();
Stack(const Stack &st);
bool isfull() const;
bool isempty() const;
bool push(const Item &item);
bool pop(Item &item);
Stack &operator=(const Stack &s);
};
#endif


stack.cpp

#include<iostream>
#include"stack.h"
using namespace std;
Stack::Stack(int n)
{
pitem = new Item[n];
top = 0;
size = 0;
}
Stack::~Stack()
{
delete []pitem;
}
Stack::Stack(const Stack &st)
{
pitem = new Item[st.size];
for (int i = 0; i < st.size; i++)
{
pitem[i] = st.pitem[i];
size++;
top++;
}
}
bool Stack::isfull() const
{
return top == MAX;
}
bool Stack::isempty() const
{
return top == 0;
}
bool Stack::push(const Item &item)
{
if (top < MAX)
{
pitem[top++] = item;
size++;
return true;
}
else
return false;
}
bool Stack::pop(Item &item)
{
if (top>0)
{
item = pitem[--top];
size--;
return true;
}
else
return false;
}
Stack &Stack::operator=(const Stack &s)
{
pitem = new Item[s.size];
for (int i = 0; i < s.size; i++)
{
pitem[i] = s.pitem[i];
size++;
top++;
}
return *this;
}


user.cpp

#include<iostream>
#include"stack.h"
int main()
{
using namespace std;
Stack stk;
unsigned long po;
int i;
cout << "Enter Item member :\n";
while (!stk.isfull()&&cin>>po)
{
cin.get();
stk.push(po);
cout << "Enter any noItem to quit or stack is full.\n";
}
Stack tak = stk;
while (!tak.isempty())
{
tak.pop(po);
cout << "PO# " << po << "poped.\n";
}
return 0;
}

0 0