Complicated Expressions HDU - 1123 中缀、后缀表达式互转

题意:

给出表达式,可能会有多余的括号,去掉这些多余的括号。

思路:

先把表达式转化成后缀,不带括号,再利用后缀转化成中缀,带上括号,要注意括号的位置。

代码:

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define LL long long
using namespace std;
const int maxn=2000+10;
char ch[maxn];
char hz[maxn];
char zz[maxn];
stack<char>oc;
stack<double>on;
int priority(char c)
{
    switch (c)
    {
    case '*':
        return 2;
    case '/':
        return 2;
    case '+':
        return 1;
    case '-':
        return 1;
    case '(':
        return 0;
    case ')':
        return 0;
    default:
        return -1;
    }
}
int lenh;
void toH()
{
    /*
    1.数字直接入队列
    2.运算符要与栈顶元素比较
     ①栈为空直接入栈
     ②运算符优先级大于栈顶元素优先级则直接入栈
     ③小于或等于则出栈入列,再与栈顶元素进行比较,直到运算符优先级大于栈顶元
        素优先级后,操作符再入栈
    3.操作符是 ( 则无条件入栈
    4.操作符为 ),则依次出栈入列,直到匹配到第一个(为止,此操作符直接舍弃,(直接出栈舍弃
    */
    stack<char>s;
    queue<char>q;
    while(s.size())
        s.pop();
    while(q.size())
        q.pop();
    int len=strlen(ch);
    for(int i=0; i<len; i++)
    {
        char tmp=ch[i];
        if(islower(tmp))
            q.push(tmp);
        else if(tmp=='(')
            s.push(tmp);
        else if(tmp==')')
        {
            while(s.top()!='(')
            {
                q.push(s.top());
                s.pop();
            }
            s.pop();//左括号出栈
        }
        else
        {
            if(!s.size())
                s.push(tmp);
            else if(priority(tmp)>priority(s.top()))
            {
                s.push(tmp);
            }
            else
            {
                while(s.size()&&priority(tmp)<=priority(s.top()))
                {
                    q.push(s.top());
                    s.pop();
                }
                s.push(tmp);
            }
        }
    }
    while(s.size())
    {
        q.push(s.top());
        s.pop();
    }
    lenh=0;
    while(q.size())
    {
        cout<<q.front();
        hz[lenh++]=q.front();
        q.pop();
    }
    cout<<endl;
}
int vis[maxn];
void toZ()
{
    memset(vis,0,sizeof(vis));
    stack<string>s;
    while(s.size())
        s.pop();
    string a="",b="";
    for(int i=0; i<lenh; i++)
    {
        if(islower(hz[i]))
        {
            string tmp="";
            tmp.push_back(hz[i]);
            s.push(tmp);
            vis[s.size()-1]=0;
            //cout<<hz[i]+""<<"--"<<hz[i]<<endl;
        }
        else
        {
            a=s.top();
            s.pop();
            b=s.top();
            s.pop();
            //b在前
            switch (hz[i])
            {
            case '+':
                s.push(b+"+"+a);
                vis[s.size()-1]=1;
                //cout<<"("+b+"+"+a+")"<<endl;
                break;
            case '-':
                if(vis[s.size()+1]==1)
                    s.push(b+"-"+"("+a+")");
                else
                    s.push(b+"-"+a);
                vis[s.size()-1]=1;
                //cout<<"("+b+"-"+a+")"<<endl;
                break;
            case '*':
                if(vis[s.size()+1]==1)
                    a="("+a+")";
                if(vis[s.size()]==1)
                    b="("+b+")";
                s.push(b+"*"+a);
                vis[s.size()-1]=2;
                //cout<<b+"*"+a<<endl;
                break;
            case '/':
                if(vis[s.size()+1]>=1)
                    a="("+a+")";
                if(vis[s.size()]==1)
                    b="("+b+")";
                s.push(b+"/"+a);
                vis[s.size()-1]=2;
                //cout<<b+"/"+a<<endl;
                break;
            }
        }
    }
    cout<<s.top()<<endl;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",ch);
        toH();
        toZ();
    }
}
//((4+3)*2)
发布了1062 篇原创文章 · 获赞 72 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/104867603
今日推荐