예제 #1
0
        private CommonF Factor()
        {
            Pow     pow   = new Pow();
            CommonF power = Power();

            if (!power.IsZero())
            {
                pow.RaiseTo(power);
            }
            else
            {
                while (IsCaret(look))
                {
                    Match('^');
                    Power();
                }
                return(new Constant(0));
            }
            while (IsCaret(look))
            {
                Match('^');
                power = Power();
                if (!power.IsZero())
                {
                    pow.RaiseTo(power);
                }
                else
                {
                    if (pow.baseAndPower.Count > 1)
                    {
                        pow.baseAndPower.RemoveAt(pow.baseAndPower.Count - 1);
                        while (IsCaret(look))
                        {
                            Match('^');
                            Power();
                        }
                        return(pow);
                    }
                    else
                    {
                        while (IsCaret(look))
                        {
                            Match('^');
                            Power();
                        }
                        return(new Constant(1));
                    }
                }
            }
            if (pow.baseAndPower.Count == 1)
            {
                return(pow.baseAndPower[0]);
            }
            else
            {
                return(pow);
            }
        }
예제 #2
0
        private CommonF Power()
        {
            CommonF result = null;

            if (look == '(')
            {
                Match('(');
                result = Expression();
                Match(')');
                return(result);
            }
            if (IsAlpha(look))
            {
                var name = GetName();
                if (look == '(')
                {
                    Match('(');
                    var arg = Expression();
                    result = new OneVar(builtInFunctions[name], arg, name + "(argument)");
                    Match(')');
                }
                else
                {
                    if (variables.ContainsKey(name))
                    {
                        result = new Variable(variables[name], name);
                    }
                    else
                    {
                        if (name == "PI" || name == "pi" || name == "Pi")
                        {
                            return(new PI());
                        }
                        if (name == "e" || name == "E")
                        {
                            return(new E());
                        }
                        variables[name] = variables.Count != 0 ? variables.Last().Value + 1 : 0;
                        result          = new Variable(variables[name], name);
                    }
                    ((Variable)result).name = name;
                }
            }
            else
            {
                double constant = GetNum();
                result = new Constant(constant);
            }

            return(result);
        }
예제 #3
0
        private CommonF Term()
        {
            Mul      mul      = new Mul();
            Constant constant = new Constant(1);
            CommonF  factor   = Factor();

            if (!factor.IsZero())
            {
                if (!(factor is Constant))
                {
                    mul *= factor;
                }
                else
                {
                    constant *= (Constant)factor;
                }
            }
            else
            {
                while (IsMulOperator(look))
                {
                    switch (look)
                    {
                    case '*':
                        Match('*'); break;

                    case '/':
                        Match('/'); break;
                    }
                    Factor();
                }
                return(new Constant(0));
            }
            while (IsMulOperator(look))
            {
                switch (look)
                {
                case '*':
                    Match('*');
                    factor = Factor();
                    if (!factor.IsZero())
                    {
                        if (!(factor is Constant))
                        {
                            mul *= factor;
                        }
                        else
                        {
                            constant *= (Constant)factor;
                        }
                    }
                    else
                    {
                        while (IsMulOperator(look))
                        {
                            switch (look)
                            {
                            case '*':
                                Match('*'); break;

                            case '/':
                                Match('/'); break;
                            }
                            Factor();
                        }
                        return(new Constant(0));
                    }
                    break;

                case '/':
                    Match('/');
                    factor = Factor();
                    if (!factor.IsZero())
                    {
                        if (!(factor is Constant))
                        {
                            mul /= factor;
                        }
                        else
                        {
                            constant /= (Constant)factor;
                        }
                    }
                    else
                    {
                        throw new ArithmeticException("null division");
                    }
                    break;

                default:
                    throw new Exception("unexpected parser error");
                }
            }

            if (constant.IsZero())
            {
                return(new Constant(0));
            }
            if (mul.operands.Count == 0)
            {
                return(constant);
            }
            if (mul.operands.Count == 1)
            {
                if (constant.Value == 1)
                {
                    return(mul.operands[0]);
                }
                else
                {
                    return(mul.operands[0] * constant);
                }
            }

            if (constant.Value == 1)
            {
                return(mul);
            }
            else
            {
                return(mul * constant);
            }
        }
예제 #4
0
        //get expression as graph
        private CommonF Expression()
        {
            Sum sum = new Sum();

            Constant constant = new Constant(0);

            CommonF term = null;

            if (!IsAddOperator(look))
            {
                term = Term();
                if (!term.IsZero())
                {
                    if (!(term is Constant))
                    {
                        sum += term;
                    }
                    else
                    {
                        constant += (Constant)term;
                    }
                }
            }
            while (IsAddOperator(look))
            {
                switch (look)
                {
                case '+':
                    Match('+');
                    term = Term();
                    if (!term.IsZero())
                    {
                        if (!(term is Constant))
                        {
                            sum += term;
                        }
                        else
                        {
                            constant += (Constant)term;
                        }
                    }
                    break;

                case '-':
                    Match('-');
                    term = Term();
                    if (!term.IsZero())
                    {
                        if (!(term is Constant))
                        {
                            sum -= term;
                        }
                        else
                        {
                            constant -= (Constant)term;
                        }
                    }
                    break;

                default:
                    throw new Exception();
                }
            }

            if (sum.operands.Count == 0)
            {
                return(constant);
            }
            if (constant.Value != 0)
            {
                sum += constant;
            }
            return(sum);
        }