Пример #1
0
        public static OneNode NewNode(ref string st, ref int pos)
        {
            OneNode res = null;

            while (pos < st.Length && st[pos] == ' ')
            {
                ++pos;
            }
            //MessageBox.Show("In ONENODE, '" + st[pos] + "'");
            if (pos < st.Length)
            {
                if (Char.IsLower(st[pos]))
                {
                    res = new VarNode("");
                }
                else
                if (Char.IsDigit(st[pos]))
                {
                    res = new IntNode(0);
                }
                else
                if (st[pos] == '(')
                {
                    res = new SignNode(Sign.empty);
                }
            }
            if (res != null && !res.LoadNodeFromString(ref st, ref pos))
            {
                res = null;
            }
            return(res);
        }
Пример #2
0
        bool  TryClear(ref OneNode R, ref int count)
        {
            bool res = true;

            if (R != null)
            {
                OneNode l = R.left;
                OneNode r = R.right;
                if (Root is SignNode)
                {
                    if (l == null || r == null)
                    {
                        return(false);
                    }
                    Sign s = ((SignNode)R).Sign;
                    // MessageBox.Show("Here, s = " + s.ToString());
                    if (l is IntNode && r is IntNode)
                    {
                        int left  = ((IntNode)l).Finfo;
                        int right = ((IntNode)r).Finfo;
                        switch (s)
                        {
                        case Sign.mult:
                            R = new IntNode(left * right);
                            ++count;
                            break;

                        case Sign.add:
                            R = new IntNode(left + right);
                            ++count;
                            break;

                        case Sign.divide:
                            if (right == 0)
                            {
                                return(false);
                            }
                            else
                            {
                                R = new IntNode(left / right);
                            }
                            ++count;
                            break;

                        case Sign.subst:
                            R = new IntNode(left - right);
                            ++count;
                            break;

                        default: return(false);
                        }
                    }
                    else
                    {
                        switch (s)
                        {
                        case Sign.add:
                            if (l is IntNode && ((IntNode)l).Finfo == 0)
                            {
                                R = R.right;
                                ++count;
                                TryClear(ref R, ref count);
                            }
                            else
                            if (r is IntNode && ((IntNode)r).Finfo == 0)
                            {
                                R = R.left;
                                ++count;
                                TryClear(ref R, ref count);
                            }
                            else
                            {
                                TryClear(ref R.left, ref count);
                                TryClear(ref R.right, ref count);
                            }
                            break;

                        case Sign.divide:
                            if (l is IntNode && ((IntNode)l).Finfo == 0)
                            {
                                R = new IntNode(0);
                                ++count;
                            }
                            else
                            if (r is IntNode)
                            {
                                if (((IntNode)r).Finfo == 0)
                                {
                                    return(false);
                                }
                                else
                                if (((IntNode)r).Finfo == 1)
                                {
                                    R = R.left;
                                    ++count;
                                    TryClear(ref R, ref count);
                                }
                            }
                            else
                            {
                                TryClear(ref R.left, ref count);
                                TryClear(ref R.right, ref count);
                            }
                            break;

                        case Sign.mult:
                            if (l is IntNode)
                            {
                                if (((IntNode)l).Finfo == 0)
                                {
                                    ++count;
                                    R = new IntNode(0);
                                }
                                else
                                if (((IntNode)l).Finfo == 1)
                                {
                                    R = R.right;
                                    ++count;
                                    TryClear(ref R, ref count);
                                }
                            }
                            else
                            if (r is IntNode)
                            {
                                if (((IntNode)r).Finfo == 0)
                                {
                                    R = new IntNode(0);
                                    ++count;
                                }
                                else
                                if (((IntNode)r).Finfo == 1)
                                {
                                    R = R.left;
                                    ++count;
                                    TryClear(ref R, ref count);
                                }
                            }
                            else
                            {
                                TryClear(ref R.left, ref count);
                                TryClear(ref R.right, ref count);
                            }
                            break;

                        case Sign.subst:
                            if (r is IntNode && ((IntNode)r).Finfo == 0)
                            {
                                R = R.left;
                                ++count;
                                TryClear(ref R, ref count);
                            }
                            else
                            {
                                TryClear(ref R.left, ref count);
                                TryClear(ref R.right, ref count);
                            }
                            break;

                        default: return(false);
                        }
                    }
                }
            }
            return(res);
        }
Пример #3
0
        public override OneNode Copy()
        {
            IntNode result = new IntNode(this.Finfo);

            return(result);
        }