数据结构:中缀式转为后缀式

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;

template<class ElemType>
class stack
{
private:
    ElemType *base;   // 栈底指针
    ElemType *top;   // 栈顶指针
    int maxSize;
public:
    //初始化顺序栈
    stack(int MS=100)
    {
        base = new ElemType[MS];
        top = base;
        maxSize=MS;
    }
    //删除顺序栈
    ~stack()
    {
        stackDestroy();
    }
    //将顺序栈置为空表
    bool StackClear()
    {
        top = base;
        return true;
    }
    //返回顺序栈的长度
    int StackLength() const
    {
        return top - base;
    }
    //设置顺序栈的长度
    bool isEmpty() const
    {
        return top == base;
    }
    //判断顺序栈是否为满栈
    bool isFull() const
    {
        return StackLength()==maxSize;
    }
    //用e返回栈顶元素
    bool GetTop(ElemType &e) const
    {
        if(isEmpty()) return false;
        e = *(top-1);
        return true;
    }
    //入栈
    bool push(ElemType &e)
    {
        if(isFull()) return false;//DoubleSpace();
        *top = e;
        top++;
        return true;
    }
    //出栈
    bool pop(ElemType &e)
    {
        if(isEmpty()) return false;
        top--;
        e=*top;
        return true;
    }
    //销毁顺序栈
    bool stackDestroy()
    {
        delete []base;
        return true;
    }
    //遍历顺序栈
    void stackTraverse() const
    {
        for(ElemType *p = top-1; p>=base; p--)
        {
            cout<<*p;
        }
        cout<<endl;
    }
};

template<class ElemType>
int getprecedence(ElemType &operater)//优先级
{
    switch(operater)
    {
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
        return 2;
    }
    return 0;
}

template<class ElemType>
void infix_to_suffix( stack<ElemType> &s, string &infix, string &suffix)
{
    char e;
    for(int i=0; i<infix.length(); i++)
    {
        if(isdigit(infix[i]))//遇到数字字符直接输出,因为我们oj上格式控制有要求,所以空格空格有点麻烦
        {
            cout<<infix[i];
            if(!isdigit(infix[i+1])&&(i+1)!=infix.length())
                cout<<" ";
            if(i==infix.length()-1)
                cout<<" ";
        }
        else if(infix[i]=='+'||infix[i]=='-'||infix[i]=='*'||infix[i]=='/')
        {
            if(s.isEmpty())//是空的的话就直接入栈
                s.push(infix[i]);
            else
            {
                s.GetTop(e);
                if(getprecedence(e)>=getprecedence(infix[i]))//比较优先级,栈中运算符优先级大于或等于此运算符就弹出
                {
                    s.pop(e);
                    cout<<e<<" ";
                }
                s.push(infix[i]);
            }
        }
        else if(infix[i]=='('||infix[i]==')')//遇到右括号,就将栈中运算符一直弹出直至遇到左括号
        {
            if(infix[i]=='(')
                s.push(infix[i]);
            else
            {
                s.GetTop(e);
                while(e!='(')
                {
                    cout<<e<<' ';
                    s.pop(e);
                    s.GetTop(e);
                }
                s.pop(e);
            }
        }
        if(i==infix.length()-1&&!s.isEmpty())
        {
            while(!s.isEmpty())
            {
                s.pop(e);
                cout<<e;
                if(s.StackLength()!=0)
                    cout<<" ";
            }
        }
    }
    cout<<endl;
}

int main()
{
    stack<char>s;
    string infix,suffix;
    getline(cin,infix);
    infix_to_suffix(s,infix,suffix);
    return 0;
}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值