コード例 #1
0
        private void StartBtn_Click(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(this.InputTextX.Text);
                int    n = int.Parse(this.InputTextN.Text);
                double E = double.Parse(this.InputTextE.Text);

                if ((x > -1) & (x < 1))
                {
                    FunctionCalc calc = new FunctionCalc(x);

                    string result = "Сумма N слагаемых: " + calc.CalcFunctionWith_N_E(n, 0) + Environment.NewLine;
                    result += "Сумма слагаемых, больших заднанного Е: " + calc.CalcFunctionWith_N_E(n, E) + Environment.NewLine;
                    result += "Сумма слагаемых, больших заданного Е/10: " + calc.CalcFunctionWith_N_E(n, E / 10) + Environment.NewLine;
                    result += "Точное значение функции: " + calc.CalcFunction();

                    this.ResultText.Text = result;
                }
                else
                {
                    MessageBox.Show("Необходимо ввести значение Х в интервале (-1: 1)!");
                    InputTextX.Text = "";
                }
            }

            catch (Exception exc)
            {
                MessageBsc.ShowError(exc.Message);
            }
        }
コード例 #2
0
        // -----------------------------
        private static IList <EqnCalc> TestEquationParser()
        {
            IList <EqnCalc> retList = new List <EqnCalc>();

            IList <string> eqnStrings = new List <string>();

            // Engineering: Heat: Heat Exchanger
            // Engineering: Equipment: Heat Exchanger
            eqnStrings.Add("Q = m*cp*(T1-T2)");
            eqnStrings.Add("Q = U*A*DTLM");
            eqnStrings.Add("DTLM = (DT1-DT2)/LOG(DT1/DT2)");
            eqnStrings.Add("DTLM = ((Thi-Tco)-(Tho-Tci))/LOG((Thi-Tco)/(Tho-Tci))");

            // Geometry: Solid Bodies: Cylinder
            eqnStrings.Add("V = PI*r^2*h");
            eqnStrings.Add("Am = 2*PI*r*h");
            eqnStrings.Add("A0 = 2*PI*r*(r+h)");

            foreach (var item in eqnStrings)
            {
                JongErrWarn  errW    = null;
                FunctionCalc funCalc = _contentManager.CreateFunctionCalcFromExpression(item, null, null, out errW);
                if (funCalc != null)
                {
                    retList.Add((EqnCalc)funCalc);
                }
            }
            return(retList);
        }
コード例 #3
0
        //Dictionary<double, double> zz = new Dictionary<double, double>();
        //private double aaa;
        //private double bbb;
        //private double aaaY;
        //private double bbbY;

        private bool TryGetPointAt(IOperation operation, double screenX, out int value)
        {
            value = -1;

            var x = FunctionCalc.GetXFromScreenX(screenX);

            var result = FunctionCalc.GetYFromX(operation, x);

            if (double.IsNaN(result) || double.IsInfinity(result))
            {
                return(false);
            }

            result = FunctionCalc.GetScreenYFromY(result);

            //if (result > Height)
            //    result = Height;
            //if (result < -1)
            //    result = -1;
            result = MinMax(result, -1, Height);

            value = (int)result;
            //if (value >= 0 && value < Height)
            {
                return(true);
            }

            //return false;
        }
