/**写一个函数,利用栈运算读入一行本文,并以相反的次序输出**/
#include "iostream"
using namespace std;
enum ResultCode{Underflow,Overflow};
template
class Stack
{
public:
virtual void Push(const T &x)=0;
virtual void Pop()=0;
virtual T Top() const=0;
virtual bool IsEmpty() const=0;
virtual bool IsFull() const=0;
};
template
class SeqStack:public Stack
{
public:
SeqStack(int mSize);
~SeqStack(){delete[] s;}
bool IsEmpty() const {return (top==-1);}
bool IsFull() const {return (top==maxSize-1);}
void Push(const T &x);
void Pop();
T Top() const;
private:
void Output(ostream & out)const;
T *s;
int maxSize;
int top;
friend ostream &operator<<(ostream & out,SeqStack & s);
};
template
SeqStack::SeqStack(int mSize)
{
maxSize=mSize;
s=new T[maxSize];
top=-1;
}
template
void SeqStack::Pop()
{
if(IsEmpty()) throw Underflow;
top--;
}
template
void SeqStack::Push(const T &x)
{
if(IsFull()) throw Overflow;
s[++top]=x;
}
template
T SeqStack::Top() const
{
if(IsEmpty()) throw Underflow;
return s[top];
}
template
void SeqStack::Output(ostream& out) const
{
out<<"the stack contains:"<for(int i=top;i>-1;i--)
out<out<}
template
ostream& operator<<(ostream& out,SeqStack& s)
{
s.Output(out);
return out;
}
void input()
{
char c;
cout<<"please input the text and end for *"<SeqStack dstk(100);
while((c=getchar())!='\n')
dstk.Push(c);
while(!dstk.IsEmpty())
{
cout< dstk.Pop();
}
}
int main()
{
input();
}
阅读(1410) | 评论(0) | 转发(0) |