簡體   English   中英

我應該如何處理在 Eclipse IDE 中折射寫得不好的 Reverse Polish Notation java 代碼

[英]How should i approach refracting the badly written Reverse Polish Notation java code in Eclipse IDE

在 Eclipse 中重構以下寫得很糟糕的逆波蘭表示法時,我很難知道從哪里開始。 並且想知道在嘗試重構代碼時一般從哪里開始。

import java.util.Scanner;

class StackNode 
{
    public StackNode(double data, StackNode underneath) 
    {
        this.data = data;
        this.underneath = underneath;
    }

    public StackNode underneath;
    public double data;
}


class RPN 
{
    public void into(double new_data) 
    {
        StackNode new_node = new StackNode(new_data, top);
        top = new_node;
    }

    public double outof( ) 
    {
        double top_data = top.data;
        top = top.underneath;
        return top_data;
    }

    public RPN(String command) 
    {
        top = null;
        this.command = command;
    }

    public double get( ) 
    {
        double a, b;
        int j;

        for(int i = 0; i < command.length( ); i++) 
        {
            // if it's a digit
            if(Character.isDigit(command.charAt(i))) 
            {
                double number;

                // get a string of the number
                String temp = "";
                for(j = 0; (j < 100) && (Character.isDigit(command.charAt(i)) || (command.charAt(i) == '.')); j++, i++) 
                {
                    temp = temp + String.valueOf(command.charAt(i));
                }

                // convert to double and add to the stack
                number = Double.parseDouble(temp);
                into(number);
                } else if(command.charAt(i) == '+') {
                    b = outof( );
                    a = outof( );
                    into(a + b);
                } else if(command.charAt(i) == '-') {
                    b = outof( );
                    a = outof( );
                    into(a - b);
                } else if(command.charAt(i) == '*') {
                    b = outof( );
                    a = outof( );
                    into(a * b);
                } else if(command.charAt(i) == '/') {
                    b = outof( );
                    a = outof( );
                    into(a / b);
                } else if(command.charAt(i) == '^') {
                    b = outof( );
                    a = outof( );
                    into(Math.pow(a, b));
                } else if(command.charAt(i) != ' ') {
                    throw new IllegalArgumentException( );
            }
        }

        double val = outof( );
        if(top != null) 
        {
            throw new IllegalArgumentException( );
        }

        return val;
    }

    private String command;
    private StackNode top;

    /* main method */
    public static void main(String args[]) 
    {
        Scanner input = new Scanner(System.in);
        while(true) 
        {
            System.out.println("Enter RPN expression or \"quit\".");
            String line = input.nextLine( );
            if(line.equals("quit")) 
            {
                input.close();
                break;
            } else {
                RPN calc = new RPN(line);
                System.out.printf("Answer is %f\n", calc.get( ));
            }
        }
    }
}

任何事情都會有所幫助謝謝。 PS抱歉,代碼太長了,我不能讓它更短。

您可以使用SonarLint(Eclipse 擴展)來幫助您在編寫代碼時檢測和修復質量問題。 您可以從Eclipse Marketplace下載它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM