表达式求值(多函数问题)

不了解表达式求值请访问
http://blog.csdn.net/ffgcc/article/details/78033447
仅单函数求值请访问
http://blog.csdn.net/ffgcc/article/details/78034821
这里是一个多函数求值的问题
题目描述
Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20,23)的值是20 ,add(10,98) 的值是108等等。经过训练,Dr.Kong设计的机器人卡多甚至会计算一种嵌套的更复杂的表达式。

假设表达式可以简单定义为:

  1. 一个正的十进制数 x 是一个表达式。

  2. 如果 x 和 y 是 表达式,则 函数min(x,y )也是表达式,其值为x,y 中的最小数。

  3. 如果 x 和 y 是 表达式,则 函数max(x,y )也是表达式,其值为x,y 中的最大数。

4.如果 x 和 y 是 表达式,则 函数add(x,y )也是表达式,其值为x,y 之和。

例如, 表达式 max(add(1,2),7) 的值为 7。

请你编写程序,对于给定的一组表达式,帮助 Dr.Kong 算出正确答案,以便校对卡多计算的正误。

输入
第一行: N 表示要计算的表达式个数 (1≤ N ≤ 10)

接下来有N行, 每行是一个字符串,表示待求值的表达式

(表达式中不会有多余的空格,每行不超过300个字符,表达式中出现的十进制数都不

超过1000。)

输出
输出有N行,每一行对应一个表达式的值。

样例输入
3
add(1,2)
max(1,999)
add(min(1,1000),add(100,99))
样例输出
3
999
200
这里在单函数求值的基础上对原字符串进行修改,
使每个逗号被压入栈时已经是相应函数对应的字母.
代码如下:

#include <bits/stdc++.h>


using namespace std;


int priority(char s)
{
    if(s=='a'||s=='x'||s=='n')
        return 0;
    else if(s=='*')
        return 2;
    else if(s=='+')
        return 1;
}
bool isok(char s)
{
    if(s=='*'||s=='+'||s=='a'||s=='x'||s=='n')
        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));
        }
        else if(s[i]=='a')
        {
            getnum(st,f,e);
            st.push(f+e);
        }
        else if(s[i]=='x')
        {
            getnum(st,f,e);
            st.push(max(f,e));
        }
        else if(s[i]=='n')
        {
            getnum(st,f,e);
            st.push(min(f,e));
        }
    }
    return st.top();
}
int main()
{
    ios::sync_with_stdio(false);
    int t ;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        map<int,char>mp;
        int cnt=0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='a'||s[i]=='x'||s[i]=='n')
            {
                mp[cnt++]=s[i];
                s[i]='d';
            }
            else if(s[i]==',')
                s[i]=mp[--cnt];
        }
        //cout<<s<<endl;
        //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、付费专栏及课程。

余额充值