//very Important function-=-Self Explainatory
        private void PopConnectPush()
        {
            ExpTreeNode Temp = OpStack.Pop(); //Get Top operator and associate the operands

            //Find here if the Operator is "="  - then dont make the Symbol InVALID--Give some value


            Temp._right = NodeStack.Pop(); //Get Left and right from NodeStck
            if (Temp._value.ToString() == "=")
            {
                ExpTreeNode TmpLeft = NodeStack.Pop();

                //Now this needs to be Symbol with no Sub tree
                if (TmpLeft._ndType != ExpTreeNode.NodeType.Symbol)
                {
                    ErrorException Er = new ErrorException("Operator = at the wrong Place Left Operand must be lvalue ");
                    throw Er;
                }
                if (TmpLeft._left == null || TmpLeft._right == null)
                {
                    TmpLeft._value = -1;  //SYMBOL TO BE CALCULATED
                    Temp._left     = TmpLeft;
                    NodeStack.Push(Temp); // puch the sub tree on the stack
                    return;
                }
                else
                {
                    ErrorException Er = new ErrorException("Operator = at the Wrong Place Left Operand must be lvalue");
                    throw Er;
                }
            }
            Temp._left = NodeStack.Pop();
            NodeStack.Push(Temp); // puch the sub tree on the stack
        }
Exemple #2
0
 public bool SetExpression(string _exp)
 {
     //Clear the Previous Exp
     _exp_parser.Clear();
     //Now Parse the Exp
     _exp_tree = _exp_parser.ParseExpression(_exp);
     return(true);
 }
Exemple #3
0
        private double _evaluate_exp(ExpTreeNode n)
        {
            if (n == null)
            {
                ErrorException Er = new ErrorException("No hay formula para evaluar. ");
                throw Er;
            }
            double x, y;

            if (n._ndType == ExpTreeNode.NodeType.Operator)
            {
                x = _evaluate_exp(n._left);
                y = _evaluate_exp(n._right);
                return((_OpInst == null) ? ApplyOp(n._value.ToString(), x, y) : _OpInst.ApplyOp(n._value.ToString(), x, y));
            }
            return(System.Convert.ToInt64(n._value));
        }
        public static string Evaluar(string strFormula)
        {
            string strError = "";

            ExpTreeNode[] sk   = new ExpTreeNode[10];
            int           i    = 0;
            string        NExp = strFormula;

            string[] lines       = NExp.Split(';');
            string   _expression = "";

            foreach (string _line in lines)
            {
                if (_line.IndexOf("=") > 0)
                {
                    string[] Sybl = _line.Split('=');
                    if (Sybl[0].Trim().Length < 1)
                    {
                        strError = "FORMULA_ERROR_SIMBOLO_NO_VALIDO: " + Sybl[0];
                        return(strError);
                    }
                    if (Sybl[0].Trim().Length < 1)
                    {
                        strError = "FORMULA_ERROR_VALOR_NO_PERMITIDO: " + Sybl[0];
                        return(strError);
                    }
                    ExpTreeNode tmp = new ExpTreeNode();
                    tmp._symbol = Sybl[0].Trim();
                    tmp._value  = System.Convert.ToInt64(Sybl[1].Trim());
                    sk[i++]     = tmp;
                }
                else
                {
                    if (_line.Length > 0)
                    {
                        _expression = _line;
                    }
                }
            }

            try
            {
                IParser      par = new ExpParser();
                ExpEvaluator eu  = new ExpEvaluator(par);
                eu.SetExpression(_expression);
                eu["y"] = 1;
                for (int j = 0; j < i; j++)
                {
                    eu[sk[j]._symbol] = System.Convert.ToInt64(sk[j]._value);
                }
                double res = eu.Evaluate();
                //strError = res.ToString();
                strError = "OK"; //+ res.ToString();
                return(strError);
            }
            catch (ErrorException ka)
            {
                strError = ka._message;
                return(strError);
            }
        }
        //Not Implemented

        public string ReMakeExpression(ExpTreeNode Tree)
        {
            //Doing nothing right now
            return("");
        }
 public void Push(ExpTreeNode f)
 {
     data[top++] = f;
 }
 public TreeStack(int size)
 {
     data = new ExpTreeNode[size];
     top  = 0;
 }
        //Parse Expression funtion main funtion
        //Creating the Exp tree from the Infix Expression
        public ExpTreeNode ParseExpression(string exp)
        {
            //Check for the Expresiioon validity
            if (!Isvalid_Exp(exp))
            {
                //Throw an Error Saying Commas are not proper
                ErrorException Er = new ErrorException("Los Parentesis () no estan bien ubicados");
                throw Er;
                //Is c# need this return after that -- i dont think so but anyway
            }
            //Fill ur _exp Variable will need that
            _exp = exp;

            NextToken();
            do
            {
                //The errors that might come would be from stack
                //If that went wrong -- gone case -- What use rwould do
                //No USer specific Errors from this funtion
                switch (current)
                {
                case Token.Literal:
                    //CreateLeafNode and push it on the stack
                    ExpTreeNode LNode = new ExpTreeNode();
                    LNode._value  = literal;
                    LNode._ndType = ExpTreeNode.NodeType.Literal;
                    NodeStack.Push(LNode);
                    break;

                case Token.Operator:
                    //pop the St
                    ExpTreeNode ONode = new ExpTreeNode();
                    ONode._value  = Curr_op;
                    ONode._ndType = ExpTreeNode.NodeType.Operator;
                    if (Curr_op == "(")
                    {
                        OpStack.Push(ONode);
                        break;
                    }
                    if (Curr_op == ")")
                    {
                        while ("(" != (string)OpStack.Top._value)
                        {
                            PopConnectPush();
                        }
                        OpStack.Pop();     //Throw away {{
                        break;
                    }

                    if (OpStack.IsEmpty())
                    {
                        OpStack.Push(ONode);
                    }
                    else if ((string)OpStack.Top._value == "(")
                    {
                        OpStack.Push(ONode);
                    }
                    else if (Priority(OpStack.Top._value.ToString()) < Priority(ONode._value.ToString()))
                    {
                        OpStack.Push(ONode);
                    }
                    else
                    {
                        while (true)
                        {
                            PopConnectPush();
                            if (OpStack.IsEmpty() == true || (string)OpStack.Top._value == "(" || Priority(OpStack.Top._value.ToString()) < Priority(ONode._value.ToString()))
                            {
                                break;
                            }
                        }
                        OpStack.Push(ONode);
                    }
                    break;

                case Token.Symbol:
                {
                    ExpTreeNode SNode = new ExpTreeNode();
                    SNode._symbol = symbol;
                    SNode._ndType = ExpTreeNode.NodeType.Symbol;
                    NodeStack.Push(SNode);
                }
                break;

                default:
                    break;
                }
            } while (NextToken() != Token.Eof);

            while (OpStack.IsEmpty() == false)
            {
                PopConnectPush();
            }
            //Get the Top of the Node Stack thats the root
            return(NodeStack.Top);
        }