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

来源:互联网 发布:大众软件pdf 编辑:程序博客网 时间:2024/05/21 04:21

不了解表达式求值请访问
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;}
原创粉丝点击