堆栈添加设备不能添加,但挂起等待更多ARGS [英] Stack Adding Machine doesn't add but hangs waiting for more args

查看:140
本文介绍了堆栈添加设备不能添加,但挂起等待更多ARGS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我有我建立了一个栈,我有一台机器前evalute pressions一样,(9 + 0),他们可以更复杂。我运行它的乐趣命令行,然后当我键入例如(9 + 5)该方案只是坐在那里。我可以得到一个新的生产线,但前pression不评估。所以我的问题是我错过了什么。我相信有,我没有理解错的,我在想,我失去了一些关于扫描仪或对一般的Java数组的东西。

so I have a Stack that I built and I have a Machine to evalute expressions like, ( 9 + 0 ) and they can be more complex. I run it fun the commandline and then when I type the example ( 9 + 5 ) the program just sits there. I can get a new line but the expression doesn't evaluate. So My question is what did I miss. I am sure there is something that I haven't understood correctly and I was thinking that I am missing something about the Scanner or about arrays in Java in general.

也许我在想昨晚,我应该用ArrayList的更换阵列。这是否有意义?

Perhaps I was thinking last night that I should replace arrays with ArrayList. Does this make sense?

下面是固定容量堆叠

public class FCStack<Item> {

private Item[] a; 
private int top; // pointer to top of Stack
private int capacity; // size of the Stack+1

public FCStack(int cap){
    capacity = cap;
    a = (Item[]) new Object[capacity];   
    top = 0;
}

public void push(Item i){ //will only push an Item to the Stack if there is room. 
    if (!isFull()) {
        a[top++] = i;
    }
}

public Item pop(){ //will only pop an Item from the stack if there is something to pop.
    if (!isEmpty()) {
        --top;
    }
    return a[top];
}

public boolean isFull(){ //returns true if is full
    return top == capacity;
}

public boolean isEmpty(){ //returns true if is empty
    return top == 0; 
}

public int size(){ //returns the current size of the stack+1 or the array index 
    return top;
}

}

下面是两个叠计算器

import java.io.*;
import java.util.Scanner;

public class TwoStackMaths {

public static void main (String[] args) {
    FCStack<String> ops = new FCStack<String>(10);
    FCStack<Double> vals = new FCStack<Double>(10);
    Scanner console = new Scanner(System.in);
    while(console.hasNext()) {
        String str = console.next();
        if (str.equals("("))
            ;
        else if (str.equals("+")) {
            ops.push(str);
        }
        else if (str.equals("-")) {
            ops.push(str);
        }
        else if (str.equals("*")) {
            ops.push(str); 
        }
        else if (str.equals("/")) {
            ops.push(str);
        }
        else if (str.equals("^")) {
            ops.push(str);
        }
        else if (str.equals(")")) {
            String op = ops.pop();
            double v = vals.pop();
            if (op.equals("+")) {
                v = vals.pop() + v;
            }
            else if (op.equals("-")) {
                v = vals.pop() - v;
            }
            else if (op.equals("*")) {
                v = vals.pop() * v;
            }
            else if (op.equals("/")) {
                v = vals.pop() / v;
            }
            else if (op.equals("^")) {
                v = Math.pow(v, vals.pop());
            }
            vals.push(v);
        }
        else {
        vals.push(Double.parseDouble(str));
        }
    }
    //console.close();
    System.out.println(vals.pop());
}

}

推荐答案

您code为我工作;我没有改变它使用ArrayList,而我添加了一个偷看像这样

Your code worked for me; I did change it to use an ArrayList, and I added a peek like this

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FCStack<T> {

  public static void main(String[] args) {
    FCStack<String> ops = new FCStack<String>(10);
    FCStack<Double> vals = new FCStack<Double>(10);
    Scanner console = new Scanner(System.in);
    try {
      while (console.hasNext()) {
        String str = console.next().trim();
        if (str.equals(".")) {
           System.out.println(vals.peek());
         } else if (str.equals("(")) {
            ;
         } else if (str.equals("+")) {
           ops.push(str);
         } else if (str.equals("-")) {
           ops.push(str);
         } else if (str.equals("*")) {
           ops.push(str);
         } else if (str.equals("/")) {
           ops.push(str);
         } else if (str.equals("^")) {
           ops.push(str);
         } else if (str.equals(")")) {
           String op = ops.pop();
           double v = vals.pop();
           if (op.equals("+")) {
             v = vals.pop() + v;
           } else if (op.equals("-")) {
             v = vals.pop() - v;
           } else if (op.equals("*")) {
             v = vals.pop() * v;
           } else if (op.equals("/")) {
             v = vals.pop() / v;
           } else if (op.equals("^")) {
             v = Math.pow(v, vals.pop());
          }
          vals.push(v);
        } else {
          vals.push(Double.parseDouble(str));
        }
     }
  } finally {
    console.close();
  }
}

private List<T> a;
private int top; // pointer to top of FCStack
private int capacity; // size of the FCStack+1

public FCStack(int cap) {
  capacity = cap;

  a = new ArrayList<T>();
  top = 0;
}

public void push(T i) { // will only push an Item to
                      // the FCStack if there is room.
  if (!isFull()) {
    a.add(i);
    ++top;
  }
}

public T pop() { // will only pop an Item from the
                 // stack if there is something to pop.
  if (!isEmpty()) {
    return a.remove(--top);
  }
  return null;
}

public T peek() {
  if (!isEmpty()) {
    return a.get(top - 1);
  }
  return null;
}

public boolean isFull() { // returns true if is full
  return top > capacity;
}

public boolean isEmpty() { // returns true if is empty
  return top == 0;
}

  public int size() { // returns the current size of the
                      // stack+1 or the array index
    return top;
  }
}

测试像这样

( 12.0 * 3.0 ) .
36.0

这篇关于堆栈添加设备不能添加,但挂起等待更多ARGS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