2016河南ACM省赛-A-表达式求值

描述

假设表达式定义为:
1. 一个十进制的正整数X是一个表达式。
2. 如果X和Y是表达式,则X + Y, X * Y也是表达式;优先级*高于+。
3. 如果X和Y是表达式,则函数Smax(X, Y)也是表达式,其值为:先分别求出X,Y值的各位数字之和,再从中选最大值。
4. 如果X是表达式,则(X)也是表达式。
例如:

表达式 12*(2+3)+Smax(333,220+280) 的值为69。

请你变成,对给定的表达式,输出其值。

标准输入
第一行: 表示要计算的表达式个数(1≤T≤10)
接下来有T行:每行是一个字符串,表示待求的表达式,长度≤1000

标准输出
对于每个表达式,输出一行,表示对应表达式的值。

样例

标准输入 标准输出
3 (此空格请忽略)
12+2*3 18
12*(2+3) 60
12*(2+3)+Smax(333,220+280) 69

注:表达式中出现的所有正整数≤1000,求出的所有表达式的值小于10^6.

题解

一道简单直观的字符串处理问题,需要用到栈,这里我使用的是双栈,一个数据栈,一个字符栈,另外需要着重注意的是Smax函数,这个函数是Smax(a,b)的形式,所以我们忽略掉Smax四个字符,直接处理检索到的“(”“,”“)”,也就是说将这个函数中的括号和其他括号统一规则处理,而将逗号和加号、乘号划为一类来处理。注意优先级问题即可。

代码C++

#include <iostream>
#include <algorithm>
#include <stack>
#include <string>

using namespace std;

string s;
stack<int> stnum;
stack<char> stopt;

int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        cin >> s;
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] >= '0' && s[i] <= '9')
            {
                int num = s[i] - '0';
                i++;
                while (s[i] >= '0' && s[i] <= '9' && i < s.length())
                {
                    num *= 10;
                    num += s[i] - '0';
                    i++;
                }
                //cout << num << '\n';
                stnum.push(num);
                if (i >= s.length())
                {
                    break;
                }
            }
            if (s[i] == '(')
            {
                stopt.push(s[i]);
            }
            else if (s[i] == '*')
            {
                stopt.push(s[i]);
            }
            else if (s[i] == 'S')
            {
                stopt.push('(');
                i += 4;
            }
            else if (s[i] == ',')
            {
                int num = stnum.top();
                stnum.pop();
                while (stopt.top() != '(')
                {
                    if (stopt.top() == '+')
                    {
                        num += stnum.top();
                        stnum.pop();
                    }
                    else if (stopt.top() == '*')
                    {
                        num *= stnum.top();
                        stnum.pop();
                    }
                    stopt.pop();
                }
                stnum.push(num);
                stopt.push(',');
            }
            else if (s[i] == '+')
            {
                int num = stnum.top();
                stnum.pop();
                while (!stopt.empty() && stopt.top() != '(' && stopt.top() != ',')
                {
                    if (stopt.top() == '+')
                    {
                        num += stnum.top();
                        stnum.pop();
                    }
                    else if (stopt.top() == '*')
                    {
                        num *= stnum.top();
                        stnum.pop();
                    }
                    stopt.pop();
                }
                stnum.push(num);
                stopt.push('+');
            }
            else if (s[i] == ')')
            {
                int num = stnum.top();
                stnum.pop();
                while (stopt.top() != '(')
                {
                    if (stopt.top() == '+')
                    {
                        num += stnum.top();
                        stnum.pop();
                    }
                    else if (stopt.top() == '*')
                    {
                        num *= stnum.top();
                        stnum.pop();
                    }
                    else if (stopt.top() == ',')
                    {
                        int ans=0,ans1=0;
                        while(num)
                        {
                            ans += num%10;
                            num/=10;
                        }
                        while(stnum.top())
                        {
                            ans1+=stnum.top()%10;
                            stnum.top()/=10;
                        }
                        num=max(ans,ans1);
                        stnum.pop();
                    }
                    stopt.pop();
                } 
                stnum.push(num);
                stopt.pop();
            }
        }

        int num = stnum.top();
        stnum.pop();
        while (!stopt.empty())
        {
            if (stopt.top() == '+')
            {
                num += stnum.top();
                stnum.pop();
            }
            else if (stopt.top() == '*')
            {
                num *= stnum.top();
                stnum.pop();
            }
            stopt.pop();
        }

        cout << num << '\n'; 
    }

    return 0;
}

2017.5.2 修改代码
之前补这道题时并没有提交,因为那时还没有什么 OJ 挂这道题,现在这个题在 NYOJ 上挂有,评论区有大神说我原来的代码有问题,所以我重新写了一遍,这次应该比较严谨了一些,并且在 NYOJ 上提交通过了,谢谢评论区不是泰迪的二哈的提醒(^__^) 嘻嘻……

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
5015次浏览 47人参与
# 你的实习产出是真实的还是包装的? #
1103次浏览 27人参与
# 巨人网络春招 #
11163次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
6898次浏览 36人参与
# 简历第一个项目做什么 #
31245次浏览 312人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186343次浏览 1114人参与
# MiniMax求职进展汇总 #
22877次浏览 293人参与
# 面试紧张时你会有什么表现? #
30328次浏览 188人参与
# 简历中的项目经历要怎么写? #
309361次浏览 4150人参与
# 网易游戏笔试 #
6304次浏览 83人参与
# 职能管理面试记录 #
10682次浏览 59人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6850次浏览 154人参与
# 从哪些方向判断这个offer值不值得去? #
56695次浏览 357人参与
# 腾讯音乐求职进展汇总 #
160391次浏览 1105人参与
# 小红书求职进展汇总 #
226845次浏览 1356人参与
# AI时代,哪些岗位最容易被淘汰 #
62375次浏览 727人参与
# 你怎么看待AI面试 #
179254次浏览 1163人参与
# 正在春招的你,也参与了去年秋招吗? #
362517次浏览 2631人参与
# 你的房租占工资的比例是多少? #
92123次浏览 896人参与
# 机械求职避坑tips #
94396次浏览 567人参与
# 校招笔试 #
466168次浏览 2950人参与
# 面试官最爱问的 AI 问题是...... #
27111次浏览 834人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务