0
votes

I am trying to parse a string with both negative numbers and minus signs and add each token to a queue called infix and a function to tell if the token is an operator. However the negative gets treated like a minus sign and is added to queue separate from the negative number. Here is my code to break the string into numbers and add them to a queue.

for(int i = 0;i < expression.length();i++) // add all items from the expression to the infix queue
        {
            String value = String.valueOf(expression.charAt(i));

            if(eval.isOperator(value) == true)
            {
                infix.add(value);
            }
            else
            {
                for(int j = i+1; eval.isOperator(String.valueOf(expression.charAt(j))) == false; j++)//add token to value until an operator is found and that number ends
                {
                    value += expression.charAt(j);
                    i++;
                }

                    infix.add(value);

            }
        }

and the eval class

public class Eval 
{
    public boolean isOperator(String n)
    {
        switch(n)
        {
        case "(":
            return true;
        case ")":
            return true;
        case "*":
            return true;
        case "/":
            return true;
        case "+":
            return true;
        case "-":
            return true;
        case "#":
            return true;
        }

        return false;
    }

    public int Priority(String op)
    {
        switch(op)
        {
        case "(":
            return 3;
        case "*":
            return 2;
        case "/":
            return 2;
        case "+":
            return 1;
        case "-":
            return 1;

        }
        return 0;
    }

}

    }
2
what is infix?user2201041
What about not treating - as an infix operator? If you just think of it as just a part of a number (operand), and automatically add a + operator if you see a positive number followed by a negative number, it should work fine.Sweeper
Please provide some small concrete examples of the behavior you want and behavior you don't want.ᴇʟᴇvᴀтᴇ
A queue i am adding the tokens from the string toGeoffM

2 Answers

0
votes

The way most languages handle this is by doing it later. As @Sweeper said, - would be an operator, and later code would select whether this is a binary or unary operator. Assuming you plan to evaluate these expressions at some point, doing it this way would really not be much extra work in the end.

To handle it your way, though, I would first extract some function, perhaps eval.lexNumber(String expression, int index) from your loop here

for(int j = i+1; eval.isOperator(String.valueOf(expression.charAt(j))) == false; j++)

Then it's just a matter of an explicit check:

if (value == "-" && isNumber(expression.charAt(j+1))
{
    // I assume you have access to some sort of isNumber function
    // and that you can check for an out of bounds access on your own 
    infix.add("-" + lexNumber(expression, j+1));
}
else if(eval.isOperator(value) == true)
{
     infix.add(value);
}
else
{ 
    // etc
}

This is a rough, untested push in the right direction that overlooks small problems. In particular, the problem of updating the loop index. I suggest some new class to encapsulate the expression source along with the current position. Something like:

while (tokenStream.hasNext())
{
    infix.add(tokenStream.next());
}
-1
votes
    public class Eval 
{
     boolean binaryOp = false;
    public boolean isOperator(String n)
    {
        switch(n)
        {
        case "(":
            return true;
        case ")":
            return true;
        case "*":
            ;
        case "/":
            ;
        case "+":
            binaryOp = true;
            return true;
        case "-":
                    If (binaryOp) break;
        case "#":
            return true;
        }
        binaryOp = false;
        return false;
     }
     public int Priority(String op)
    {
        switch(op)
        {
        case "(":
            return 3;
        case "*":
            return 2;
        case "/":
            return 2;
        case "+":
            return 1;
        case "-":
            return 1;

        }
        return 0;
    }

}