2

I have made bisection method program in C# Console Application. Bisection method works, but for function which is already written in the code. I want to edit program that user can input function which they want to use for bisection method. For example Console.ReadLine() for input "x^2 + x - 2" and then I want it automatically written after return in the code below.

        static double Function(double x)
    {
        return x*x - 2;
    } //this is Function which I used in code.

Here is the whole code. (as i mentioned it works for function which is written in static double Function(double x) part

using System;

namespace MPI
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            // in [a,b]
            double inPoc = 0; //a
            double inKraj = 0; //b
            double sredina = 0;
            double tacnost = 0;



        Start:
            int i = 0; //brojac


            Console.Write("Unesite početak intervala: ");
            inPoc = Convert.ToDouble(Console.ReadLine());
            Console.Write("Unesite kraj intervala: ");
            inKraj = Convert.ToDouble(Console.ReadLine());
            Console.Write("Unesite tacnost: ");
            tacnost = Convert.ToDouble(Console.ReadLine());

            sredina = (inPoc + inKraj) / 2;

            if (Function(inPoc) * Function(inKraj) < 0)
            {


                while ((Math.Abs(inPoc - inKraj)) > tacnost)
                {
                    sredina = (inPoc + inKraj) / 2;

                    Console.WriteLine("trenutno X: " + sredina);
                    Console.WriteLine("Funkcija za trenutno x ima vrednost: " + Function(sredina));
                    Console.WriteLine("");
                    i++;


                    if (Function(sredina) < 0)
                    {
                        inPoc = sredina;
                    }
                    else
                    {
                        inKraj = sredina;
                    }
                }
                Console.WriteLine("X: " + sredina);
                Console.WriteLine("Broj izvrsenih koraka je " + i);
            }

            else
            {
                Console.WriteLine("Krajevi intervala funkcije su istog znaka");
                Console.WriteLine();

            }


            goto Start; //sluzi da vrati program na pocetak kako bi ga opet koristili

        }


        static double Function(double x)
        {
            return x*x - 2; //primer funkcije
        }
    }
}
  • It could be handy if you translate the strings and comments in your code, so everyone can understand what they say. – Headhunter Xamd Oct 25 '18 at 09:31
  • Did you look at `dotnet-script`? There the user could interactively write your code and no parsing would be needed (https://www.hanselman.com/blog/CAndNETCoreScriptingWithTheDotnetscriptGlobalTool.aspx). Another possibility would be to use Reflection to interpret the user input directly as C# code: https://www.codeproject.com/Articles/9019/Compiling-and-Executing-Code-at-Runtime – Peter Ittner Oct 25 '18 at 10:00

1 Answers1

2

Looks like this question is asking about the same.

There are two solutions to do this:

Solution 1 - just use Flee.

Copy-paste from documentation:

ExpressionContext context = new ExpressionContext();
VariableCollection variables = context.Variables;
variables.Add("a", 100);
variables.Add("b", 1);
variables.Add("c", 24);

IGenericExpression<bool> e = context.CompileGeneric<bool>("(a = 100 OR b > 0) AND c <> 2");
bool result = e.Evaluate();

So you can do the same, just change input/output types and put your input line into the CompileGeneric

Solution 2 - parse input string manually.

So question can be divided to the two parts:

  • How to parse input string into the expression tree.
  • How to execute this tree.

For the first item - please check reverse polish notation. It allows to construct computation stack.

Next you will able to compute your expression tree. Each operand (after trimming) will have variable or integer constant. So just replace variable to the actual value and just parse string to the integer.

Manushin Igor
  • 3,398
  • 1
  • 26
  • 40