后缀的中缀和一元/二元运算符

发布于 2024-08-25 05:53:46 字数 387 浏览 9 评论 0原文

我有一段代码将中缀表达式转换为内存中的表达式树。这很好用。只是有一个小麻烦。我只是连接计算出如何正确地涉及一元运算符(正确的关联运算符)。

使用以下中缀表达式:

+1 + +2 - -3 - -4

我期望 RPN 为:

1+2++3-4--

然而,我找到的任何在线中缀后置转换器都无法按照我期望的方式处理此示例。有谁对处理右结合运算符有明确的解释,特别是可能被误认为一元运算符的二元运算符?

编辑/澄清: 我想知道从中缀到后缀的转换过程中如何处理一元运算符。即:将相同的“-”字符识别为一元运算符而不是二元运算符,因此具有不同的优先级。我会考虑使用可能具有两个状态的状态机,但是......?

I have a piece of code that converts an infix expression to an expression tree in memory. This works just fine. There's just one small trouble. I just connect work out how to involve the unary operators correctly (the right associative ones).

With the following infix expression :

+1 + +2 - -3 - -4

I would expect an RPN of:

1+2++3-4--

Yet, none of the online infix-post converters I can find handle this example in the way I would expect. Does anyone have a clear explanation of handling right associative operators, specifically the binary ones that can be mistaken for the unary ones?

Edit/Clarification:
I would like to know how to deal with the unary operators during the translation from infix to postfix. Ie: recognizing the same '-' character for example as being unary instead of binary operator and thus a different precedence. I would think of using a state machine perhaps with two states but ...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喵星人汪星人 2024-09-01 05:53:46

那么,您需要在解析阶段确定给定的运算符是二元还是一元。完成此操作后,在创建 RPN 时,您可以将运算符标记为采用 2 或 1 个参数。

您可以尝试使用 Shunting Yard 算法进行解析(并同时创建 RPN ),它也被设计为与一元运算符一起使用。

无论如何,如果您关心的只是一元 + 或 -,那么当您看到“意外”出现的 + 或 - 时,您可以插入带括号的 0。

例如

+1 + +2 - -3 - -4

您应该能够通过它并转换为

(0+1) + (0+2) - (0 -3) - (0-4)

现在您无需担心一元 + 或 - 并且可能会忘记用它们所采用的参数数量来标记运算符。

Well, you need to determine if a given operator is binary/unary during the parsing stage. Once you do that, when you create the RPN, you can tag the operators as taking 2 or 1 arguments.

You could try using the Shunting Yard algorithm to do the parsing (and creation of RPN at the same time), which was designed to work with unary operators too.

In any case, if all you care about is unary + or -, you could just insert a 0 with brackets when you see a + or - that appears 'unexpectedly'.

For instance

+1 + +2 - -3 - -4

You should be able to make a pass through it and convert to

(0+1) + (0+2) - (0-3) - (0-4)

Now you don't need to worry about the unary + or - and can probably forget about tagging the operators with the number of arguments they take.

未蓝澄海的烟 2024-09-01 05:53:46

也许这段混乱的 C# 代码会对您有所帮助。一元运算符在算术运算中具有最高优先级,因此它们的优先级会更高。
为了识别一元运算符,我采用了一个布尔变量一元,它将在每个运算符标记后设置为 true,在操作数后设置为 false。
您必须对一元加号和减号运算符使用不同的表示法,以便您可以在 PFE 中区分一元和二元运算符。这里
'#' 和 '~' 用于描述后缀表达式(PFE)中的一元加和一元减。

您可以使用此方法处理 1+-1,1+(-1),1---1,1+--1 等所有情况。

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DSA
{
    class Calculator
    {
        string PFE;
        public Calculator()
        {
            Console.WriteLine("Intializing Calculator");
        }

        public double Calculate(string expression)
        {
            ConvertToPost(expression);
            return CalculatePOST();
        }

        public void ConvertToPost(string expression)
        {
            expression = "(" + expression + ")";

            var temp = new DSA.Stack<char>();
            string ch = string.Empty;
            bool unary = true;
            char a;
            foreach (var b in expression)
            {
                a = b;
                if (!a.IsOperator()) {
                    ch += a.ToString();
                    unary = false;
                }
                else
                {
                    if (ch!=string.Empty) 
                    PFE += ch+",";
                    ch = string.Empty;
                    if(a!='(' && a!=')')
                    {
                        if (unary == true)
                        {
                            if (a == '-') a = '~'; else if (a == '+') a = '#'; else throw new InvalidOperationException("invalid input string");
                        }
                        try
                        {
                            if (a.Precedence() <= temp.Peek().Precedence())
                            {
                                PFE += temp.Pop().ToString() + ",";
                            }
                          }
                        catch(InvalidOperationException e)
                        {

                        }

                            temp.Push(a);
                            unary = true;
                    }
                    if (a == '(') { temp.Push(a);}
                    if(a==')')
                    {
                       for(char t=temp.Pop();t!='(';t=temp.Pop())
                        {
                            PFE += t.ToString() + ","; 
                        }
                    }
                }

            }

        }

        public double CalculatePOST()
        {
            var eval = new Stack<double>();
            string ch = string.Empty;
            bool skip = false;
            foreach(char c in PFE)
            {
                if(!c.IsOperator())
                {
                    if (c == ',')
                    {
                        if (skip == true)

                        {
                            skip = false;
                            continue;
                        }
                        eval.Push(Double.Parse(ch));
                        ch = string.Empty;
                    }
                    else ch += c;
                }
                else
                {
                    if (c == '~')
                    {
                        var op1 = eval.Pop();
                        eval.Push(-op1);
                    }
                    else if (c == '#') ;
                    else
                    {
                        var op2 = eval.Pop();
                        var op1 = eval.Pop();
                        eval.Push(Calc(op1, op2, c));
                    }
                    skip = true;
                }
              }
            return eval.Pop();
        }

        private double Calc(double op1, double op2, char c)
        {   
           switch(c)
            {

                case '+':
                    return op1 + op2;
                case '-':
                    return op1 - op2;
                case '*':
                    return op1 * op2;
                case '%':
                    return op1 % op2;
                case '/':
                    return op1 / op2;
                case '^':
                    return float.Parse(Math.Pow(op1,op2).ToString());
                default:
                    throw new InvalidOperationException();
            }
        }
    }


    public static class extension
    {
        public static bool IsOperator(this char a)
        {
            char[] op = {'+','-','/','*','(',')','^','!','%','~','#'};
             return op.Contains(a);
        }

        public static int Precedence(this char a)
        {
            if (a == '~' || a== '#')
                return 1;
            if (a == '^')
                return 0;
            if (a == '*' || a == '/' || a=='%')
                return -1;
            if (a == '+' || a == '-')
                return -2;
            return -3;       
        }       
    }
}

