⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack.h

📁 实现对输入的算术表达式进行计算,用栈完成此项功能
💻 H
字号:
// Stack.h: interface for the Stack class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STACK_H__BB8389C1_689E_4D28_A65F_A330A28A510C__INCLUDED_)
#define AFX_STACK_H__BB8389C1_689E_4D28_A65F_A330A28A510C__INCLUDED_
#include "iostream.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include<iostream.h>
#include<assert.h>

/*class Stack  
{
public:
	Stack();
	virtual ~Stack();

};*/
template<class Type>
class Stack
{
    private:
		int top;
		Type *elems;
		int Maxsize;
    public:
		Stack(int=10);
		~Stack(){ delete []elems; }
		void Push( Type  );//入栈
		Type Pop();//出栈
		Type Gettop();//取栈顶
		void MakeEmpty() { top=-1; }//置空
		int IsEmpty()  const{ return top==-1;}
		int IsFull()  const { return top==Maxsize-1; }
        friend ostream & operator << (ostream &out,Stack<Type> &ob);
  };
template<class Type>
Stack<Type>::Stack(int s):top(-1),Maxsize(s)
{
    elems=new Type[Maxsize];
    assert(elems!=0);
}
///////////////////////////////////////////////////////////
template<class Type>
void Stack<Type>::Push( Type item)
{
    assert(!IsFull());
    elems[++top]=item;
}
///////////////////////////////////////////////////////////
template<class Type>
Type Stack<Type>::Pop()
{
    assert(!IsEmpty());
    return elems[top--];
}
///////////////////////////////////////////////////////////
template<class Type>
Type Stack<Type>::Gettop()
{
    assert(!IsEmpty());
    return elems[top];
}
///////////////////////////////////////////////////////////
template<class Type>
ostream & operator << (ostream &out,Stack<Type> &ob)
{
    if(ob.IsEmpty())  out<<"NULL";
    for(int i=0;i<=ob.top;i++)
    {
		out<<"  "<<ob.elems[i]<<"  ";
    }
    out<<endl;
	return out;
}
///////////////////////////////////////////////////////////
#endif // !defined(AFX_STACK_H__BB8389C1_689E_4D28_A65F_A330A28A510C__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -