在Java中乘以除后缀数

时间:2018-10-07 02:12:32

标签: java

对于我的作业,我需要将方程式放入后缀形式并将其划分。我能够正确添加数字。但是,当存在除法,乘法或括号过多时,我无法使它们正确解决。我不太确定我的代码有什么问题。在下面的代码中,我有等式:(5 + 3)*(8 + 2)。该公式的结果应为80,但这是结果:

My result when it solves (5+3)*(8+2)

这是我的代码。我有两节课。这是主要的类:

package stack;

import javaapplication2.Init;

public class st {

    public static void main(String[] args) {
        String aa = "(5+3)*(8+2)";
        Init init = new Init(aa);
         }

    } 

这是最后一堂课:

 package javaapplication2;

import java.util.Arrays;

public final class Init {

    public int top = -1;
    Integer[] stack = new Integer[10];
    String[] queue = new String[10];
    int number, x, y, z, op, front, rear;
    int finalValue;
    int CurrValue;
    String queueOperand;

    Character s;

    public Init(String s) {
        ValidateEquation(s);
        int finalVal = postFix(s);
        System.out.printf("The answer is " + finalVal + "\n");
    }

    void ValidateEquation(String s) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(') {
                push(c);
            } else if (c == ')') {
                if (isStackEmpty()) {
                    System.out.printf("Error: Too many ')'");
                    System.exit(0);
                } else {
                    int closeParen = pop();
                }
            }
        }
    }

    public int postFix(String s) {
        int CurrentCalculation;

        int i = 0;
        while (i < s.length()) {
            char c = s.charAt(i);

            if ((int) c > 47 && (int) c < 58) {
                int numVal = Character.getNumericValue(c);
                push(numVal);
            } else if ((int) c > 41 && (int) c < 48) {
                String StrVal = Character.toString(c);
                pushQ(StrVal);
            } 

            if (c == '(' || c == ')' || i == s.length()-1) {
                String newString = "";

                for (Integer stack1 : stack) {
                    /* iterate through the stack */
                    if (stack1 != null) {
                        newString = newString + stack1 + " ";
                    }
                }
                for (String queue1 : queue) {
                    /* iterate through the queue */
                    if (queue1 != null) {
                        newString = newString + queue1 + " ";
                        queueOperand = queue1;
                    }
                }

                if (newString.length() > 2) {
                    int CurrValue = calculateEquation(newString);

                    if (null != queueOperand) switch (queueOperand) {
                        case "+":
                            finalValue = finalValue + CurrValue;
                            break;
                        case "-":
                            finalValue = finalValue - CurrValue;
                            break;
                        case "*":
                            finalValue = finalValue * CurrValue;
                            break;
                        case "/":
                            finalValue = finalValue / CurrValue;
                            break;
                        default:
                            break;
                    }
                    popAll();

                 }
            }
            i++;

        }

        return finalValue;
    }

    int calculateEquation(String s) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if ((int) c > 47 && (int) c < 58) {
                int numVal = Character.getNumericValue(c);
                push(numVal);
            }
            if ((int)c > 41 && (int)c < 48) {
                if (s.length() <= 4) {
                    x = pop();
                    if (c == '*' || c == '/') {
                        y = 1;
                    } else {
                        y = 0;
                    }
                } else {
                    x = pop();
                    y = pop();
                }

                System.out.println("\n" + x + " " + y + " " + c);

                if (c == '+') {
                    z = x + y;
                } else if (c == '-') {
                    z = x - y;
                } else if (c == '*') {
                    z = x * y;
                } else if (c == '/') {
                    z = x / y;
                }
            }
        }
        return z;
    }

    void push(int x) {
        top = top + 1;
        /* Increment stack pointer. */
        stack[top] = x;
        /* Place x on top of stack. */
    }

    void pushQ(String x) {
        rear = rear + 1;
        /* Increment stack pointer. */
        queue[rear] = x;
        /* Place x on top of stack. */
    }

    int pop() {
        int x;
         x = stack[top];
        /* Retrieve item from top of stack. */
        top = top - 1;
        /* Decrement stack. */
        return x;
    }

    void popAll() {
        Arrays.fill(stack, null);
        Arrays.fill(queue, null);
    }

    boolean isStackEmpty() {
        boolean empty;
        empty = false;
        if (top == -1) {
            /* If top = -1, that indicates an empty stack. */
            empty = true;
        }
        return empty;
    }

}

0 个答案:

没有答案
相关问题