所以我有一个自己构建的堆栈,还有一个机器来评估诸如 (9 + 0) 之类的表达式,它们可能更复杂。我在命令行中运行它,然后当我输入示例( 9 + 5 )时,程序就停在那里。我可以得到一个新行,但表达式不计算。所以我的问题是我错过了什么。我确信有些东西我没有正确理解,并且我认为我遗漏了有关扫描仪或 Java 中数组的一些内容。

也许我昨晚在想我应该用 ArrayList 替换数组。这有道理吗?

这是固定容量堆栈

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()); 
} 

}

请您参考如下方法:

你的代码对我有用;我确实将其更改为使用 ArrayList,并且添加了一个像这样的peek

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 


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!