70pts求助 悬关 WA#3#9#10
查看原帖
70pts求助 悬关 WA#3#9#10
677489
Aaron530楼主2025/7/21 20:18
#include <bits/stdc++.h>
using namespace std;
int priority(string op)
{
    char c=op[0];
    switch(c)
    {
        case '+':return 1;
        case '-':return 1;
        case '*':return 2;
        case '/':return 2;
        case '^':return 3;
        case '(':return 0;
        case ')':return 0;
        default:return -1;
    }
}
string ctos(char c)
{
    string res;
    res+=c;
    return res;
}
bool isint(string s)
{
    if(s.size()==1 && !isdigit(s[0]))
        return false;
    for(int i=0;i<s.size();i++)
        if(s[i]!='-' && !isdigit(s[i]))
            return false;
    return true;
}
bool check(queue <string> t)
{
    if(t.size()<3)
        return false;
    string s1,s2,s3;
    s1=t.front();t.pop();
    s2=t.front();t.pop();
    s3=t.front();t.pop();
    if(isint(s1) && isint(s2) && priority(s3)!=-1)
        return true;
    return false;
}
string compute(string num1,string num2,string operation)
{
    int x1=0,x2=0;
    char op=operation[0];
    if(num1[0]!='-')
        for(int i=0;i<num1.size();i++)
            x1=x1*10+(num1[i]-'0');
    else
    {
        for(int i=1;i<num1.size();i++)
            x1=x1*10+(num1[i]-'0');
        x1=-x1;
    }
    if(num2[0]!='-')
        for(int i=0;i<num2.size();i++)
            x2=x2*10+(num2[i]-'0');
    else
    {
        for(int i=1;i<num2.size();i++)
            x2=x2*10+(num2[i]-'0');
        x2=-x2;
    }
    int res;
    if(op=='+')    res=x1+x2;
    else if(op=='-')    res=x1-x2;
    else if(op=='*')    res=x1*x2;
    else if(op=='/')    res=x1/x2;
    else if(op=='^')    res=pow(x1,x2);
    string ans;
    if(res<0)
    {
        ans+='-';
        res=-res;
    }
    while(res)
    {
        int t=res%10;
        ans+=t+'0';
        res/=10;
    }
    reverse(ans.begin(),ans.end());
    return ans;
}
void print(stack <string> t)
{
    while(!t.empty())
    {
        cout<<t.top()<<" ";
        t.pop();
    }
    cout<<endl;
}
int main()
{
    stack <string> op,num;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
        if(isdigit(s[i]))
            num.push(ctos(s[i]));
        else
        {
            int t=priority(ctos(s[i]));
            if(t>=1 && t<=3)
            {
                while(t<=2 && !op.empty() && priority(op.top())>=t)
                {
                    num.push(op.top());
                    op.pop();
                }
                op.push(ctos(s[i]));
            }
            else if(t==0)
                if(s[i]=='(')
                    op.push(ctos(s[i]));
                else
                {
                    while(op.top()!="(")
                    {
                        num.push(op.top());
                        op.pop();
                    }
                    op.pop();
                }
        }
    while(!op.empty())
    {
        num.push(op.top());
        op.pop();
    }

    stack <string> exp;//存储正序的后缀表达式
    while(!num.empty())
    {
        exp.push(num.top());
        num.pop();
    }
    print(exp);
    while(exp.size()>1)//计算
    {
        
        queue <string> q;
        stack <string> temp;
        while(!exp.empty())
        {
            if(check(q)==true)
                break;
            if(q.size()==3)
            {
                temp.push(q.front());
                q.pop();
            }
            q.push(exp.top());
            exp.pop();
        }
        string s1,s2,s3;
        s1=q.front();q.pop();
        s2=q.front();q.pop();
        s3=q.front();q.pop();
        exp.push(compute(s1,s2,s3));
        while(!temp.empty())
        {
            exp.push(temp.top());
            temp.pop();
        }
        print(exp);
    }
    return 0;
}
2025/7/21 20:18
加载中...