Ejemplo n.º 1
0
        private static RawExpression Exp2(char **input)
        {
            RawExpression e = Exp1(input);

            while (true)
            {
                if (Char(input, '*'))
                {
                    e = new MulExpression
                    {
                        Left  = e,
                        Right = Exp1(input),
                    };
                }
                else if (Char(input, '/'))
                {
                    e = new DivExpression
                    {
                        Left  = e,
                        Right = Exp1(input),
                    };
                }
                else
                {
                    break;
                }
            }
            return(e);
        }
Ejemplo n.º 2
0
 public RawExpression Simplify()
 {
     try
     {
         return(new NumberExpression
         {
             Number = Execute(new Dictionary <string, double>()),
         });
     }
     catch (KeyNotFoundException)
     {
         RawExpression s = SimplifyInternal();
         try
         {
             return(new NumberExpression
             {
                 Number = s.Execute(new Dictionary <string, double>()),
             });
         }
         catch (KeyNotFoundException)
         {
             return(s);
         }
     }
 }
Ejemplo n.º 3
0
        private static RawExpression Exp3(char **input)
        {
            RawExpression e = Exp2(input);

            while (true)
            {
                if (Char(input, '+'))
                {
                    e = new AddExpression
                    {
                        Left  = e,
                        Right = Exp2(input),
                    };
                }
                else if (Char(input, '-'))
                {
                    e = new SubExpression
                    {
                        Left  = e,
                        Right = Exp2(input),
                    };
                }
                else
                {
                    break;
                }
            }
            return(e);
        }
Ejemplo n.º 4
0
        public static double Solve(this RawExpression e, string name, double start, int maxCount = 1000)
        {
            RawExpression f  = e.Simplify();
            RawExpression df = f.Different(name).Simplify();

            return(f.Solve(df, name, start, maxCount));
        }
Ejemplo n.º 5
0
        private static RawExpression Exp0(char **input)
        {
            if (Char(input, '('))
            {
                RawExpression e = Exp3(input);
                if (!Char(input, ')'))
                {
                    throw new ArgumentException("Error encountered, at " + new string(*input));
                }
                return(e);
            }
            else if (Char(input, '-'))
            {
                return(new NegExpression
                {
                    Op = Exp0(input),
                });
            }
            else
            {
                double number = Number(input);
                if (!double.IsNaN(number))
                {
                    return(new NumberExpression
                    {
                        Number = number,
                    });
                }

                string name = Name(input);
                if (name == null)
                {
                    throw new ArgumentException("Error encountered, at " + new string(*input));
                }

                if (!Char(input, '('))
                {
                    return(new VariableExpression
                    {
                        Name = name,
                    });
                }

                FunctionExpression f = FunctionExpression.FromName(name);
                f.Op = Exp3(input);
                if (!Char(input, ')'))
                {
                    throw new ArgumentException("Error encountered, at " + new string(*input));
                }
                return(f);
            }
        }
Ejemplo n.º 6
0
        private static RawExpression UnsafeParse(char *input)
        {
            RawExpression result = Exp3(&input);

            if ((int)*input == 0)
            {
                return(result);
            }
            else
            {
                throw new ArgumentException("Expression contains unparsed tail, at " + new string(input));
            }
        }
Ejemplo n.º 7
0
        private static RawExpression Exp1(char **input)
        {
            RawExpression e = Exp0(input);

            while (true)
            {
                if (Char(input, '^'))
                {
                    e = new PowerExpression
                    {
                        Left  = e,
                        Right = Exp0(input),
                    };
                }
                else
                {
                    break;
                }
            }
            return(e);
        }
Ejemplo n.º 8
0
 public static double Solve(this RawExpression f, RawExpression df, string name, double start, int maxCount = 1000)
 {
     return Solve((x) => f.Execute(name, x), (x) => df.Execute(name, x), start, maxCount);
 }
Ejemplo n.º 9
0
        private void TextBoxChanged()
        {
            RawExpression tempFunction = null;
            int tempUnitPixels = 0;
            double tempOriginX = 0;
            double tempOriginY = 0;

            try
            {
                tempFunction = RawExpression.Parse(textBoxFunction.Text);
            }
            catch (Exception e)
            {
                buttonRender.Enabled = false;
                labelErrorMessage.Text = "[Function]" + e.Message;
                return;
            }

            try
            {
                tempUnitPixels = int.Parse(textBoxUnitPixels.Text, NumberStyles.None);
            }
            catch (Exception e)
            {
                buttonRender.Enabled = false;
                labelErrorMessage.Text = "[UnitPixels]" + e.Message;
                return;
            }

            try
            {
                tempOriginX = double.Parse(textBoxOriginX.Text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign);
            }
            catch (Exception e)
            {
                buttonRender.Enabled = false;
                labelErrorMessage.Text = "[OriginX]" + e.Message;
                return;
            }

            try
            {
                tempOriginY = double.Parse(textBoxOriginY.Text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign);
            }
            catch (Exception e)
            {
                buttonRender.Enabled = false;
                labelErrorMessage.Text = "[OriginY]" + e.Message;
                return;
            }

            this.function = tempFunction;
            this.unitPixels = tempUnitPixels;
            this.originX = tempOriginX;
            this.originY = tempOriginY;
            buttonRender.Enabled = true;
            labelErrorMessage.Text = "(Ready)";
        }
Ejemplo n.º 10
0
 public static double Execute(this RawExpression e, string name, double value)
 {
     return(e.Execute(new Dictionary <string, double> {
         { name, value }
     }));
 }
Ejemplo n.º 11
0
 public static double Solve(this RawExpression f, RawExpression df, string name, double start, int maxCount = 1000)
 {
     return(Solve((x) => f.Execute(name, x), (x) => df.Execute(name, x), start, maxCount));
 }
Ejemplo n.º 12
0
 public static RawExpression Apply(this RawExpression e, string name, double value)
 {
     return(e.Apply(new Dictionary <string, double> {
         { name, value }
     }));
 }