Exemplo n.º 1
0
        //••••••••••••••••••••••••••••••••••••••••••••••••••••••••
        /// <summary>
        /// Solve unray operator, where operand taken from right of operator
        /// position</summary>
        /// <param name="_blocks">in blocks</param>
        /// <param name="_iHipos">at operator position</param>
        /// <returns>calculation result</returns>
        private CNumber SolveUnary(CBlock _blocks, int _iHipos)
        {
            ASSERT(((_iHipos + 1) < _blocks.Count) && (_blocks[_iHipos + 1].IsNumber)
                   , "Missing argument for >" + _blocks[_iHipos].Operator + '<'
                   , "SolveUnary");

            string  strOperator = _blocks[_iHipos].Operator;
            CNumber number      = _blocks[_iHipos + 1].Number;
            CNumber res;

            STUB(strOperator != null && strOperator != "", "Bad _sOper in SolveUnary");
            STUB(number != null, "Bad _Num in SolveUnary");

            ///-- LOGIC
            if (the(AO.NOT) == strOperator)
            {
                res = CMath.Not(number);
            }
            ///-- ARITHMETIC
            else if (the(AO.MIN) == strOperator)
            {
                res = CMath.Minus(number);
            }
            else if (the(AO.PLS) == strOperator)
            {
                res = CMath.Plus(number);
            }
            else if (the(AO.ROOT) == strOperator)
            { // square root
                res = CMath.Root(number);
            }
            else if (the(AO.ABS) == strOperator)
            {
                res = CMath.Abs(number);
            }
            ///-- THRIGONOMETRY
            else if (the(AO.SIN) == strOperator)
            {
                res = CMath.Sin(number);
            }
            else if (the(AO.COS) == strOperator)
            {
                res = CMath.Cos(number);
            }
            else if (the(AO.EXP) == strOperator)
            {
                res = CMath.Exp(number);
            }
            else if (the(AO.TAN) == strOperator)
            {
                res = CMath.Tan(number);
            }
            ///-- FUNCTION
            else if (the(AO.FACT) == strOperator)
            {
                res = CMath.Factorial(number);
            }
            else if (the(AO.LG) == strOperator)
            {
                res = CMath.Lg(number);
            }
            else if (the(AO.LN) == strOperator)
            {
                res = CMath.Ln(number);
            }
            else if (the(AO.LOG) == strOperator)
            {
                res = CMath.Log(number);
            }
            else
            {
                STUB(false, "[ " + strOperator.ToUpper()
                     + " ] not implemented unary operation!");
                return(null);
            }

            ASSERT(!(double.IsInfinity(res.dValue))
                   , "Result is ±Infinity", "SolveUnary");
            ASSERT(!(double.IsNaN(res.dValue))
                   , "Result is Not a number", "SolveUnary");

            return(res);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Solve binary operator, where operands taken from right and left
        /// of operator position</summary>
        /// <param name="_blocks">in blocks</param>
        /// <param name="_iHipos">at operator position</param>
        /// <returns>calculation result</returns>
        private CNumber SolveBinary(CBlock _blocks, int _iHipos)
        {
            ASSERT((_blocks.Count >= 3) && (_iHipos > 0) &&
                   ((_iHipos + 1) < _blocks.Count)
                   , "Unrecovered binary operator >" + _blocks[_iHipos].Operator + '<'
                   , "SolveBinary1");
            ASSERT(_blocks[_iHipos - 1].IsNumber && _blocks[_iHipos + 1].IsNumber
                   , "Missing argument for binary operator >" + _blocks[_iHipos].Operator + '<'
                   , "SolveBinary2");

            CNumber nLeft       = _blocks[_iHipos - 1].Number;
            string  strOperator = _blocks[_iHipos].Operator;
            CNumber nRight      = _blocks[_iHipos + 1].Number;
            CNumber res;

            STUB((nLeft != null) && (nRight != null), "Bad _nLeft in SolveBinary.");
            STUB((strOperator != null) && (strOperator != ""), "Bad _sOper in SolveBinary.");

            ///-- ARITHMETIC
            if (the(AO.POW) == strOperator)
            {
                res = CMath.Pow(nLeft, nRight);
            }
            else if (the(AO.MUL) == strOperator)
            {
                res = CMath.Mul(nLeft, nRight);
            }
            else if (the(AO.DIV) == strOperator)
            {
                res = CMath.Div(nLeft, nRight);
            }
            else if (the(AO.MOD) == strOperator)
            {
                res = CMath.Mod(nLeft, nRight);
            }
            else if (the(AO.PLS) == strOperator)
            {
                res = CMath.Plus(nLeft, nRight);
            }
            else if (the(AO.MIN) == strOperator)
            {
                res = CMath.Minus(nLeft, nRight);
            }
            else if (the(AO.ROOT) == strOperator)
            {
                res = CMath.Root(nLeft, nRight);
            }
            ///-- FUNCTION
            else if (the(AO.LOG) == strOperator)
            {
                res = CMath.Log(nLeft, nRight);
            }
            ///-- LOGIC
            else if (the(AO.AND) == strOperator)
            {
                res = CMath.Log(nLeft, nRight);
            }
            else if (the(AO.OR) == strOperator)
            {
                res = CMath.Or(nLeft, nRight);
            }
            else if (the(AO.XOR) == strOperator)
            {
                res = CMath.Xor(nLeft, nRight);
            }
            ///-- BITWISE
            else if (the(AO.SH_LEFT) == strOperator)
            {
                res = CMath.ShiftLeft(nLeft, nRight);
            }
            else if (the(AO.SH_RIGHT) == strOperator)
            {
                res = CMath.ShiftRight(nLeft, nRight);
            }
            else if (the(AO.GCD) == strOperator)
            {
                res = CMath.GCD(nLeft, nRight);
            }
            //-- not implemented
            else
            {
                STUB(false, "[" + strOperator
                     + "] not implemented binary operation!");
                return(null);
            }

            ASSERT(!(double.IsInfinity(res.dValue))
                   , "Result is ±Infinity", "SolveBinary");
            ASSERT(!(double.IsNaN(res.dValue))
                   , "Result is Not a number", "SolveBinary");

            return(res);
        }