nyoj305

题意:表达式求值
思路:栈的运用。
AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
using namespace std;
const int maxn=500;
char ch[maxn];

void calc(char *ch){
    stack<int>s;   //数字
    stack<char>ss;  // 运算符号
    stack<char>sss; //比较符号
    int len=strlen(ch);
    for(int i=0; i<len; i++){
        if(ch[i]==',') continue;
        if(ch[i]=='m') continue;
        if(ch[i]=='a' && ch[i+1]=='d') {
            sss.push(ch[i+1]); i+=2;
        }else if(ch[i]=='i'){
            sss.push(ch[i]); i+=1;
        }else if(ch[i]=='a' && ch[i+1]=='x'){
            sss.push(ch[i]); i+=1;
        }
        else if(ch[i]=='('){
            ss.push(ch[i]);
        }
        else if(ch[i]==')'){
            int xx=s.top();
            s.pop();
            int yy=s.top();
            s.pop();
            if(sss.top()=='i'){
                s.push(min(xx,yy));
            }else if(sss.top()=='d'){
                s.push(xx+yy);
            }else if(sss.top()=='a') {
                s.push(max(xx,yy));
            }
            sss.pop();
            ss.pop();
        }
        else if(ch[i]>='0' && ch[i]<='9'){
            int xx=0;
            while(ch[i]>='0' && ch[i]<='9'){
                xx=xx*10+ch[i]-'0'; i++;
            }
            i=i-1;
            s.push(xx);
        }
    }
    printf("%d\n",s.top());
}


int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",ch);
        calc(ch);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值