题解 | #表达式求值#

表达式求值

http://www.nowcoder.com/practice/c215ba61c8b1443b996351df929dc4d4

//中缀表达式求和,懒得写注释了,就是数据结构书上的栗子,不会的同学自行复习
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 返回表达式的值
     * @param s string字符串 待计算的表达式
     * @return int整型
     */
    int solve(string s) {
        // write code here
        int stack1[100],top1=-1,top2=-1;
        char stack2[100];
        int sum=0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]>='0'&&s[i]<='9')
                sum=sum*10+s[i]-'0';
            else{
                if(sum!=0)
                {
                    stack1[++top1]=sum;
                    sum=0;
                }
                if(top2==-1||judge(s[i],stack2[top2])==true)
                    stack2[++top2]=s[i];
                else{
                    if(s[i]==')')
                    {
                        while(stack2[top2]!='(')
                        {
                            char ch=stack2[top2--];
                            int x=stack1[top1--];
                            int y=stack1[top1--];
                            if(ch=='+')
                                stack1[++top1]=x+y;
                            else if(ch=='-')
                                stack1[++top1]=y-x;
                            else if(ch=='*')
                                stack1[++top1]=x*y;
                            else stack1[++top1]=y/x;
                        }
                        top2--;
                    }
                    else{
                        while(top2!=-1&&judge(s[i],stack2[top2])==false)
                        {
                            char ch=stack2[top2--];
                            int x=stack1[top1--];
                            int y=stack1[top1--];
                            if(ch=='+')
                                stack1[++top1]=x+y;
                            else if(ch=='-')
                                stack1[++top1]=y-x;
                            else if(ch=='*')
                                stack1[++top1]=x*y;
                            else stack1[++top1]=y/x;
                        }
                        stack2[++top2]=s[i];
                    }
                }
            }
        }
        if(sum!=0)
            stack1[++top1]=sum;
        char ch=stack2[top2--];
        int x=stack1[top1--];
        int y=stack1[top1--];
                            if(ch=='+')
                                return x+y;
                            else if(ch=='-')
                                return y-x;
                            else if(ch=='*')
                               return x*y;
                       return y/x;
    }
    bool judge(char c1,char c2)
    {
        if(c1=='(')
            return true;
        else if(c1==')')
            return false;
        else if(c2=='(')
            return true;
        else if((c1=='*'||c1=='/')&&(c2=='+'||c2=='-'))
            return true;
        else return false;
    }
};
全部评论

相关推荐

05-22 09:23
门头沟学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
05-28 12:15
点赞 评论 收藏
分享
来个大佬救一下,为上投了都是石沉大海了,没实习经历的话怕秋招直接进不了面。什么实习这么难找,基本
心态爆炸了:现在正式的岗位都少,实习基本不咋招的,除了大厂,中小企业其实没那么多岗位需求,就算是有,大多都是招一两个廉价劳动力,同时,他们也会希望你一来就能干活的,没时间培训你,就让你了解公司的项目,你了解完就可以开始干活。再者是,很多低质量的实习其实用处没有那么大的。我去年也是找实习找到破防,最后去了一家深圳的小公司实习,工作对我来说很简单,甚至不如我在学校做的项目,秋招的时候,这段实习经历也并没有帮上什么忙,投递简历,依旧非常低的回复率。低回复率是常态,尤其是找实习,找不到,那就把重心放在优化自己的简历和项目,多看八股文,锻炼自己的面试能力,多看别人的面经,自己模拟面试,等秋招的时候,只要有那么寥寥几次,好好抓住那几次机会。
点赞 评论 收藏
分享
评论
4
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务