zoj 1418||poj 1686 Lazy Math Instructor

第一次用后缀表达式求表达式的值,比中缀表达式求好写多了!1Y~

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>

using namespace std;

char opn[200],opnn=0,opc[200],opcn=0;
int dui[200],duin=0;

int cmp(char c)
{
    if(c=='+'||c=='-')return 1;
    if(c=='*'||c=='/')return 2;
    if(c=='('||c==')')return 3;
}

void compute(char ope)
{
    switch(ope)
    {
        case '+' : dui[duin-1]=dui[duin-1]+dui[duin];break;
        case '-'  : dui[duin-1]=dui[duin-1]-dui[duin];break;
        case '*'  : dui[duin-1]=dui[duin-1]*dui[duin];break;
        case '/' :  dui[duin-1]=dui[duin-1]/dui[duin];break;
    }
    duin--;
}

int result(char s[])
{
    int i=0,j;
    opnn=opcn=duin=0;
    while(s[i]!='#')
    {
        if(s[i]==' '||s[i]=='\t'||s[i]=='\n'){i++;continue;}
        if(s[i]>='0'&&s[i]<='9'){opn[++opnn]=s[i];i++;continue;}
        if(s[i]>='a'&&s[i]<='z'){opn[++opnn]=s[i];i++;continue;}
        if(s[i]=='('){opc[++opcn]=s[i];i++;continue;}
        if(s[i]==')')
        {
            while(opc[opcn]!='(')opn[++opnn]=opc[opcn--];
            opcn--;
            i++;
            continue;
        }
        while(opcn>0)
        {
            if(opc[opcn]=='(')break;
            if(cmp(opc[opcn])<cmp(s[i]))break;
            opn[++opnn]=opc[opcn];
            opcn--;
        }
        opc[++opcn]=s[i];
        i++;
    }
    for(i=opcn;i>0;i--)opn[++opnn]=opc[i];
    for(i=1;i<=opnn;i++)
    {
        if(opn[i]=='*'||opn[i]=='-'||opn[i]=='+'||opn[i]=='/')compute(opn[i]);
        else               {dui[++duin]=opn[i]-'0';}
    }
    return dui[1];
}

int main()
{
    int n,len1,len2;
    char s1[100],s2[100];
    scanf("%d",&n);
    getchar();
    while(n>0)
    {
        n--;
        memset(s1,0,sizeof(s1));
        memset(s2,0,sizeof(s2));
        gets(s1);
        gets(s2);
        len1=strlen(s1);
        len2=strlen(s2);
        s1[len1]='#';
        s2[len2]='#';
        if(result(s1)==result(s2))printf("YES\n");
        else                                             printf("NO\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值