Exemplo n.º 1
0
        public void USDtoCAD()
        {
            var result = mp.Evaluate("1 usd to cad");

            Assert.AreEqual(1, result.Value);
            Assert.AreEqual(CurrencyUnits.CAD, result.DesiredUnit);
        }
Exemplo n.º 2
0
        public void NewOperatorParsed()
        {
            var p = new MathParser();

            p.Operators.Add(new BinaryOperator<double>("$", 2, (x, y) =>
            {
                p.Manipulate("var x = " + x);
                p.Manipulate("var y = " + y);

                return p.Evaluate("x * y");
            }));

            Assert.AreEqual(p.Evaluate("3 $ 4"), 12);
        }
Exemplo n.º 3
0
        public static double Integral(string Expression, double LowerIndex, double UpperIndex, char IterationVariable, int Kind)
        {
            var parser = new MathParser();

            var Var = new Variable<double>(IterationVariable.ToString());

            parser.Variables.Add(Var);
            parser.Parse(Expression);

            Func<double,double> proc = x =>
                {
                    Var.Value = x;
                    return parser.Evaluate();
                };

            switch (Kind)
            {
                case 0:
                    return Integration.Trapezoidal(LowerIndex, UpperIndex, proc);

                case 1:
                    return Integration.LeftHand(LowerIndex, UpperIndex, proc);

                default:
                    return Integration.MidPoint(LowerIndex, UpperIndex, proc);
            }
        }
Exemplo n.º 4
0
 public void TestMathParser()
 {
     Assert.AreEqual(MathParser.Evaluate(0, 0, MathParser.ConvertToRPN("3^4")), 81f, 0.01, "Math Parser Test 1 Failed");
     Assert.AreEqual(MathParser.Evaluate(0, 0, MathParser.ConvertToRPN("(2^2)^(2^2)")), 256f, 0.01, "Math Parser Test 2 Failed");
     Assert.AreEqual(MathParser.Evaluate(0, 0, MathParser.ConvertToRPN("0.0001+(-0.4)")), -0.4f, 0.01, "Math Parser Test 3 Failed");
     Assert.AreEqual(MathParser.Evaluate(2, 2, MathParser.ConvertToRPN("0.0001+(x * y)")), 4f, 0.01, "Math Parser Test 4 Failed");
 }
Exemplo n.º 5
0
        public void NewOperator()
        {
            var p = new MathParser();

            p.Operators.Add(new BinaryOperator<double>("$", 2, (x, y) => x * y));

            Assert.AreEqual(p.Evaluate("3 $ 4"), 12);
        }
Exemplo n.º 6
0
        public static double Sigma(string Expression, double LowerIndex, double UpperIndex, char IterationVariable, double Step)
        {
            double Result = 0;

            var parser = new MathParser();

            var Var = new Variable<double>(IterationVariable.ToString());

            parser.Variables.Add(Var);
            parser.Parse(Expression);

            for (var i = LowerIndex; i <= UpperIndex; i += Step)
            {
                Var.Value = i;
                Result += parser.Evaluate();
            }

            return Result;
        }
Exemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <param name="targetType"></param>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="ConverterException"/>
        /// <exception cref="FormatException"/>
        /// <exception cref="OverflowException"/>
        public object Convert(object source, Type targetType)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (targetType is null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            if (source is string value)
            {
                if (targetType.IsEnum)
                {
                    if (long.TryParse(value, out long enumValue))
                    {
                        return(Enum.ToObject(targetType, enumValue));
                    }
                    else
                    {
                        return(Enum.Parse(targetType, value, true));
                    }
                }
                else if (targetType == typeof(int))
                {
                    return(MathParser.Evaluate(value).AsInt());
                }
                else if (targetType == typeof(uint))
                {
                    return(MathParser.Evaluate(value).AsUInt());
                }
                else if (targetType == typeof(float))
                {
                    return(MathParser.Evaluate(value).AsSingle());
                }
                else if (targetType == typeof(double))
                {
                    return(MathParser.Evaluate(value).AsDouble());
                }
                else if (targetType == typeof(byte))
                {
                    return(MathParser.Evaluate(value).AsByte());
                }
                else if (targetType == typeof(sbyte))
                {
                    return(MathParser.Evaluate(value).AsSByte());
                }
                else if (targetType == typeof(short))
                {
                    return(MathParser.Evaluate(value).AsShort());
                }
                else if (targetType == typeof(ushort))
                {
                    return(MathParser.Evaluate(value).AsUShort());
                }
                else if (targetType == typeof(long))
                {
                    return(MathParser.Evaluate(value).AsLong());
                }
                else if (targetType == typeof(ulong))
                {
                    return(MathParser.Evaluate(value).AsULong());
                }
                else if (targetType == typeof(decimal))
                {
                    return(MathParser.Evaluate(value).AsDecimal());
                }
                else if (targetType == typeof(bool))
                {
                    return(bool.Parse(value));
                }
                else if (targetType == typeof(Unit))
                {
                    if (Unit.TryParse(value, out var unit))
                    {
                        return(unit);
                    }
                }
                else if (targetType == typeof(Thickness))
                {
                    if (Thickness.TryParse(value, out var thickness))
                    {
                        return(thickness);
                    }
                }
                else if (targetType == typeof(Color))
                {
                    if (Color.TryParse(value, out var color))
                    {
                        return(color);
                    }
                }
                else if (targetType == typeof(Index))
                {
                    return(ParseIndex(value));
                }
                else if (targetType == typeof(Range))
                {
                    var split = value.Split('-', StringSplitOptions.RemoveEmptyEntries);
                    if (split.Length == 1)
                    {
                        var index = ParseIndex(split[0]);
                        return(index..index);
                    }
                    else if (split.Length == 2)
                    {
                        return(ParseIndex(split[0])..ParseIndex(split[1]));
                    }
                    else
                    {
                        throw new ConverterException($"Cannot convert '{value}' to range.");
                    }
                }
            }

            throw new ConverterException(source.GetType(), targetType);
        }
Exemplo n.º 8
0
        public void BitAsBit()
        {
            var result = mp.Evaluate("1bit");

            Assert.AreEqual((decimal)1 / 8, result.Value);
            Assert.AreEqual("1 Bit", result.ToString());
        }
Exemplo n.º 9
0
 public void HexPlusHex()
 {
     Assert.AreEqual(0x1 + 0x1, mp.Evaluate("0x1 + 0x1").Value);
 }
