Example #1
0
        protected int Precedence(EqnElement Elmnt)
        {
            switch (Elmnt.elmType)
            {
            case Type.Bracket:
                return(5);

            case Type.Function:
                return(4);

            case Type.Comma:
                return(0);
            }
            switch (Elmnt.strName)
            {
            case "^":
                return(3);

            case "/":
            case "*":
            case "%":
                return(2);

            case "+":
            case "-":
                return(1);
            }
            return(-1);
        }
Example #2
0
        protected EqnElement Evaluate(EqnElement Elmnt1, EqnElement opr, EqnElement Elmnt2)
        {
            EqnElement result;

            result.strName  = Elmnt1.strName + opr.strName + Elmnt2.strName;
            result.elmType  = Type.Result;
            result.dblValue = 0;
            switch (opr.strName)
            {
            case "^":
                result.dblValue = System.Math.Pow(Elmnt1.dblValue, Elmnt2.dblValue);
                break;

            case "/":
            {
                if (Elmnt2.dblValue > 0)
                {
                    result.dblValue = Elmnt1.dblValue / Elmnt2.dblValue;
                }
                else
                {
                    result.strName = "Divide by Zero.";
                    result.elmType = Type.Error;
                }
                break;
            }

            case "*":
                result.dblValue = Elmnt1.dblValue * Elmnt2.dblValue;
                break;

            case "%":
                result.dblValue = Elmnt1.dblValue % Elmnt2.dblValue;
                break;

            case "+":
                result.dblValue = Elmnt1.dblValue + Elmnt2.dblValue;
                break;

            case "-":
                result.dblValue = Elmnt1.dblValue - Elmnt2.dblValue;
                break;

            default:
                result.elmType = Type.Error;
                result.strName = "Undefine operator: " + opr.strName + ".";
                break;
            }
            if (result.dblValue.ToString(System.Globalization.CultureInfo.InvariantCulture).Contains("E-"))
            {
                result.dblValue = 0;
            }
            return(result);
        }