Static class used to parse and evaluate expressions.
Esempio n. 1
0
		public override PointDataSourceBase TryBuild(object data)
		{
			string expression = data as string;
			if (expression != null)
			{
				try
				{
					Parser parser = new Parser("x");
					var expr = parser.Parse(expression).ToExpression<Func<double, double>>();
					Func<double, double> func = expr.Compile();

					DoubleLambdaDataSource ds = new DoubleLambdaDataSource(func);
					return ds;
				}
				catch (ParserException exc1)
				{
					try
					{
						Parser parser = new Parser("x", "t");
						var expr = parser.Parse(expression).ToExpression<Func<double, double, double>>();
						Func<double, double, double> func = expr.Compile();

						AnimatedDoubleLambdaDataSource ds = new AnimatedDoubleLambdaDataSource(func);
						return ds;
					}
					catch (ParserException exc2)
					{

					}
				}
			}

			return null;
		}
Esempio n. 2
0
 static void Main(string[] args)
 {
     List<Variable> variable = new List<Variable>();
     variable.Add(new Variable("x", 10));
     Parser parser = new Parser(variable);
     Console.WriteLine(parser.calculate("x"));
     Console.WriteLine("Write your Equation:");
     Console.WriteLine(parser.calculate(Console.ReadLine()));
     Console.WriteLine("Exit with any Key");
     Console.ReadLine();
 }
		public static ParsingResult Parse(this string expression)
		{
			Parser parser = new Parser();
			return parser.Parse(expression);
		}
		public static ParsingResult ParseWithParameters(this string expression, params string[] parameters)
		{
			Parser parser = new Parser(parameters);
			return parser.Parse(expression);
		}
Esempio n. 5
0
        /// <summary>
        /// Вычисление значения функции из текстбокса
        /// </summary>
        /// <param name="x">координата X</param>
        /// <param name="y">координата Y</param>
        /// <returns></returns>
        static double Function(double x, double y)
        {
            Parser p = new Parser();
            string tmp; // Текущее значение текстбокса будет храниться здесь.
            tmp = s.ToLower().Replace("x", x.ToString()).Replace("y", y.ToString()); //Заменяем в формуле все x и y на соответствующие им численные значения.

            var parsingResult = p.Parse(tmp); //Парсим формулу
            return (double)parsingResult.Evaluate(); //Вычисляем значение формулы
        }
Esempio n. 6
0
 public override void initials(String UX0, String VX0)
 {
     // Initialize initials
     // setting an initial waves at t = 0
     Parallel.Invoke(
         () =>
         {
             if (u != null)
             {
                 if (UX0.Equals(""))
                 {
                     for (int i = 0; i < N; i++)
                         u[0, i] = u_x_0(x[i]);
                 }
                 else
                 {
                     Parser pUX0 = new Parser();
                     for (int i = 0; i < N; i++)
                     {
                         pUX0.Evaluate(UX0.Replace("x", x[i].ToString()));
                         u[0, i] = pUX0.Result;
                     }
                 }
             }
         },
         () =>
         {
             if (v != null)
             {
                 if (VX0.Equals(""))
                 {
                     for (int i = 0; i < N; i++)
                         v[0, i] = v_x_0(x[i]);
                 }
                 else
                 {
                     if (v != null)
                     {
                         Parser pVX0 = new Parser();
                         for (int i = 0; i < N; i++)
                         {
                             pVX0.Evaluate(VX0.Replace("x", x[i].ToString()));
                             v[0, i] = pVX0.Result;
                         }
                     }
                 }
             }
         }
     );
 }
Esempio n. 7
0
        public double calculate()
        {
            double ret = 0;
            switch (type)
            {
                case "(":
                    ret = parameters[0];
                    break;

                case "sin(":
                    ret = Math.Sin(parameters[0]);
                    break;

                case "sqrt(":
                    if (parameters.Count == 1)
                        ret = Math.Sqrt(parameters[0]);
                    else
                        ret = Math.Pow(parameters[1], 1 / parameters[0]);
                    break;
                case "log(":
                    if (parameters.Count == 1)
                        ret = Math.Log(parameters[0]);
                    else
                        ret = Math.Log(parameters[1], parameters[0]);
                    break;

                case "abs(":
                    ret = Math.Abs(parameters[0]);
                    break;
                case "fac(":
                    ret = parameters[0];
                    int tempret = 1;
                    if (ret == (int)ret)
                    {
                        for (int ii = 2; ii <= (int)ret; ii++)
                            tempret *= ii;
                        ret = tempret;
                    }
                    else
                    {
                        ret = (Math.Sqrt(3 * Math.PI * (6 * ret + 1)) * Math.Pow(ret, ret) * Math.Pow(Math.E, -ret)) / 3;
                    }

                    break;
                case "integral(":
                    double min = parameters[0];
                    double max = parameters[1];                   
                    int n = (int)parameters[2];
                    double h = 0;
                    string trapezregel = "";
                    Parser parser = new Parser(null);

                    if (n > 0)
                    {
                        ret = 0;
                        h = (max - min) / n;
                        //(f(a) + f(b)) / 2
                        trapezregel = "(" + term.Replace("x", min.ToString(CultureInfo.CreateSpecificCulture("en-GB"))) +"+"+
                            term.Replace("x", max.ToString(CultureInfo.CreateSpecificCulture("en-GB"))) + ")/2";

                        ret += parser.calculate(trapezregel) ;

                        for (int ii = 1; ii < n; ii++)
                        {
                            //<-- + f(a+ i*h)
                            trapezregel = term.Replace("x", "(" + min.ToString(CultureInfo.CreateSpecificCulture("en-GB")) + "+" + ii.ToString() +
                                "*" + h.ToString(CultureInfo.CreateSpecificCulture("en-GB")) + ")");
                            ret += parser.calculate(trapezregel); 
                        }
                        ret *= h;
                    }
                    else
                        ret = 0;
                    
                    break;
            }
            return Math.Round(ret,4);
        }
 public void InitializeTransferParser()
 {
     TransferParser = new Parser("t");
     TransferParsingResult = TransferParser.Parse(TransferInfo);
 }
 public void InitializeOperatorParser()
 {
     OperatorParser = new Parser("t");
     OperatorParsingResult = OperatorParser.Parse(OperatorInfo);
 }
Esempio n. 10
0
 public void InitializeFunctionParser()
 {
     FunctionParser = new Parser("t");
     FunctionParsingResult = FunctionParser.Parse(FunctionInfo);
 }
 public static string Calculate(string expression)
 {
     Parser p = new Parser();
     if (p.Evaluate(expression)) return p.Result.ToString();
     else throw new Exception("calculate");
 }