PEMDAS计算器的奇数回报

时间:2014-02-02 23:28:04

标签: java

似乎这个计算器适用于所有其他情况,除了这样的情况:

(2 * 3 ^ 4)

它不会返回162,而是返回0.0。

我发现错误必须来自方法public static double operation,因为默认的return语句是0.0

以下是代码:

import java.util.Iterator;

import java.util.NoSuchElementException;

public class StackC<Item> implements Stack<Item> {

private Item[] a;         // array of items
private int N;            // number of elements on stack

public StackC() {
    a = (Item[]) new Object[2];
}

public boolean isEmpty() {
    return N == 0;
}

public int size() {
    return N;
}

private void resize(int capacity) {
    assert capacity >= N;
    Item[] temp = (Item[]) new Object[capacity];
    for (int i = 0; i < N; i++) {
        temp[i] = a[i];
    }
    a = temp;
}

public void push(Item item) {
    if (N == a.length) resize(2*a.length);    // double size of array if necessary
    a[N++] = item;                            // add item
}

public Item pop() {
    if (isEmpty()) 
        throw new NoSuchElementException("Stack underflow");
    Item item = a[N-1];
    a[N-1] = null;                              // to avoid loitering
    N--;
    // shrink size of array if necessary
    if (N > 0 && N == a.length/4) 
        resize(a.length/2);
    return item;
}

public Item peek() {
    if (isEmpty()) 
        throw new NoSuchElementException("Stack underflow");
    return a[N-1];
}

public Iterator<Item> iterator() {
    return new ReverseArrayIterator();
}

private class ReverseArrayIterator implements Iterator<Item> {
    private int i;

    public ReverseArrayIterator() {
        i = N;
    }

    public boolean hasNext() {
        return i > 0;
    }

    public void remove() {
        throw new UnsupportedOperationException();
    }

    public Item next() {
        if (!hasNext()) throw new NoSuchElementException();
        return a[--i];
    }
}

//---------------------------------------------------------------------

public static void main(String[] args) {
    StackC<String> Operator = new StackC<String>();
    StackC<Double> Values = new StackC<Double>();

    while (!StdIn.isEmpty()) {

        String token = StdIn.readString();

        try { 
            Double x = Double.parseDouble(token);
            Values.push(x);
        }
        catch(NumberFormatException nFE) {

        }

        if (token.equals("(")) 
            Operator.push(token); 

        if (token.equals(")")) 
        {
            if (Operator.peek() != "(")
            {
                String type = Operator.pop();
                double b = Values.pop();
                double a = Values.pop();

                Values.push(operation(type,a,b));
            }
            Operator.pop();
        }

        if(token.equals("*") || token.equals("+") || token.equals("/") || token.equals("-") || token.equals("^") )
        {
            if(!Operator.isEmpty())
            {
                String prev = Operator.peek();

                int x = comparePrecedence(token, Operator.peek()); // You need to compare precedence first

                if(x == -1 || x == 0)
                {

                    String type = Operator.pop();
                    double b = Values.pop();
                    double a = Values.pop();

                    Values.push(operation(type,a,b));
                }

            } 
            Operator.push(token);

        } 
    }

    while(!Operator.isEmpty())
    {

        String prev = Operator.peek();

        String type = Operator.pop();
        double b = Values.pop();
        double a = Values.pop();

        Values.push(operation(type,a,b));

    }
    System.out.println(Values.pop());
}

public static double operation(String operator, double a, double b) {  
    if (operator.equals("+"))  
        return a + b;  
    else if (operator.equals("-"))  
        return a - b;  
    else if (operator.equals("*"))  
        return a * b;
    else if (operator.equals("/"))  
        return a / b;
    else if (operator.equals("^"))
        return Math.pow(a,b);
    return 0.0;
} 

public static int comparePrecedence(String x, String y)
{
    int val1 = 0;
    int val2 = 0; 

    if(x.equals("-"))
        val1 = 0;
    if(y.equals("-"))
        val2 = 0;

    if(x.equals("+"))
        val1 = 1;
    if(y.equals("+"))
        val2 = 1;

    if(x.equals("/"))
        val1 = 2;
    if(y.equals("/"))
        val2 = 2;

    if(x.equals("*"))
        val1 = 3;
    if(y.equals("*"))
        val2 = 3;

    if(x.equals("^"))
        val1 = 4;
    if(y.equals("^"))
        val2 = 4;

    if(val1 > val2)
        return 1;
    else if(val2 > val1)
        return -1;
    else 
        return 0;
}

}

虚线上方的一切都是通过教授给出的,而不是代码的问题。

StdIn只是一种读取输入的方法。

0 个答案:

没有答案