另一种表达式求值

/*表达式求值
输入
3
12+2*3
12*(2+3)
12*(2+3)+Smax(333,220+280)
输出
18
60
69
*/
 


#include <stdio.h>  
#include <string.h>  
#include <algorithm>  
#include <stack>  
using namespace std;  
stack<char> s1;//字符栈  
stack<int> s2;  //数据栈 
char s[1010];  
int main()  
{  
    int T;  
    scanf("%d",&T);  
    while(T--)  
    {  
        int i,j;  
        getchar();  
        scanf("%s",&s[1]);  
        int len = strlen(&s[1]);  
        s[0] = '(';  
        s[len+1] = ')';  
        len++;  
        for(i=0;i<=len;i++)  
        {  
            if(s[i]=='(')  
            {  
                s1.push(s[i]);  
            }  
            else if(s[i]=='S')  
            {  
                s1.push('(');  
                s1.push('S');  
                i += 4;  
            }  
            else if(s[i]>='0' && s[i]<='9')  
            {  
                int v = 0;  
                while(s[i]>='0' && s[i]<='9')  
                {  
                    v = v*10+(s[i++]-'0');  
                }  
                i--;  
                s2.push(v);  
            }  
            else if(s[i]=='+' || s[i]=='-')  
            {  
                while(s1.top()!='(' && s1.top()!='S')  
                {  
                    int a = s2.top(); s2.pop();  
                    int b = s2.top(); s2.pop();  
                    int c;  
                    switch(s1.top())  
                    {  
                        case '+': c = b + a; break;  
                        case '-': c = b - a; break;  
                        case '*': c = b * a; break;  
                        case '/': c = b / a; break;  
                    }  
                    s2.push(c);  
                    s1.pop();  
                }  
                s1.push(s[i]);  
            }  
            else if(s[i]=='*' || s[i]=='/')  
            {  
                if(s1.top()=='*' )  
                {  
                    int a = s2.top(); s2.pop();  
                    int b = s2.top(); s2.pop();  
                    s2.push(b*a);  
                    s1.pop();  
                }  
                else if(s1.top()=='/')  
                {  
                    int a = s2.top();s2.pop();  
                    int b = s2.top();s2.pop();  
                    s2.push(b/a);  
                    s1.pop();  
                }  
                s1.push(s[i]);  
            }  
            else if(s[i]==')')  
            {  
                while(s1.top()!='(' &&  s1.top()!='S')  
                {  
                    int a = s2.top();s2.pop();  
                    int b = s2.top();s2.pop();  
                    int c,da = 0 ,db = 0;  
                    switch(s1.top())  
                    {  
                        case '+': c = b + a; break;  
                        case '-': c = b - a; break;  
                        case '*': c = b * a; break;  
                        case '/': c = b / a; break;  
                    }  
                    s2.push(c);  
                    s1.pop();  
                }  
                if(s1.top()=='S')     
                {  
                    int a=s2.top();s2.pop();  
                    int b=s2.top();s2.pop();  
                    int c,da = 0 ,db = 0;  
                    while(b!=0)  
                    {  
                        db += b%10;  
                        b /= 10;  
                    }  
                    while(a!=0)  
                    {  
                        da += a%10;  
                        a /= 10;  
                    }  
                    c = max(da,db);  
                    s2.push(c);  
                    s1.pop();  
                }  
                s1.pop();             
            }  
        }  
        printf("%d\n",s2.top());  
        s2.pop();  
    }  
    return 0;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值