后缀表达式评估。

时间:2016-05-08 14:09:43

标签: java evaluation

以下java代码是从中缀转换为后缀并评估后缀表达式。我在Eval类中获得了数组索引超出范围的异常,并且不知道为什么。请帮忙!!!!
这与指出的内容并不重复。我没有完成该问题答案中指出的错误。

import java.util.Scanner;

 class I2F2 
{
    int top = -1;
    char mystack[] = new char[25];
    String  arr = "";



    void push(char ch)
    {
        mystack[++top] = ch;
    }

    char pop()
    {
        return mystack[top--];
    }

    char peek()
    {
        return mystack[top];
    }

    boolean isEmpty(){
    return top==-1;
    }

    int prec(char ch)
    {
        switch(ch)
        {
            case '+': return 1;
            case '-': return 1;

            case '*': return 2;
            case '/': return 2;

            case '^': return 3;
        }
        return 0;
    }

    boolean isAlpha(char ch)
    {
        return (ch >= 'a' && ch<= 'z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9');
    }

    boolean isOperator(char ch)
    {
        return ch=='/' || ch=='*' || ch=='+' || ch=='-' || ch=='^';
    }

    void infixToPost(String str)
    {

        char ch;
        int a = 0;

        for (int i=0; i < str.length(); i++)
        {
            ch = str.charAt(i);
            if (isAlpha(ch))
            {
                arr+= ch;
            }

            else if (ch == '(' || ch == '{' || ch == '[')
            {
                push(ch);
            }        
        else if (ch==')' || ch=='}' || ch==']')
            {               
                while( !(peek() == '(' || peek() == '{' || peek() == '[') )
                {                        
                    arr+= pop();
                }
                pop();
            }

            else if (isOperator(ch))
            {
                if ( isEmpty() || (prec(ch)>prec(peek())) || (peek() == '('))
                    push(ch);            

                  else if (prec(ch)<=prec(mystack[top]))
                {
                    while (prec(ch)<=prec(mystack[top]) )
                    {
                        arr+= pop();
                        if (top == -1)
                            break;
                    }
                    push(ch);                        
                }
            }
        }

        while (top != -1)
            arr+= pop();



            System.out.print(arr);

    }
}
    class Eval
    {
        I2F2 o = new I2F2(); 
        int no1, no2;
        int stack1[] = new int[25];
        int t = -1;

        void push(int i)
        {
         t = t + 1;
         stack1[t] = i;
        }

        int pop()
        {
            int e = stack1[t];
            t = t-1;
            return e;
        }

        int pEval()
        {
        for (int i = 0; i < o.arr.length(); i++)
        {
        char ch = o.arr.charAt(i);
        if (Character.isDigit(ch))
        {
            push(ch);
        }

        else
        {
             no2 = pop();
             no1 = pop();

             switch(ch)
             {
                 case '+': push ((no1+no2)); break;
                 case '-': push(  (no1-no2)); break;
                 case '*': push(  (no1*no2)); break;
                 case '/': push(  (no1/no2)); break;
                 case '%': push(  (no1%no2)); break;
                 case '^': push((int)  (Math.pow(no1,no2))); break;

             }

         }
        }
        return pop();
    }
    }

class MyMain
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter  an infix expression: ");
        String s = sc.nextLine();


        I2F2 obj = new I2F2();
        Eval obj2 = new Eval();

        obj.infixToPost(s);
        System.out.println(obj2.pEval());
    }    
}

0 个答案:

没有答案