如何解析带有负数和减号的算术字符串表达式? [英] How to parse an arithmetic string expression with negative numbers and minus signs?

查看:44
本文介绍了如何解析带有负数和减号的算术字符串表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一个带有负数和减号的字符串,并将每个标记添加到名为 infix 的队列和一个用于判断该标记是否为运算符的函数.然而,负数被视为减号,并被添加到与负数分开的队列中.这是我将字符串分解为数字并将它们添加到队列中的代码.

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);

            }
        }

和 eval 类

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;
    }

}

    }

推荐答案

大多数语言处理这个问题的方式是稍后再做.正如@Sweeper 所说, - 将是一个运算符,后来的代码将选择这是一个二元运算符还是一元运算符.假设您计划在某个时候评估这些表达式,那么这样做最终实际上不会有太多额外的工作.

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.

不过,为了按照您的方式处理它,我会首先从您的循环中提取一些函数,可能是 eval.lexNumber(String expression, int index)

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());
}

这篇关于如何解析带有负数和减号的算术字符串表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