後置式を評価するこのプログラムを作成しました。1 桁の数字のみが使用されている場合は問題なく動作します。
私の問題は、入力にスペースが含まれている場合に複数桁の数字をプッシュするにはどうすればよいですか?
元。入力: 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);
            }
}