コード例 #1
0
 public static NumberResult AsNumber(this IMathMeta meta)
 {
     if (meta.IsNumber())
     {
         return(meta as NumberResult);
     }
     throw new ArgumentException("meta is not a number!");
 }
コード例 #2
0
 public static double GetNumber(this IMathMeta meta)
 {
     if (meta.IsNumber())
     {
         return((NumberResult)meta);
     }
     throw new ArgumentException("unexpected \"number-->object\" cast." +
                                 " Numbers should parsed as " + typeof(NumberResult).FullName + " type!");
 }
コード例 #3
0
        public StringBuilder SyntaxAnalysisInfixNotation(IMathMeta token, StringBuilder outputString, Stack <MathMetaBase> block)
        {
            if (token.IsNumber())
            {
                outputString.Append(token.ToString());
            }
            else if (token.IsFunction()
                     ||
                     token.GetType().IsAssignableFrom(typeof(LeftParenthesis))
                     )
            {
                block.Push(token as MathMetaBase);
            }
            else if (token.GetType().IsAssignableFrom(typeof(RightParenthesis)))
            {
                string elem;
                while ((elem = block.Pop().ToString()) != new LeftParenthesis().ToString())
                {
                    outputString.Append(elem);
                }

                if (block.Count > 0 &&
                    block.Peek().IsFunction())
                {
                    outputString.Append(block.Pop());
                }
            }
            else
            {
                while (block.Count > 0
                       &&
                       (token as MathMetaBase) < block.Peek())
                {
                    outputString.Append(block.Pop().ToString());
                }
                block.Push(token as MathMetaBase);
            }
            return(outputString);
        }
コード例 #4
0
        private Stack <double> SyntaxAnalysisRPN(Stack <double> stack, IMathMeta token)
        {
            // if it's operand then just push it to stack
            if (token.IsNumber())
            {
                stack.Push(token.GetNumber());
            }
            else
            {
                double rst = 0;

                Stack <double> arguments = new Stack <double>();
                for (int i = 0; i < token.NumberOfArguments; i++)
                {
                    arguments.Push(stack.Pop());
                }

                token.Operate(ref rst, arguments.ToArray());

                stack.Push(rst);
            }

            return(stack);
        }