题解 | #四则运算#

四则运算

https://www.nowcoder.com/practice/9999764a61484d819056f807d2a91f1e

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
	    // 处理空格和多样括号 
        str = str.replaceAll("\\s","").replaceAll("\\[","(").replaceAll("\\]",")").replaceAll("\\{","(").replaceAll("\\}",")");
        char[] c = str.toCharArray();

        Stack<Integer> s1 = new Stack<>();
        Stack<Character> s2 = new Stack<>();
        StringBuilder number = new StringBuilder();

        Map<Character,Integer> priority = new HashMap<>();
        priority.put('+',1);
        priority.put('-',1);
        priority.put('*',2);
        priority.put('/',2);
        priority.put('(',0);

        for(int i=0;i<c.length;i++){
            char cc = c[i];
		    // ‘-’出现在第一个或者前面是运算符+'('时 负数
            if(cc=='-' && (i==0 || isSymbol(c[i-1]))){
                number.append(cc);
            }else if(Character.isDigit(cc)){
                number.append(cc);
			    // 最后一个或者下一个不是数字时,多位数结束存入栈1中
                if(i==c.length-1 || !Character.isDigit(c[i+1])){
                    s1.push(Integer.parseInt(number.toString()));
                    number.setLength(0);
                }
            }else if(cc=='('){
                s2.push(cc);
            }else if(cc==')'){
                while(s2.peek()!='('){
                    fun(s1,s2);
                }
                s2.pop();
            }else{
			    // 当前字符的优先级<=栈顶元素的优先级时 先计算前面的再放入栈
                while(!s2.isEmpty() && priority.get(cc) <= priority.get(s2.peek())){
                    fun(s1,s2);
                }
                s2.push(cc);
            }
        }

        while(!s2.isEmpty()){
            fun(s1,s2);
        }

        System.out.println(s1.peek());
    }

    public static boolean isSymbol(char c){
        if(c=='+' || c=='-' || c=='*' || c=='/' || c=='(') return true;
        else return false;
    }

    public static void fun(Stack<Integer> s1, Stack<Character> s2){
        char c = s2.pop();
        int b = s1.pop();
        int a = s1.pop();
        switch(c){
            case'+':
                s1.push(a+b);
                break;
            case'-':
                s1.push(a-b);
                break;
            case'*':
                s1.push(a*b);
                break;
            case'/':
                s1.push(a/b);
                break;
        }
    }
}

全部评论

相关推荐

吾族血脉,自吾始立铁律:凡我子孙,胆敢研习计算机之术者,当受七窍流血之刑!若见Python之书,必遭雷殛;若触Java代码,定为不孝!键盘鼠标准入族谱秽物录,显示器乃摄魂邪镜祖祠前当立戒碑:"二进制者,断子绝孙之道也!"算法者,乱我族心智之毒也!数据结构,毁我门风之刃也!倘有逆子偷装&nbsp;vscode,即按祖规捆于祠堂梁柱,令其DEBUG至死不得解脱!今颁天条三则:壹)三代血亲不得报考计算机系违者削去辈分,永世称码奴贰)族中幼童须背《戒算经》"if-else咒,switch符,皆是断头术"叁)凡见子侄讨论编程者须即刻砸其电脑,焚其书籍泼黑狗血于键盘之上!太祖母口谕:"吾宁要文盲孙,不要程序员!"尔...
好吃的薯饼:姐妹这不是我们计算机系吧,我们计算机系的都在言情小说里当黑客大佬,各种竞赛拿奖拿到手软,公司系统道路监控随便入侵。身体线条非常优美,挺拔的站姿十分端正,给人以强壮有内涵的感觉。脸庞轮廓深刻,五官分明透露着对太阳底下最光辉的职业的向往和坚定,尤其是那双深邃的眼睛,写满了对代码和计算机系统的热情和无限的活力。我们计算机系是天之骄子、明日之星,人手一个博士学位不然高中电脑老师都当不上。组会的时候,面对导师和同事的疑难问题,也能够回答自如。我们总是把高高的发际线当做荣耀的象征。妈咪这不素我们计算机系吧,集美集帅怎么只会写hello world?
点赞 评论 收藏
分享
FieldMatching:看成了猪头顾问,不好意思
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务