在 Postfix 评估上实现 Java 算法时需要帮助

发布于 2024-12-01 21:05:34 字数 2523 浏览 0 评论 0原文

我尝试从头开始编写这段代码、编码并运行它,但它似乎不起作用。这被分配为课堂上的实验作业。要求是: 使用堆栈和堆栈操作(用户定义)实现后缀评估。 我认为我的程序的算法是正确的,但它总是给我错误的答案。 这是我的代码。

public class StackApplication {

    public static class Stack<T> {

        private int top = 0;
        private final static int stackMax=100;
        // highest index of stk array
        private Object[] stk = new Object[stackMax+1];
        //Elements must be cast back.

        public Stack() { // constructor
        }

        public boolean isEmpty(){
            if (top==0) return true;
            else return false;
        }

        public void push(T el) {
            if(top==stackMax)
                System.out.println("Stack push overflow error");
            else top=top+1;
            stk[top]=el;
        }

        public T pop(){
            if(isEmpty()){
                System.out.println("Stack push underflow error");
                return null;
            }
            else top=top-1;
            return(T)stk[top+1];
        }

        public T top(){
            if(isEmpty()){
                //System.out.println("Stack empty");
                return null;
            }
            else return (T)stk[top];
        }
    }
    public static boolean isOperator(char c){
        return(c=='+' || c=='-' || c=='/' || c=='*' || c=='^');
    }
    public static double evaluate(double x, char o, double y) {

        double result=0;
        switch(o) {
            case '+' : result=x+y; break;
            case '-' : result=x-y; break;
            case '*' : result=x*y; break;
            case '/' : result=x/y; break;
            case '^' : result=Math.pow(x, y);  break;
            default : break;    
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner console=new Scanner(System.in);
        Stack<Double> s=new Stack<Double>();

        System.out.println("Input Postfix form to evaluate:");
        String inp=console.nextLine();
        char[] chararray=inp.toCharArray();
        double b,a;

        for(int i=0; i<chararray.length; i++) {
            if(!isOperator(chararray[i]))
                s.push((double)chararray[i]);
            else {
                b=s.pop();
                a=s.pop();
                double c=evaluate(a, chararray[i], b);
                s.push(c);
            }
        }
        System.out.println(" " +s.pop());
    }
}

示例输出: 输入后缀形式进行评估:

23+ (Input)
101.0  (Output)
5.0 (Expected output) 

I've tried writing this code from scratch, coding, and running it but it just doesn't seem to work. This was assigned as lab work in class. The requirements are:
Implementing a postfix evaluation with the use of a stack and stack operations (user-defined).
I think the algorithm of my program is right, but it always gives me the wrong answer.
Here is my code.

public class StackApplication {

    public static class Stack<T> {

        private int top = 0;
        private final static int stackMax=100;
        // highest index of stk array
        private Object[] stk = new Object[stackMax+1];
        //Elements must be cast back.

        public Stack() { // constructor
        }

        public boolean isEmpty(){
            if (top==0) return true;
            else return false;
        }

        public void push(T el) {
            if(top==stackMax)
                System.out.println("Stack push overflow error");
            else top=top+1;
            stk[top]=el;
        }

        public T pop(){
            if(isEmpty()){
                System.out.println("Stack push underflow error");
                return null;
            }
            else top=top-1;
            return(T)stk[top+1];
        }

        public T top(){
            if(isEmpty()){
                //System.out.println("Stack empty");
                return null;
            }
            else return (T)stk[top];
        }
    }
    public static boolean isOperator(char c){
        return(c=='+' || c=='-' || c=='/' || c=='*' || c=='^');
    }
    public static double evaluate(double x, char o, double y) {

        double result=0;
        switch(o) {
            case '+' : result=x+y; break;
            case '-' : result=x-y; break;
            case '*' : result=x*y; break;
            case '/' : result=x/y; break;
            case '^' : result=Math.pow(x, y);  break;
            default : break;    
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner console=new Scanner(System.in);
        Stack<Double> s=new Stack<Double>();

        System.out.println("Input Postfix form to evaluate:");
        String inp=console.nextLine();
        char[] chararray=inp.toCharArray();
        double b,a;

        for(int i=0; i<chararray.length; i++) {
            if(!isOperator(chararray[i]))
                s.push((double)chararray[i]);
            else {
                b=s.pop();
                a=s.pop();
                double c=evaluate(a, chararray[i], b);
                s.push(c);
            }
        }
        System.out.println(" " +s.pop());
    }
}

Sample Output:
Input Postfix form to evaluate:

23+ (Input)
101.0  (Output)
5.0 (Expected output) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-12-08 21:05:34

问题出在这里:s.push((double)chararray[i]);。您无法通过这种方式将 char 转换为 double。您现在正在获取23 的ascii 代码。

50(ascii 代码 2) + 51(ascii 代码 3) = 101

这样做:s.push((double)(chararray[i] - '0')) ;

The problem is here: s.push((double)chararray[i]);. You can't convert char to double this way. You are now taking the ascii code of 2 and 3.

50(ascii code of 2) + 51(ascii code of 3) = 101

Do it like this: s.push((double)(chararray[i] - '0'));

迷你仙 2024-12-08 21:05:34

您正在执行 2 和 3 的 ASCII 代码相加,而不是 2 和 3 的 ASCII 代码相加。2

的代码是 50,3 的代码是 51,所以您的输出是 101,在本例中这是正确的。

当您按下时,请按下 chararray[i]-'0'。这将解决您的问题。

Your are doing the addition of the ASCII codes for 2 and 3, not of 2 and 3.

The code for 2 is 50 and for 3 is 51, so your out is 101, which is correct in this case.

When you push, push chararray[i]-'0'. This will solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文