-1

I have a txtBox and a button to calculate the area under the function I want to know how to take the txt and make it a function like this "-x * x * x + 10 * x * x + 8 * x + 10;"

 private void button1_Click(object sender, EventArgs e)
    {
        string pivote;
        pivote = txtEcuacion1.Text;
        double ecuacion;
        double base_rectangulo, altura_1, altura_2, altura_total, x;
        double partes = 8, area = 0;
        double lim_inferior = 3, lim_superior = 7;
        base_rectangulo = (lim_superior - lim_inferior) / partes;
        x = lim_inferior;
        while (x < lim_superior)
        {

            altura_1 = Convert.ToDouble(pivote);//-x * x * x + 10 * x * x + 8 * x + 10;
            x = x + base_rectangulo;
            altura_2 = Convert.ToDouble(pivote); //-x * x * x + 10 * x * x + 8 * x + 10;
            altura_total = (altura_1 + altura_2) / 2;
            area = area + base_rectangulo * altura_total;


        }
        MessageBox.Show("El valor del area es" + area);
    }

In "altura_1" I tried to convert it to double but it didn't work

  • 5
    Possible duplicate of [Evaluate string with math operators](https://stackoverflow.com/questions/5838918/evaluate-string-with-math-operators) – 41686d6564 Oct 31 '19 at 05:00
  • Does this answer your question? [Is it possible to compile and execute new code at runtime in .NET?](https://stackoverflow.com/questions/234217/is-it-possible-to-compile-and-execute-new-code-at-runtime-in-net) – Ian Kemp Oct 31 '19 at 07:21

1 Answers1

1

I'm not really sure what you are trying to do. But if you are trying to solve a string as a calculation, i can help you.

The easiest way to work around this, is using a library. The library i used for this project is: "Zirpl.CalcEngine" Get it here.

static void Main(string[] args)
    {
        String Calculation = "2 * 5 * 7 * 6 / 4 + 2"; ///The calculation, you can take this from txtBox
        CalculationEngine engine = new CalculationEngine(); /// Creating a new calculatorEngine
        var result = engine.Evaluate(Calculation); ///Evaluate the calculation, and store it in a "var"
        Console.WriteLine(result);  ///Result is 107

        Console.ReadKey(); 
    }

Make sure you don't forget to use the library. Add this: using Zirpl.CalcEngine;

I hope this helps you. Good luck!

Twan

Twan
  • 21
  • 4