Example #1
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();
 }
Example #2
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);
        }