6.6

· · 个人记录

#include<bits/stdc++.h>
using namespace std;
string s;
stack<char> op;
queue<char> dat;
map<char,int> f;
void c(string st) {
    string ans="";
    int len=st.size();
    for(int i=0;i<len;i++) {
        if(st[i]>='0'&&st[i]<='9') dat.push(st[i]);
        else {
            if(st[i]=='(') op.push('(');
            else if(st[i]==')') {
                while(op.top()!='(') 
                    dat.push(op.top()),op.pop();
                if(op.size())op.pop();
            }
            else {
                while(op.size()&&f[op.top()]>=f[st[i]])
                    dat.push(op.top()),op.pop();
                op.push(st[i]);
            }
        }
    }
    while(op.size()) 
        dat.push(op.top()),op.pop();
    queue<char> q;
    q=dat;
    while(q.size()) {
        cout<<q.front()<<" ";
        q.pop();
    }
}
queue<char> js(queue<char> q) {
    int a=0,b=0;
    char ysf;
    stack<int> tmp,tp;
    while(q.size()&&q.front()>='0'&&q.front()<='9')
        tmp.push(q.front()),q.pop();
    ysf=q.front();q.pop();
    b=tmp.top()-'0';tmp.pop();a=tmp.top()-'0';tmp.pop();
    cout<<a<<" "<<b<<" "<<ysf;
    int jg=0;
    switch(ysf) {
        case '*': jg=a*b;break; 
        case '+': jg=a+b;break; 
        case '/': jg=a/b;break; 
        case '^': jg=pow(a,b);break; 
        case '-': jg=a-b;break; 
    }
    tmp.push(jg-'0');
    cout<<jg<<"\n";
    while(q.size())tmp.push(q.front()),q.pop();
    while(tmp.size())tp.push(tmp.top()),tmp.pop();
    queue<char>qq;
    while(tp.size())qq.push(tp.top()),tp.pop();
    return qq;
}
int main() {
    cin>>s;
    f['^']=3;
    f['/']=f['*']=2;
    f['+']=f['-']=1;
    f['(']=f[')']=0;
    c(s);
//  puts("");
//  queue<char> q=js(dat);
//  while(q.size()) {
//      cout<<q.front()<<" ";q.pop();
//  }
    return 0;
}
//后缀表达式