如何在包含空格的同时使java中缀为postfix

时间:2015-03-24 19:30:26

标签: java

我正在尝试创建一个java类,允许用户在放入计算器的标记时放入空格,然后将其转换为后缀。

但它没有提供正确的输出。例如,对于输入1+2,输出应为12+,但为12

import java.util.*;


public class Infix
{
    Stack loco = new Stack();

    //create a scanner
    //now to create a stack
    public String Prefix(String gordo)
    {
        //Here is where the Program Begin   

        //type the the regular expression
        String[] red;
        red=gordo.split("(?=[()+\\-*/])|(?<=[()+\\-*/])"); //tokenize the string include delimiter
        System.out.println("THE EXPRESSION IN INFIX IS");

        for(int k=0;i<red.length;k++)
        {
            red[i]=red[i].trim(); //remove white spaces
        }

        //now we will test out if what is stored is digit or character
        System.out.println("BREAKING IT ALL DOWN INTO A STRING OF CHARACTERS");
        String ramon;
        char[] c; //an array of characters
        char feo; // a single character

        String post=""; //this is where the post fix expression will be put in

        for(int i=0;i<red.length;i++)
        {

            ramon=red[i];
            c=ramon.toCharArray();

            for(int j=0;j<c.length;j++)
            {
                System.out.println(c[j]); //print what is stored in C
                feo=c[j];

                if(Character.isLetterOrDigit(feo) == true)
                {
                    post=post+feo; //add character to string to post fix

                }
                else if( feo == '(' )
                {
                    loco.push(feo);
                }
                else if( feo == ')')
                {
                    char look;
                    while((look = LookAt()) != '(')
                    {
                        post=post+look; //add it all in there
                        PopIt();
                    }


                }
                //this does the associtivity and the operator precdence
                //if the operator is lower or equal to the precedence change
                //the current operand pop it from stack and put it into output
               //string
                else 
                {
                    while(LaPrio(feo) <= LaPrio(LookAt()))
                    {
                        post=post+LookAt();
                        PopIt();

                    }

                }

            }
        }


        System.out.println("THIS IS THE POSTFIX EXPRESSION");
        return post;

    }


    //this will determine operator precedence 
    private int LaPrio(char operator)
    {
        if(operator == '/' || operator == '*' || operator == '%')
        {   
            return 2;
        }
        if(operator == '+' || operator == '-')
        {
            return 1;
        }

        return 0;

    }

    //this will do the see what is one top of the stack
    private  Character LookAt()
    {
        if( !loco.empty() == false) //if there no items it will return false plus ! make it true
        {
            return(Character) loco.peek();
        }
        else
            return 0;


    }

    private  void PopIt()
    {
        if(!loco.empty())
        {
            loco.pop();
        }

    }

}       

0 个答案:

没有答案