表达式求值

http://acm.nyist.net/JudgeOnline/problem.php?pid=35

 
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+7;
const int inf=0x3f3f3f3f;
char str[N];
stack<double> num;
stack<char> op;
int priority(char c)
{
    if(c=='+') return 1;
    if(c=='-') return 1;
    if(c=='*') return 2;
    if(c=='/') return 2;
    return 0;
}
void calculate()
{
    double b=num.top();num.pop();
    double a=num.top();num.pop();
    switch(op.top())
    {
        case '+': num.push(a+b);break;
        case '-': num.push(a-b);break;
        case '*': num.push(a*b);break;
        case '/': num.push(a/b);break;
    }
    op.pop();
}
int main()
{
    int i,j,t,n,m,tt=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("\n%s",&str);
        while(!op.empty()) op.pop();
        while(!num.empty()) num.pop();
        int len=strlen(str);
        for(i=0;i<len;i++)
        {
            if(isdigit(str[i]))
            {
                double tmp;
                sscanf(str+i,"%lf%n",&tmp,&n);
                i+=(n-1);
                num.push(tmp);
            }
            else if(str[i]=='(') op.push(str[i]);
            else if(str[i]==')')
            {
                while(op.top()!='(') calculate();
                op.pop();
            }
            else if(op.empty()||priority(str[i])>priority(op.top())) op.push(str[i]);
            else
            {
                while(!op.empty()&&priority(str[i])<=priority(op.top()))calculate();
                op.push(str[i]);
            }
        }
        op.top();
        printf("%.2lf\n",num.top());
        num.pop();
    }
    return 0;
}
        
http://acm.nyist.net/JudgeOnline/problem.php?pid=1272

 
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+7;
const int inf=0x3f3f3f3f;
char str[N];
stack<int> num;
stack<char> op;
int priority(char c)
{
    if(c=='*') return 2;
    if(c=='+') return 1;
    return 0;
}
void calculate()
{
    int suma=0,sumb=0;
    int b=num.top();num.pop();
    int a=num.top();num.pop();
    switch(op.top())
    {
        case '+': num.push(a+b);break;
        case '*': num.push(a*b);break;
        case 'S': while(b!=0) sumb+=b%10,b/=10;
                  while(a!=0) suma+=a%10,a/=10;
                  num.push(max(suma,sumb));break;
    }
    op.pop();
}
int main()
{
    int i,j,t,n,m,tt=0,tmp;
    scanf("%d",&t);
    while(t--)
    {
        scanf("\n%s",&str);
        while(!op.empty()) op.pop();
        while(!num.empty()) num.pop();
        int len=strlen(str);
        for(i=0;i<=len;i++)
        {
            if(isdigit(str[i]))
            {
                sscanf(str+i,"%d%n",&tmp,&n);
                i+=(n-1);
                num.push(tmp);
            }
            else if(str[i]=='(') op.push(str[i]);
            else if(str[i]==')')
            {
                while(op.top()!='(') calculate();
                op.pop();
            }
            else if(str[i]=='S')
            {
                op.push('(');op.push('S');
                i+=4;
            }
            else if(str[i]==',') {}
            else if(op.empty()||priority(str[i])>priority(op.top())) op.push(str[i]);
            else
            {
                while(!op.empty()&&priority(str[i])<=priority(op.top()))
                    calculate();
                op.push(str[i]);
            }
        }
        printf("%d\n",num.top());
    }
    return 0;
}
        


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值