may be this chaotic C# code will help you. a unary operator has the maximum priority in arithmetic operations, so the precedence will be higher for them.
for identifying unary operator I have taken a boolean variable unary which will be set true after each operator token and false after an operand.
You have to use a different notation for unary plus and minus operator so that you can distinguish between unary and binary operator in PFE. here
'#' and '~' are used to depict the unary plus and unary minus in postfix expression(PFE).

you can handle all cases like 1+-1,1+(-1),1---1,1+--1 by using this approch.

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DSA
{
    class Calculator
    {
        string PFE;
        public Calculator()
        {
            Console.WriteLine("Intializing Calculator");
        }

        public double Calculate(string expression)
        {
            ConvertToPost(expression);
            return CalculatePOST();
        }

        public void ConvertToPost(string expression)
        {
            expression = "(" + expression + ")";

            var temp = new DSA.Stack<char>();
            string ch = string.Empty;
            bool unary = true;
            char a;
            foreach (var b in expression)
            {
                a = b;
                if (!a.IsOperator()) {
                    ch += a.ToString();
                    unary = false;
                }
                else
                {
                    if (ch!=string.Empty) 
                    PFE += ch+",";
                    ch = string.Empty;
                    if(a!='(' && a!=')')
                    {
                        if (unary == true)
                        {
                            if (a == '-') a = '~'; else if (a == '+') a = '#'; else throw new InvalidOperationException("invalid input string");
                        }
                        try
                        {
                            if (a.Precedence() <= temp.Peek().Precedence())
                            {
                                PFE += temp.Pop().ToString() + ",";
                            }
                          }
                        catch(InvalidOperationException e)
                        {

                        }

                            temp.Push(a);
                            unary = true;
                    }
                    if (a == '(') { temp.Push(a);}
                    if(a==')')
                    {
                       for(char t=temp.Pop();t!='(';t=temp.Pop())
                        {
                            PFE += t.ToString() + ","; 
                        }
                    }
                }

            }

        }

        public double CalculatePOST()
        {
            var eval = new Stack<double>();
            string ch = string.Empty;
            bool skip = false;
            foreach(char c in PFE)
            {
                if(!c.IsOperator())
                {
                    if (c == ',')
                    {
                        if (skip == true)

                        {
                            skip = false;
                            continue;
                        }
                        eval.Push(Double.Parse(ch));
                        ch = string.Empty;
                    }
                    else ch += c;
                }
                else
                {
                    if (c == '~')
                    {
                        var op1 = eval.Pop();
                        eval.Push(-op1);
                    }
                    else if (c == '#') ;
                    else
                    {
                        var op2 = eval.Pop();
                        var op1 = eval.Pop();
                        eval.Push(Calc(op1, op2, c));
                    }
                    skip = true;
                }
              }
            return eval.Pop();
        }

        private double Calc(double op1, double op2, char c)
        {   
           switch(c)
            {

                case '+':
                    return op1 + op2;
                case '-':
                    return op1 - op2;
                case '*':
                    return op1 * op2;
                case '%':
                    return op1 % op2;
                case '/':
                    return op1 / op2;
                case '^':
                    return float.Parse(Math.Pow(op1,op2).ToString());
                default:
                    throw new InvalidOperationException();
            }
        }
    }


    public static class extension
    {
        public static bool IsOperator(this char a)
        {
            char[] op = {'+','-','/','*','(',')','^','!','%','~','#'};
             return op.Contains(a);
        }

        public static int Precedence(this char a)
        {
            if (a == '~' || a== '#')
                return 1;
            if (a == '^')
                return 0;
            if (a == '*' || a == '/' || a=='%')
                return -1;
            if (a == '+' || a == '-')
                return -2;
            return -3;       
        }       
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文