括号匹配,栈实现
来源:互联网 发布:linux 创建管理员用户 编辑:程序博客网 时间:2024/06/07 05:01
#include<iostream>
using namespace std;
template<class T>
class Stack
{
public:
void Clear(); //清空栈
bool Push(const T item);//栈的压入操作
bool Pop(T & item); //读取栈顶的元素并删除
bool Top(T & item); //读取栈顶的元素并不删除
bool isEmpty(); //判断栈是否为空
bool isFull(); //判断栈是否已满
};
template<class T>
class ArrayStack:public Stack<T>
{
private:
int maxSize;
int top;
T * st;
public:
ArrayStack(int size);
ArrayStack();
~ArrayStack();
void Clear();
bool Push(const T item);
bool Pop(T &item);
bool PopAll();
bool Top(T & item);
bool isEmpty(); //判断栈是否为空
bool isFull(); //判断栈是否已满
};
template<class T>
ArrayStack<T>::ArrayStack(int size)
{
maxSize=size;
top=-1;
st=new T[maxSize];
}
template<class T>
ArrayStack<T>::ArrayStack()
{
top=-1;
}
template<class T>
ArrayStack<T>::~ArrayStack()
{
delete []st;
}
template<class T>
void ArrayStack<T>::Clear()
{
top=-1;
}
template<class T>
bool ArrayStack<T>::Push(const T item)
{
if(top==maxSize-1)
{
cout<<"栈满溢出"<<endl;
return false;
}
else
{
st[++top]=item;
return true;
}
}
template<class T>
bool ArrayStack<T>::Pop(T &item)
{
if(top==-1)
{
cout<<"缺少左括号"<<endl;
return false;
}
else
{
item= st[top--];
return true;
}
}
template<class T>
bool ArrayStack<T>::PopAll()
{
T a;
while(!isEmpty())
{
Pop(a);
cout<<a<<" ";
}
}
template<class T>
bool ArrayStack<T>::Top(T & item)
{
if(top==-1)
{
cout<<"缺少左括号"<<endl;
return false;
}
else
{
item =st[top];
return true;
}
}
template<class T>
bool ArrayStack<T>::isEmpty() //判断栈是否为空
{
if(top==-1)
return true;
else
return false;
}
template<class T>
bool ArrayStack<T>::isFull() //判断栈是否已满
{
if(top==maxSize-1)
return true;
else
return false;
}
void peidui()
{
ArrayStack<char> stack1(40);
char temp;
char a;
cout<<"请输入表达式"<<endl;
do
{
cin>>temp;
if(temp=='{' || temp=='[' || temp=='(' || temp==')'|| temp==']'|| temp=='}')
{
if(temp=='{' || temp=='[' || temp=='(')
stack1.Push(temp);
else if(temp==')' )
{
if(stack1.isEmpty())
{
cout<<"缺少左括号"<<endl;
break;
}
else
{
stack1.Top(a);
if(a=='(')
stack1.Pop(a);
else
cout<<"表达式错误"<<endl;
}
}
else if(temp==']' )
{
if(stack1.isEmpty())
{
cout<<"缺少左括号"<<endl;
break;
}
else
{
stack1.Top(a);
if(a=='[')
stack1.Pop(a);
else
cout<<"表达式错误"<<endl;
}
}
else if(temp=='}' )
{
if(stack1.isEmpty())
{
cout<<"缺少左括号"<<endl;
break;
}
else
{
stack1.Top(a);
if(a=='{')
stack1.Pop(a);
else
cout<<"表达式错误"<<endl;
}
}
}
else if(temp=='#')
{
if(stack1.isEmpty())
{
cout<<"表达式输入正确"<<endl;
break;
}
else
{
cout<<"缺少右括号"<<endl;
break;
}
}
else
{
cout<<"输入错误"<<endl;
goto jieshu;
break;
}
}while(1);
jieshu:;
}
int main()
{
peidui();
return 0;
}
阅读全文
0 0
- 栈实现括号匹配
- 栈实现括号匹配
- 栈实现括号匹配
- 栈实现括号匹配
- 括号匹配---栈实现
- 栈实现括号匹配
- 栈实现括号匹配
- 括号匹配,栈实现
- 栈实现括号匹配
- 链栈实现括号匹配
- 括号匹配(栈实现)
- 用栈实现括号匹配
- 括号匹配问题--栈实现
- 栈的实现-括号匹配
- java栈实现括号匹配
- 括号匹配(链栈实现)
- 括号匹配--顺序栈实现
- 用栈实现括号匹配
- ipython下运行代码出现<module '__main__' (built-in)> is a built-in class错误
- Bailian3711 字符串移位包含问题【字符串循环匹配】(POJ NOI0107-19)
- max学习之unwrap uv
- 2016年提高组模拟试题(20161105) 方程式
- 浅析React生命周期函数的使用
- 括号匹配,栈实现
- MPAndroidChar X轴显示日期以及动态改变坐标点显示精度
- 【Scikit-Learn 中文文档】支持向量机
- linux——数据库
- this指针
- 杭电ACM OJ 1024 Max Sum Plus Plus 动态规划 二维dp+滚动数组dp优化
- RNN技术
- 常用vim命令
- git clone 指定分支