コード例 #4
0
        private void DrawCoordinateSystem(Graphics graphics)
        {
            const int borderSpace = 10;

            var pen   = new Pen(Color.Blue);
            var brush = new SolidBrush(Color.Blue);
            var font  = new Form().Font;

            // vertical line
            var lineX = MinMax(
                (int)FunctionCalc.GetScreenXFromX(0),
                borderSpace * 2,
                Width - borderSpace * 2);

            graphics.DrawLines(pen, GetLineWithArrow(lineX, borderSpace, lineX, Height - borderSpace));

            foreach (var y in GetLabelPoints(FunctionCalc.Scale.Y, FunctionCalc.GetYFromScreenY(Height - borderSpace),
                                             borderSpace, FunctionCalc.GetScreenYFromY))
            {
                graphics.DrawLine(pen,
                                  lineX - 2, (int)y,
                                  lineX + 2, (int)y);

                var value = (float)FunctionCalc.GetYFromScreenY(y);
                if (Math.Abs(value) > 1e-12)
                {
                    graphics.DrawString(value.ToString(CultureInfo.InvariantCulture),
                                        font, brush, lineX + 5, (int)y - 4);
                }
            }

            // horizontal line
            var lineY = MinMax(
                (int)FunctionCalc.GetScreenYFromY(0),
                borderSpace * 2,
                Height - borderSpace * 2);

            graphics.DrawLines(pen, GetLineWithArrow(borderSpace, lineY, Width - borderSpace, lineY));

            foreach (var x in GetLabelPoints(FunctionCalc.Scale.X, FunctionCalc.GetXFromScreenX(borderSpace),
                                             Width - borderSpace, FunctionCalc.GetScreenXFromX))
            {
                graphics.DrawLine(pen,
                                  (int)x, lineY - 2,
                                  (int)x, lineY + 2);

                var value = (float)FunctionCalc.GetXFromScreenX(x);
                if (Math.Abs(value) > 1e-12)
                {
                    graphics.DrawString(value.ToString(CultureInfo.InvariantCulture),
                                        font, brush, (int)x - 4, lineY + 5);
                }
                else
                {
                    if (Math.Abs(value) > 0)
                    {
                    }
                }
            }
        }
