爆马蜂

· · 个人记录

#include<bits/stdc++.h>
using namespace std;
#define _ ;
#define __ ,
#define ___ int
#define ____ main
#define _____ {
#define ______ }
#define _______ +
#define ________ (
#define _________ )
#define __________ ::
#define ___________ cin
#define ____________ cout
#define _____________ >>
#define ______________ <<
#define _______________ return 0
___ ________________ __ _________________ _ ___ ____ ________ _________ _____ ___________ _____________ ________________ _____________ _________________ _ ____________ ______________ ________________ _______ _________________ _ _______________ _ ______

开个玩笑,真正的马蜂长这样:

#include<bits/stdc++.h>
using namespace std;
// #define fileio
#define IOS
void ___()
{
#ifdef fileio
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
#ifdef IOS
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    #define endl '\n'
#endif
}
#define ll long long
string ex;
stack<char> dat,op;
stack<int> s,tmp;
int prio(char c)//优先级
{
    switch(c)
    {
        case '+': return 1;
        case '-': return 1;
        case '*': return 2;
        case '/': return 2;
        case '^': return 3;
        default: return -1;
    }
}
void change(string s)//中缀s转后缀存到dat
{
    int ln=s.length();
    for(int i=0;i<ln;i++)
    {
        if(s[i]>='0'&&s[i]<='9')
            dat.push(s[i]);
        else if(s[i]=='(')
            op.push(s[i]);
        else if(s[i]==')')
        {
            while(op.top()!='(')
            {
                dat.push(op.top());
                op.pop();
            }
            op.pop();
        }
        else if(s[i]=='^')
            op.push(s[i]);
        else
        {
            while(!op.empty()&&prio(op.top())>=prio(s[i]))
            {
                dat.push(op.top());
                op.pop();
            }
            op.push(s[i]);
        }
    }
    while(!op.empty())
    {
        dat.push(op.top());
        op.pop();
    }
    while(!dat.empty())
    {
        op.push(dat.top());
        dat.pop();
    }
}
int calc(int x,int y,char c)//计算
{
    switch(c)
    {
        case '+': return x+y;
        case '-': return x-y;
        case '*': return x*y;
        case '/': return x/y;
        case '^': return pow(x,y);
        default: return -1;
    }
}
int main()
{
    ___();
    cin>>ex;
    change(ex);
    char t;
    int x,y;
    while(!op.empty())
    {
        cout<<op.top()<<" ";
        dat.push(op.top());
        op.pop();
    }
    while(!dat.empty())
    {
        op.push(dat.top());
        dat.pop();
    }
    cout<<endl;
    while(!op.empty())
    {
        t=op.top();
        op.pop();
        if(t>='0'&&t<='9')
            s.push(t-'0');
        if(t=='+'||t=='-'||t=='*'||t=='/'||t=='^')
        {
            y=s.top(),s.pop();
            x=s.top(),s.pop();
            s.push(calc(x,y,t));
            while(!s.empty())
            {
                tmp.push(s.top());
                s.pop();
            }
            while(!tmp.empty())
            {
                cout<<tmp.top()<<" ";
                s.push(tmp.top());
                tmp.pop();
            }
            while(!op.empty())
            {
                cout<<op.top()<<" ";
                dat.push(op.top());
                op.pop();
            }
            while(!dat.empty())
            {
                op.push(dat.top());
                dat.pop();
            }
            cout<<endl;
        }
    }
    return 0;
}

差不多就这样。(猜的出来我贴的哪题代码吗?第一个猜出来奖一关