Beispiel #1
0
 public BuildInfixNode(string op, BuildInfixNode operand)
 {
     Value    = null;
     Children = new BuildInfixNode[] { new BuildInfixNode(op), operand };
 }
Beispiel #2
0
 public BuildInfixNode(BuildInfixNode open, BuildInfixNode left, string op, BuildInfixNode right, BuildInfixNode close)
 {
     Value    = null;
     Children = new BuildInfixNode[] { open, left, new BuildInfixNode(" " + op + " "), right, close };
 }
Beispiel #3
0
        private string BuildInfix(bool tokenize)
        {
            int i     = 0;
            var s     = new Stack <BuildInfixNode>();
            var open  = new BuildInfixNode("(");
            var close = new BuildInfixNode(")");

            while (i < TokenizedPostfix.Length)
            {
                char c = TokenizedPostfix[i++];
                if (c == '{')
                {
                    if (TokenizedPostfix[i] == 'A')
                    {
                        int j = i + 3;      // parsing an argument, j points at the 1st digit of the argument's index
                        int k = j + 1;
                        while (TokenizedPostfix[k] != '}')
                        {
                            ++k;
                        }

                        if (tokenize)
                        {
                            s.Push(new BuildInfixNode(TokenizedPostfix.Substring(i - 1, k - i + 2)));
                        }
                        else
                        {
                            s.Push(new BuildInfixNode(_argBook[int.Parse(TokenizedPostfix.Substring(j, k - j))]));
                        }

                        i = k + 1;
                    }
                    else
                    {
                        int j = i + 1;      // parsing a const number, j points at the number's 1st digit
                        while (TokenizedPostfix[j] != '}')
                        {
                            ++j;
                        }

                        if (tokenize)
                        {
                            s.Push(new BuildInfixNode(TokenizedPostfix.Substring(i - 1, j - i + 2)));
                        }
                        else
                        {
                            s.Push(new BuildInfixNode(TokenizedPostfix.Substring(i, j - i)));
                        }

                        i = j + 1;
                    }
                }
                else if (c == 'N')
                {
                    s.Push(new BuildInfixNode("-", s.Pop()));
                }
                else if (c == 'P')
                {
                    s.Push(new BuildInfixNode("+", s.Pop()));
                }
                else
                {
                    var x1 = s.Pop();       // merges a binary operator with two of its operands
                    var x2 = s.Pop();
                    s.Push(new BuildInfixNode(open, x2, c.ToString(), x1, close));
                }
            }
            return(s.Pop().ToString());
        }