From 33820e972b3843a49f670330af4af826fd39806e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=20=D0=98=D0=B2=D0=B0=D1=88=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Sun, 15 Dec 2024 15:47:04 +0500 Subject: [PATCH] Add function parser --- .../Windows/MainGraph/FormulaViewModel.cs | 6 ++-- .../Windows/MainGraph/GraphViewModel.cs | 2 +- .../Interfaces/FunctionDecorator.cs | 1 + .../Interfaces/IOneVariableFunction.cs | 1 + .../Models/Functions/FormulaFunction.cs | 34 +++++++++++++++++-- .../Models/Functions/TableFunction.cs | 1 + .../StructureHelperCommon.csproj | 3 ++ 7 files changed, 42 insertions(+), 6 deletions(-) diff --git a/StructureHelper/Windows/MainGraph/FormulaViewModel.cs b/StructureHelper/Windows/MainGraph/FormulaViewModel.cs index c049c84..8d85811 100644 --- a/StructureHelper/Windows/MainGraph/FormulaViewModel.cs +++ b/StructureHelper/Windows/MainGraph/FormulaViewModel.cs @@ -23,8 +23,8 @@ namespace StructureHelper.Windows.MainGraph private const string DEFAULT_NAME = "Put function name here..."; private const string DEFAULT_DESCRIPTION = "Put function description here..."; private const string DEFAULT_FORMULA = "x^2"; - private const double DEFAULT_LEFT_BOUND = -500; - private const double DEFAULT_RIGHT_BOUND = 500; + private const double DEFAULT_LEFT_BOUND = 1; + private const double DEFAULT_RIGHT_BOUND = 1000; private const int DEFAULT_STEP = 100; private const int MAX_STEP = 1000; public char GREATER { get; } = '\u2265'; @@ -171,6 +171,8 @@ namespace StructureHelper.Windows.MainGraph (Function as FormulaFunction).Step = Step; (Function as FormulaFunction).Formula = Formula; Function.Color = Color; + Function.MinArg = LeftBound; + Function.MaxArg = RightBound; var window = parameter as Window; if (LeftBound > RightBound) { diff --git a/StructureHelper/Windows/MainGraph/GraphViewModel.cs b/StructureHelper/Windows/MainGraph/GraphViewModel.cs index 8d3a99e..6a476b0 100644 --- a/StructureHelper/Windows/MainGraph/GraphViewModel.cs +++ b/StructureHelper/Windows/MainGraph/GraphViewModel.cs @@ -111,7 +111,7 @@ namespace StructureHelper.Windows.MainGraph f2.Name = "Формульная системная функция"; f2.Formula = "x^2"; f2.Step = 100; - f2.MinArg = -1000; + f2.MinArg = 1; f2.MaxArg = 1000; f2.IsUser = false; f2.Description = "Описание формульной системной функции"; diff --git a/StructureHelperCommon/Infrastructures/Interfaces/FunctionDecorator.cs b/StructureHelperCommon/Infrastructures/Interfaces/FunctionDecorator.cs index 5f8d62d..84f1124 100644 --- a/StructureHelperCommon/Infrastructures/Interfaces/FunctionDecorator.cs +++ b/StructureHelperCommon/Infrastructures/Interfaces/FunctionDecorator.cs @@ -30,6 +30,7 @@ namespace StructureHelperCommon.Infrastructures.Interfaces public IShiftTraceLogger? TraceLogger { get; set; } public Color Color { get; set; } + public string Trace { get; set; } public FunctionDecorator(IOneVariableFunction function) { diff --git a/StructureHelperCommon/Infrastructures/Interfaces/IOneVariableFunction.cs b/StructureHelperCommon/Infrastructures/Interfaces/IOneVariableFunction.cs index e83713e..f132c82 100644 --- a/StructureHelperCommon/Infrastructures/Interfaces/IOneVariableFunction.cs +++ b/StructureHelperCommon/Infrastructures/Interfaces/IOneVariableFunction.cs @@ -25,6 +25,7 @@ namespace StructureHelperCommon.Infrastructures.Interfaces public double MinArg { get; set; } public double MaxArg { get; set; } public Color Color { get; set; } + public string Trace { get; set; } public ObservableCollection Functions { get; set; } public bool Check(); public double GetByX(double xValue); diff --git a/StructureHelperCommon/Models/Functions/FormulaFunction.cs b/StructureHelperCommon/Models/Functions/FormulaFunction.cs index 4bb43df..e4f8ef0 100644 --- a/StructureHelperCommon/Models/Functions/FormulaFunction.cs +++ b/StructureHelperCommon/Models/Functions/FormulaFunction.cs @@ -13,11 +13,15 @@ using System.Windows.Documents; using StructureHelperCommon.Services; using LiveCharts.Wpf; using StructureHelperCommon.Services.ColorServices; +using FunctionParser; namespace StructureHelperCommon.Models.Functions { public class FormulaFunction : IOneVariableFunction { + private double current_xValue; + private string formula; + private Expression expression; private const string COPY = "copy"; public const string GROUP_TYPE_1 = "System function"; public const string GROUP_TYPE_2 = "User function"; @@ -27,13 +31,36 @@ namespace StructureHelperCommon.Models.Functions public string Name { get; set; } public string Description { get ; set; } public int Step { get; set; } - public string Formula { get; set; } + public string Formula + { + get + { + return formula; + } + set + { + formula = value; + Expression = new Expression(value, new string[] { "x" }, null); + } + } + public Expression Expression + { + get + { + return expression; + } + set + { + expression = value; + } + } public Guid Id => throw new NotImplementedException(); public ObservableCollection Functions { get; set; } = new ObservableCollection(); public double MinArg { get; set; } public double MaxArg { get; set; } public IShiftTraceLogger? TraceLogger { get; set; } public Color Color { get; set; } + public string Trace { get; set; } public FormulaFunction(bool isUser = false) { @@ -55,7 +82,6 @@ namespace StructureHelperCommon.Models.Functions { throw new NotImplementedException(); } - public object Clone() { var formulaFunction = new FormulaFunction(); @@ -76,7 +102,9 @@ namespace StructureHelperCommon.Models.Functions public double GetByX(double xValue) { double yValue = 0; - yValue = Math.Round(Math.Pow(xValue, 2), 2); //Временно выражение квадратичной параболы, будет разбор выражения + current_xValue = xValue; + Check(); + yValue = Math.Round(Expression.CalculateValue(new double[] { xValue }), 2); return yValue; } public GraphSettings GetGraphSettings() diff --git a/StructureHelperCommon/Models/Functions/TableFunction.cs b/StructureHelperCommon/Models/Functions/TableFunction.cs index e11dee6..1d010e1 100644 --- a/StructureHelperCommon/Models/Functions/TableFunction.cs +++ b/StructureHelperCommon/Models/Functions/TableFunction.cs @@ -34,6 +34,7 @@ namespace StructureHelperCommon.Models.Functions public double MaxArg { get; set; } public IShiftTraceLogger? TraceLogger { get; set; } public Color Color { get; set; } + public string Trace { get; set; } public TableFunction(bool isUser = false) { diff --git a/StructureHelperCommon/StructureHelperCommon.csproj b/StructureHelperCommon/StructureHelperCommon.csproj index d947f40..0d7c275 100644 --- a/StructureHelperCommon/StructureHelperCommon.csproj +++ b/StructureHelperCommon/StructureHelperCommon.csproj @@ -15,6 +15,9 @@ + + ..\..\FunctionParser\FunctionParser\bin\Debug\FunctionParser.dll + ..\StructureHelper\Libraries\LoaderCalculator.dll