水3_NYOJ_305

来源:互联网 发布:什么是好的相声知乎 编辑:程序博客网 时间:2024/05/01 03:45

3
add(1,2)
max(1,999)
add(min(1,1000),add(100,99))
求表达式的值,栈的使用!

#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<stack>using namespace std;char b[1005];char aa[] = "add",bb[]="min",cc[]="max";struct node{    char d[100];};stack<node> S1;stack<double> S2;node p;double x,y;int g,w;int main(){    int t;    scanf("%d",&t);    while(t--)    {        while(!S1.empty())S1.pop();        while(!S2.empty())S2.pop();        scanf("%s",b);        for(int i = 0; b[i];i++)        {            if(b[i] == 'a')            {                strcpy(p.d, aa);                S1.push(p);                i += 2;            }            else if(b[i] == 'm')            {                if(b[i+1] == 'i')                {                    strcpy(p.d, bb);                    S1.push(p);                    i += 2;                }                else                {                    strcpy(p.d, cc);                    S1.push(p);                    i += 2;                }            }            else if(b[i] >= '0' && b[i] <= '9')            {                x = 0;                while(b[i]>='0' && b[i] <= '9')                {                    x = x*10+(b[i]-'0');                    i++;                }                if(b[i]=='.')                {                    g = 0,w = 1;                    i++;                    while(b[i]>='0' && b[i] <= '9')                    {                        g = (g*10)+(b[i]-'0');                        w = w*10;                        i++;                    }                    x += (g*1.0/w);                }                i--;                S2.push(x);            }            else if(b[i] == ')')            {                 x = S2.top();S2.pop();                 y = S2.top();S2.pop();                p = S1.top();S1.pop();                if(strcmp(p.d,aa)==0)                {                    x += y;                    S2.push(x);                }                else if(strcmp(p.d,bb)==0)                {                    x = x<y?x:y;                    S2.push(x);                }                else                {                    x = x>y?x:y;                    S2.push(x);                }            }            else            {            }        }        double ans = S2.top();        cout<<ans<<endl;    }    return 0;}
0 0
原创粉丝点击