Exemplo n.º 1
0
        public override bool LoadNodeFromString(ref string st, ref int pos)
        {
            bool res = true;

            if (pos >= st.Length)
            {
                res = false;
            }
            else
            if (st[pos] != '(')
            {
                res = false;
            }
            else
            {
                ++pos;
                while (pos < st.Length && st[pos] == ' ')
                {
                    ++pos;
                }
                left = OneNode.NewNode(ref st, ref pos);
                if (left == null)
                {
                    res = false;
                }
                else
                {
                    while (pos < st.Length && st[pos] == ' ')
                    {
                        ++pos;
                    }
                    int p = Array.IndexOf(Sings, st[pos]);
                    if (pos == st.Length || p == -1 || p == 4)
                    {
                        res = false;
                    }
                    else
                    {
                        sign = (Sign)p;
                        // MessageBox.Show("Sign in reading = " + st[pos] + " p = " + p+" si = "+sign);
                        ++pos;
                        right = OneNode.NewNode(ref st, ref pos);
                        if (right == null)
                        {
                            res = false;
                        }
                        else
                        {
                            while (pos < st.Length && st[pos] == ' ')
                            {
                                ++pos;
                            }
                            if (pos == st.Length || st[pos] != ')')
                            {
                                res = false;
                            }
                            else
                            {
                                ++pos;
                            }
                            //  MessageBox.Show("res = " + res);
                        }
                    }
                }
            }
            return(res);
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
 public SignNode(Sign c) : base()
 {
     sign = c;
 }