表达式求值(河南省第九届省赛)

表达式求值

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 3
描述
假设表达式定义为: 1. 一个十进制的正整数 X 是一个表达式。 2. 如果 X 和 Y 是 表达式,则 X+Y, X*Y 也是表达式; *优先级高于+. 3. 如果 X 和 Y 是 表达式,则 函数 Smax(X,Y)也是表达式,其值为:先分别求出 X ,Y 值的各位数字之和,再从中选最大数。 4.如果 X 是 表达式,则 (X)也是表达式。 例如: 表达式 12*(2+3)+Smax(333,220+280) 的值为 69。 请你编程,对给定的表达式,输出其值。  
输入
【标准输入】 第一行: T 表示要计算的表达式个数 (1≤ T ≤ 10) 接下来有 T 行, 每行是一个字符串,表示待求的表达式,长度<=1000
输出
【标准输出】 对于每个表达式,输出一行,表示对应表达式的值。
样例输入
3
12+2*3
12*(2+3)
12*(2+3)+Smax(333,220+280)
样例输出
18
60
69
基本函数求值问题,直接附代码:

#include <bits/stdc++.h>


using namespace std;


int priority(char s)
{
    if(s==',')
        return 0;
    else if(s=='*')
        return 2;
    else if(s=='+')
        return 1;
}
bool isok(char s)
{
    if(s=='*'||s=='+'||s==',')
        return 1;
    return 0;
}
string getstring (string s)
{
    stack<char>st;
    string str;
    for(int i=0;i<s.size();i++)
    {
        if(isok(s[i]))
        {
            while(!st.empty()&&isok(st.top())&&priority(st.top())>=priority(s[i]))
            {
                str.push_back(st.top());
                st.pop();
            }
            st.push(s[i]);
        }
        else if(s[i]=='(')
            st.push(s[i]);
        else if(s[i]==')')
        {
            //cout<<str<<endl;
            while(st.top()!='(')
            {
                str.push_back(st.top());
                st.pop();
            }
            st.pop();
        }
        else if(s[i]>='0'&&s[i]<='9')
        {
            str.push_back(s[i]);
            if(i!=s.size()-1)
            {
                if(isok(s[i+1])||s[i+1]=='('||s[i+1]==')'||s[i+1]==',')
                    str.push_back(' ');
            }
            else
            {
                str.push_back(' ');
            }
        }
    }
    while(!st.empty())
    {
        str.push_back(st.top());
        st.pop();
    }
    return str;
}
void getnum(stack<int>&st,int &f,int &e)
{
    e=st.top();
    st.pop();
    f=st.top();
    st.pop();
}
int getResult(string s)
{
    stack <int>st;
    int f,e,cnt=0;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]>='0'&&s[i]<='9')
        {
            cnt++;
            st.push(s[i]-'0');
        }
        else if(s[i]==' ')
        {
            int j=0,sum=0;
            while(j!=cnt)
            {
                int po=1;
                for(int k=0;k<j;k++)
                    po*=10;
                sum+=st.top()*po;
                st.pop();
                j++;
            }
            //cout<<sum<<endl;
            st.push(sum);
            cnt=0;
        }
        else if(s[i]=='*')
        {
            getnum(st,f,e);
            st.push(f*e);
        }
        else if(s[i]=='+')
        {
            getnum(st,f,e);
            st.push(f+e);
        }
        else if(s[i]==',')
        {
            getnum(st,f,e);
            int a1=0,a2=0;
            while(f)
            {
                a1+=f%10;
                f/=10;
            }
            while(e)
            {
                a2+=e%10;
                e/=10;
            }
            st.push(max(a1,a2));
        }
    }
    return st.top();
}
int main()
{
    ios::sync_with_stdio(false);
    int t ;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        //cout<<getstring(s)<<endl;
        cout<<getResult(getstring(s))<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值