コード例 #5
0
        public async Task <EqnCalc> ShowCreateEquation(ContentManager cm)
        {
            Tuple <bool, string, string, string, IList <VarInfo> > tpl;

            tpl = await ShowAddEquation(cm);

            try
            {
                bool            bCancelled         = tpl.Item1;
                string          equationString     = null;
                string          userEquationString = null;
                string          eqDescription      = null;
                IList <VarInfo> varInfos           = null;

                if (!bCancelled)
                {
                    equationString     = tpl.Item2;
                    userEquationString = tpl.Item3;
                    eqDescription      = tpl.Item4;
                    varInfos           = tpl.Item5;

                    Debug.WriteLine("equationString={0}, userEquationString={1},eqDescription={2}");
                    Debug.WriteLine(varInfos);

                    JongErrWarn  errW    = null;
                    FunctionCalc funCalc = cm.CreateFunctionCalcFromExpression(equationString, eqDescription, varInfos, out errW);

                    EqnCalc equationCalc = funCalc as EqnCalc;

                    if (equationCalc == null)
                    {
                        // TODO
                    }
                    else
                    {
                        // TODO
                        //EquationLibraries.Add(equationCalc);
                        return(equationCalc);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
                throw;
            }

            return(null);
        }
コード例 #6
0
        public void RefreshEquationCalc(string eqnString)
        {
            UserEquationString = eqnString;

            if (eqnString?.Length > 0)
            {
                JongErrWarn  errW    = null;
                FunctionCalc funCalc = ContentManager.CreateFunctionCalcFromExpression(eqnString, null, null, out errW);

                EquationCalc = funCalc as EqnCalc;

                EquationMsg = errW?.ErrWarnString;
            }
            else
            {
                EquationCalc = null;
                EquationMsg  = DefaultEquationMsg;
            }

            OnPropertyChanged("UserEquationString");
            OnPropertyChanged("EquationCalc");
            OnPropertyChanged("EquationMsg");
        }
コード例 #7
0
        private static void GetCalculationTreeRows2(FunctionCalc fnCalc, IList <CalcTreeRowInfo> calculationTreeRows, string sCurrentIndent, string sIndentStep, string NumberFormat)
        {
            CalcTreeRowInfo rowInfo = new CalcTreeRowInfo();

            rowInfo.MathExpression = fnCalc;

            if (fnCalc.Function is FnEquals)
            {
                rowInfo.DisplayText = string.Format("{0}{1} ({2}) {3}\n",
                                                    sCurrentIndent, fnCalc.Function.Name, fnCalc.Function.AsciiSymbol,
                                                    (fnCalc.CalcQuantity.Message.Length == 0) ? "" : (" // " + fnCalc.CalcQuantity.Message));
            }
            else
            {
                rowInfo.DisplayText = string.Format("{0}{1} ({2}) = {3}{4}\n",
                                                    sCurrentIndent, fnCalc.Function.Name, fnCalc.Function.AsciiSymbol,
                                                    ((fnCalc.GetCalcStatus() == CalcStatus.Good) ?
                                                     string.Format("{0:" + NumberFormat + "}", fnCalc.CalcQuantity.Value) :
                                                     "!"),
                                                    (fnCalc.CalcQuantity.Message.Length == 0) ? "" : (" // " + fnCalc.CalcQuantity.Message));
            }

            calculationTreeRows.Add(rowInfo);

            // ----------------------
            foreach (SingleResult input in fnCalc.Inputs)
            {
                if (input is FunctionCalc)
                {
                    GetCalculationTreeRows2(((FunctionCalc)input), calculationTreeRows, (sCurrentIndent + sIndentStep), sIndentStep, NumberFormat);
                }
                else
                {
                    GetCalculationTreeRows2(input, calculationTreeRows, (sCurrentIndent + sIndentStep), NumberFormat);
                }
            }
        }
コード例 #8
0
        // ------------------------

        private View DisplayFunctionCalc(FunctionCalc expr, bool bSkipOpBrackets, Function lastFn)
        {
            bool bThisSkipOpBrackets = bSkipOpBrackets;
            bool bNextSkipOpBrackets = false;

            StackLayout stackLayout = new StackLayout
            {
                Orientation     = StackOrientation.Horizontal,
                VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
            };

            Function fn = expr.Function;

            if (fn is FnEquals)
            {
                bNextSkipOpBrackets = true;
            }

            if (fn.FuncLayout == FuncLayout.FuncLayout)
            {
                stackLayout.Children.Add(new FuncNameResultLabel
                {
                    Text            = fn.Symbol,
                    VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                    SingleResult    = expr
                });

                stackLayout.Children.Add(new OpenBracketResultLabel
                {
                    Text            = "(",
                    VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                    SingleResult    = expr
                });

                AddExpressionInputs(stackLayout, expr.Inputs);

                stackLayout.Children.Add(new CloseBracketResultLabel
                {
                    Text            = ")",
                    VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                    SingleResult    = expr
                });
            }
            else if (fn.FuncLayout == FuncLayout.Op_Term)
            {
                bThisSkipOpBrackets = true;

                if (!bThisSkipOpBrackets)
                {
                    stackLayout.Children.Add(new OpenBracketResultLabel
                    {
                        Text            = "(",
                        VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                        SingleResult    = expr
                    });
                }

                stackLayout.Children.Add(new OpResultLabel
                {
                    Text            = fn.Symbol,
                    VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                    SingleResult    = expr
                });

                stackLayout.Children.Add(DisplayMathExpression(expr.Inputs[0], bSkipOpBrackets: bNextSkipOpBrackets, lastFn: fn));

                if (!bThisSkipOpBrackets)
                {
                    stackLayout.Children.Add(new CloseBracketResultLabel
                    {
                        Text            = ")",
                        VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                        SingleResult    = expr
                    });
                }
            }
            else if (fn.FuncLayout == FuncLayout.Term_Op_Term)
            {
                if (fn == lastFn)        // is Functions.FnMultiply)
                {
                    bThisSkipOpBrackets = true;
                }
                if (!bThisSkipOpBrackets)
                {
                    stackLayout.Children.Add(new OpenBracketResultLabel
                    {
                        Text            = "(",
                        VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                        SingleResult    = expr
                    });
                }

                stackLayout.Children.Add(DisplayMathExpression(expr.Inputs[0], bSkipOpBrackets: bNextSkipOpBrackets, lastFn: fn));

                stackLayout.Children.Add(new OpResultLabel
                {
                    Text            = fn.Symbol,
                    VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                    SingleResult    = expr
                });

                stackLayout.Children.Add(DisplayMathExpression(expr.Inputs[1], bSkipOpBrackets: bNextSkipOpBrackets, lastFn: fn));

                if (!bThisSkipOpBrackets)
                {
                    stackLayout.Children.Add(new CloseBracketResultLabel
                    {
                        Text            = ")",
                        VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                        SingleResult    = expr
                    });
                }
            }
            else if (fn.FuncLayout == FuncLayout.Term_OverOp_Term)
            {
                if (!(fn is FnDivide))
                {
                    // TODO: Equations: What else could it be? What do we show instead of a line?
                }

                if (!bThisSkipOpBrackets)
                {
                    stackLayout.Children.Add(new OpenBracketResultLabel
                    {
                        Text              = "(",
                        VerticalOptions   = LayoutOptions.Center,
                        HorizontalOptions = LayoutOptions.Center,
                        SingleResult      = expr
                    });
                }

                StackLayout slVert = new StackLayout
                {
                    Orientation     = StackOrientation.Vertical,
                    VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                };
                stackLayout.Children.Add(slVert);

                View vwTop = DisplayMathExpression(expr.Inputs[0], bSkipOpBrackets: bNextSkipOpBrackets, lastFn: fn);
                slVert.Children.Add(vwTop);

                BoxView boxView = new BoxView
                {
                    Color           = (Color)Application.Current.Resources[App.Key_TextColor],
                    HeightRequest   = _lineDepth,
                    VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                };

                slVert.Children.Add(boxView);

                View vwBottom = DisplayMathExpression(expr.Inputs[1], bSkipOpBrackets: bNextSkipOpBrackets, lastFn: fn);
                slVert.Children.Add(vwBottom);

                MaxWidthBinder maxWidthBinder = new MaxWidthBinder(new List <View> {
                    vwTop, vwBottom
                });
                boxView.SetBinding(BoxView.WidthRequestProperty, new Binding()
                {
                    Source = maxWidthBinder, Path = "MaxWidth"
                });

                if (!bThisSkipOpBrackets)
                {
                    stackLayout.Children.Add(new CloseBracketResultLabel
                    {
                        Text              = ")",
                        VerticalOptions   = LayoutOptions.Center,
                        HorizontalOptions = LayoutOptions.Center,
                        SingleResult      = expr
                    });
                }
            }
            else if (fn.FuncLayout == FuncLayout.Term_Superscript_Term)
            {
                StackLayout slHoriz = new StackLayout
                {
                    Orientation       = StackOrientation.Horizontal,
                    VerticalOptions   = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center,
                };
                stackLayout.Children.Add(slHoriz);

                View vw = DisplayMathExpression(expr.Inputs[0], bSkipOpBrackets: bNextSkipOpBrackets, lastFn: fn);
                slHoriz.Children.Add(vw);

                SuperscriptText ssText = new SuperscriptText()
                {
                    Text = FunctionCalc.MathExpressionAsText(expr.Inputs[1]),       // Not perfect for a complex exponent....
                    //VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center,
                    BackgroundColor = (Color)Application.Current.Resources[App.Key_BackgroundColor],
                    TextColor       = (Color)Application.Current.Resources[App.Key_TextColor],
                };

                ssText.SetBinding(SuperscriptText.FontSizeProperty, new Binding()
                {
                    Source = vw, Path = "FontSize"
                });
                slHoriz.Children.Add(ssText);
            }

            return(stackLayout);
        }
コード例 #9
0
        /// <summary>
        /// Return information about how the expression was calculated
        /// </summary>
        /// <param name="sIndent">Indentation used for subsequent levels of calculationTree</param>
        /// <param name="calculationTree">A string representation of how the calculation was conducted, and the errors at different levels</param>
        private void GetCalculationTreeRows(FunctionCalc fnCalc, IList <CalcTreeRowInfo> calculationTreeRows, string sIndent, string NumberFormat)
        {
            calculationTreeRows.Clear();

            GetCalculationTreeRows2(fnCalc, calculationTreeRows, "", sIndent, NumberFormat);
        }