标题:关于一个转换为后缀表达式的题,不知道为什么有个地方一直报错
只看楼主
神经彬
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2016-10-11
结帖率:100%
 问题点数:0 回复次数:1 
关于一个转换为后缀表达式的题,不知道为什么有个地方一直报错
package mooc.fbb;

import java.util.Scanner;
import java.util.Stack;

public class 表达式计算 {
    private static Stack<String> num = new Stack<String>();//存后缀表达式
    private static Stack<String> sign = new Stack<String>();//存入符号
   

    public static void getGroup(String line){//讲字符串转换为后缀表达式
        for(int i=0; i<line.length(); i++){
            char c = line.charAt(i);
            //System.out.print((int)c+" ");
            if((int)c>=48 && (int)c<=57){//当遇到数字的时候,判断是不是多位数,然后在push进num
                int j = i+1;
                while(j<line.length() && (line.charAt(j)>=48 && line.charAt(j)<=57)){
                    j++;
                }
                num.push(line.substring(i, j));
                i = j-1;
            }else if(c == '('){//遇到左括号直接存进num
                sign.push(String.valueOf(c));
            }else if(c == ')'){//遇到右括号从sign中pop栈顶元素push到num知道遇到'(',然后再pop掉'('
                while(!sign.peek().equals("(")){
                    num.push(sign.pop());
                }
                sign.pop();
            }else{
                int n = 0;
                if(!sign.empty()){//如果sign中没有元素,直接令n = 0
                    n = getNum(sign.peek().charAt(0));
                }
                //System.out.print(n+" ");
                int m = getNum(c);
                //System.out.println(m);
                if(m >= n){//如果当前元素的运算级别比栈顶元素运算级别要高,就直接push进sign
                    sign.push(String.valueOf(c));
                }else{
                    while(m < n){//如果当前运算运算级别比sign栈顶元素运算级别要低,就将sign栈顶元素pop并且push进num,知道不符合条件
                        //System.out.println(sign.pop());
                        num.push(sign.pop());//输入例子2*3+6/3的时候,这里一直报错
                        if(!sign.empty()){
                            n = getNum(sign.peek().charAt(0));
                        }
                    }
                }
                //System.out.println(sign);
            }
        }
        while(!sign.empty()){
            num.push(sign.pop());
        }
    }
   

    private static int getNum(char c){
        int n = 0;
        switch(c){
            case '+':
            case '-':
                n = 1;
                break;
            case '*':
            case '/':
                n = 2;
                break;
        }
        return n;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        getGroup(line);
        System.out.println(num);
        //char[] exp = getGroup(line);//讲字符串转换为后缀表达式
    }

}
搜索更多相关主题的帖子: String num line int push 
2017-08-04 09:44
神经彬
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2016-10-11
得分:0 
代码中标记红色位置的地方,不知道为啥一直报错,大家帮个忙解答一下
2017-08-04 09:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-479778-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.208558 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved