0

我制作了这个评估后缀表达式的程序。如果只使用一位数字,它就可以正常工作。

我的问题是如果输入有空格,我如何推送多位数字?

前任。输入:23+34*- 输出为-7

但是如果我输入: 23 5 + 输出只有 3(这是空格前的数字),它的输出应该是 28

我的代码:

public class Node2
{
    public long num;
    Node2 next;
    Node2(long el, Node2 nx){
        num = el;
        next = nx;
    }
}


class stackOps2
    {
        Node2 top;
        stackOps2(){
            top = null;
        }

        public void push(double el){
            top = new Node2(el,top);
        }

        public double pop(){
            double temp = top.num;
            top = top.next;
            return temp;
        }

        public boolean isEmpty(){
            return top == null;
        }
    }




public class ITP {

static stackOps2 so = new stackOps2();
public static final String operator = "+-*/^";




    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the infix:");
           String s = input.next();
               String output;
    InToPost theTrans = new InToPost(s);
    output = theTrans.doTrans();
    System.out.println("Postfix is " + output + '\n');
 System.out.println(output+" is evaluated as: "+evaluate(output));
  }

    public static double evaluate(String value)throws NumberFormatException{

            for(int i=0;i<value.length();i++){
                char val = value.charAt(i);
                          if(Character.isDigit(value.charAt(i))){
                    String v = ""+val;
                    so.push(Integer.parseInt(v));
                }
                else if(isOperator(val)){
                    double rand1=so.pop();
                    double rand2=so.pop();
                    double answer ;
                    switch(val){
                        case '+': answer = rand2 + rand1;break;
                        case '-': answer = rand2 - rand1;break;
                        case '*': answer = rand2 * rand1;break;
                            case '^': answer = Math.pow(rand2, rand1);break;
                            default : answer = rand2 / rand1;break;
                    }
                    so.push(answer);
                    }
                    else if(so.isEmpty()){
                        throw new NumberFormatException("Stack is empty");
                    }
                } 
                return so.pop();
            }

            public static boolean isOperator(char ch){
                String s = ""+ch;
                return operator.contains(s);
            }

}
4

1 回答 1

1

这是一个小型的、独立的示例,它执行所有的字符串解析和评估。与您的示例的唯一区别是它一次接受整个字符串,而不是使用扫描仪。请注意Integer.parseInt您的示例中缺少的 -- 的使用。我认为您可以根据需要轻松扩展它。

@SuppressWarnings({"rawtypes", "unchecked"})
public static void main(String[] args) {
  final String in = "5 9 + 2 * 6 5 * +";
  final Deque<Object> s = new LinkedList();
  for (String t : in.split(" ")) {
    if (t.equals("+")) s.push((Integer)s.pop() + (Integer)s.pop());
    else if (t.equals("*")) s.push((Integer)s.pop() * (Integer)s.pop());
    else s.push(Integer.parseInt(t));
  }
  System.out.println(s.pop());
}
于 2012-04-22T16:34:24.517 回答