UVALive 6833

计算器

题目描述:

懒的描述了。。就是个中缀表达式变后缀然后计算值

题解:

首先是中缀表达式变后缀:对于有+-*/()的,先搞出符号优先级,分出L和R。然后扫中缀串,遇到数字就放到目标串中,遇到符号就尝试入栈操作根据优先级处理。
其次计算结果,遇到数字入栈,遇到符号计算然后入栈

重点:

复习中缀搞后缀。。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define CLR(a) memset(a, 0, sizeof(a))
#define REP(i, a, b) for(int i = a;i < b;i++)
#define REP_D(i, a, b) for(int i = a;i <= b;i++)

typedef long long ll;

using namespace std;

const int maxn = 300;
int lft[maxn], rgt[maxn];
char mys[maxn];
int stop;
char s[maxn];
char hou_s[maxn];
int hou_n;
int n;
int res;
void getNo()
{
    lft[' '] = 0;
    lft['+'] = 1;
    rgt['+'] = 2;
    lft['*'] = 3;
    rgt['*'] = 4;
}
void getHou()
{
    hou_n = 0;
    stop = 0;
    mys[0] = ' ';
    REP(i, 0, n)
    {
        if(isdigit(s[i]))
        {
            hou_s[hou_n] = s[i];
            hou_n++;
        }
        else
        {
            while(rgt[s[i]] < lft[mys[stop]])
            {
                hou_s[hou_n] = mys[stop];
                hou_n++;
                stop--;
            }
            stop++;
            mys[stop] = s[i];
        }
    }
    while(stop!=0)
    {
        hou_s[hou_n] = mys[stop];
        stop--;
        hou_n++;
    }
}
int cal()
{
    stack<int> s;
    //int ans = 0;
    REP(i, 0, n)
    {
        if(isdigit(hou_s[i]))
        {
            s.push(hou_s[i]-'0');
        }
        else
        {
            if(hou_s[i]=='+')
            {
                int a, b, c;
                a = s.top();
                s.pop();
                b = s.top();
                s.pop();
                c = a + b;
                s.push(c);
            }
            else
            {
                int a, b, c;
                a = s.top();
                s.pop();
                b = s.top();
                s.pop();
                c = a * b;
                s.push(c);
            }
        }
    }
    return s.top();
}
void solve()
{
    getHou();
    int ansM = cal();
    int ansL = s[0]-'0';
    REP(i, 1, n)
    {
        if(s[i] >= '0' && s[i] <= '9')
        {
            ;
        }
        else
        {
            if(s[i]=='+')
            {
                ansL = ansL + s[i + 1]-'0';
                i++;
            }
            else
            {
                ansL = ansL*(s[i+1]-'0');
                i++;
            }
        }
    }
    if(ansL==ansM)
    {
        if(res == ansL)
        {
            printf("U");
        }
        else
        {
            printf("I");
        }
    }
    else
    {
        if(res==ansL)
        {
            printf("L");
        }
        else if(res == ansM)
        {
            printf("M");
        }
        else
        {
            printf("I");
        }
    }
    printf("\n");
}


int main()
{
    //freopen("2Bin.txt", "r", stdin);
    //freopen("3Bout.txt", "w", stdout);
    getNo();
    while(scanf("%s", s) != EOF)
    {
        n = strlen(s);
        scanf("%d", &res);
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值