Exemplo n.º 10
0
        /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         *
         *                                              Îáðàáîòêà íàæàòèé íà êàíâàñ
         *
         * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         */

        /*
         * Íàæàòèå ËÊÌ íà êàíâàñ
         */
        private void ic_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var mousePoint = Mouse.GetPosition(this);

            switch (stateCursor)
            {
            case State.Ellipse:
                figureStart = mousePoint;
                ellipse     = getNewEllpse(mousePoint, mousePoint, getAllColor());
                ic.Children.Add(ellipse);
                break;

            case State.Rectangle:
                figureStart = mousePoint;
                rectangle   = getNewRectangle(mousePoint, mousePoint, getAllColor());
                ic.Children.Add(rectangle);
                break;

            case State.Line:
                line = getNewLine(mousePoint, mousePoint, getAllColor());
                ic.Children.Add(line);
                break;

            case State.Pen:
                if (isPaint)
                {
                    return;
                }

                var linePen = getNewLine(mousePoint, mousePoint, getAllColor());
                prev = mousePoint;
                ic.Children.Add(linePen);
                break;

            case State.Eraser:
                if (isPaint)
                {
                    return;
                }

                var lineEraser = getNewLine(mousePoint, mousePoint, Color.FromRgb(255, 255, 255));
                prev = mousePoint;
                ic.Children.Add(lineEraser);
                break;

            case State.Graph:

                mousePoint1 = mousePoint;

                MathBoard.GraphParserWindow parsG = new MathBoard.GraphParserWindow(mousePoint);
                parsG.Owner = this;
                parsG.ShowDialog();
                if (parsG.closeFlag)
                {
                    return;
                }
                parsG.closeFlag = true;
                string formula = parsG.InputTextBox.Text;

                List <Point> points = new List <Point>();
                MathParser   mp     = new MathParser(Mode.GRAD);
                MathExpression = formula;
                for (double i = START_X; i <= END_X; i += STEP)
                {
                    string temp = "";
                    temp = MathExpression.Replace("exp", "e_p");
                    temp = temp.Replace("x", i.ToString());
                    temp = temp.Replace("e_p", "exp");
                    if (mp.Evaluate(temp))
                    {
                        if ((mp.Result < 10 && mp.Result > -10) && (i < 10 && i > -10))
                        {
                            Point point = new Point(i, mp.Result);
                            points.Add(point);
                        }
                    }
                }
                DrawCoordinatePlane();
                DrawFunctionGraph(points);

                setStateCursor(State.None);

                break;

            case State.Formula:
                MathBoard.FormulaParserWindow pars = new MathBoard.FormulaParserWindow();
                pars.Owner = this;
                pars.ShowDialog();

                if (pars.imageCanvas.Source == null)
                {
                    setStateCursor(State.None);
                    return;
                }

                if (pars.saveFlag)
                {
                    setStateCursor(State.None);
                    pars.imageCanvas = null;
                    pars.saveFlag    = false;
                    return;
                }
                Image image = new Image()
                {
                    Source = pars.savePng,
                    Margin = new Thickness(mousePoint.X, mousePoint.Y - 25, 100, 100)
                };
                ic.Children.Add(image);
                setStateCursor(State.None);
                break;

            case State.Text:
                EnteringText x = new EnteringText();
                x.ShowDialog();
                if (x.DialogResult == true)
                {
                    string s = x.text.Text;

                    TextBlock TB = new TextBlock()
                    {
                        Margin   = new Thickness(mousePoint.X, mousePoint.Y - 80, 0, 0),
                        FontSize = getStroke()
                    };
                    TB.Text = s;
                    ic.Children.Add(TB);
                }
                break;

            case State.Image:
                OpenFileDialog myOpenFileDialog = new OpenFileDialog();
                myOpenFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";

                if (myOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    BitmapImage imageToCanvas = new BitmapImage(new Uri(myOpenFileDialog.FileName, UriKind.Absolute));
                    Image       imageControl  = new Image()
                    {
                        Source = imageToCanvas,
                        Margin = new Thickness(mousePoint.X, mousePoint.Y - 80, 0, 0),
                        Width  = 200,
                        Height = 200
                    };
                    ic.Children.Add(imageControl);
                }
                break;
            }
            isPaint = true;
        }
Exemplo n.º 11
0
 public void InvalidEvaluation(string input)
 {
     Assert.ThrowsAny <Exception>(() => MathParser.Evaluate(input));
 }
Exemplo n.º 12
0
        public void TestEvaluation(string input, double result)
        {
            var output = MathParser.Evaluate(input).AsDouble();

            Assert.Equal(result, output, 3);
        }
Exemplo n.º 13
0
 public void ACosUnitTestValid()
 {
     Assert.AreEqual((decimal)Math.Acos(.001), mp.Evaluate("Acos(.001)").Value);
     Assert.AreEqual("1.569796", mp.Evaluate("Acos(.001)").ToString());
 }
Exemplo n.º 14
0
 public void LongToStringTest()
 {
     Assert.AreEqual("helo", mp.Evaluate("1751477359 as string").ToString());
 }
Exemplo n.º 15
0
 public void VolumeGallonToWeightOunces()
 {
     Assert.AreEqual("128.000984 Ounces", mp.Evaluate("1 gallon to ounces").ToString());
 }
Exemplo n.º 16
0
 public void TimeTest()
 {
     Assert.AreEqual(60, Math.Round(mp.Evaluate("1 minute as seconds").Value, 6));
 }
Exemplo n.º 17
0
 public void CtoFConvert()
 {
     Assert.AreEqual("98.6 Fahrenheit", mp.Evaluate("37c to f").ToString());
 }
Exemplo n.º 18
0
 public void SimpleDistanceConvert()
 {
     Assert.AreEqual("12 Inches", mp.Evaluate("1 foot to inches").ToString());
     Assert.AreEqual("12 Inches", mp.Evaluate("1' to inches").ToString());
 }