Advertisement
V4L304

Untitled

Dec 18th, 2021
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using MathConsoleApplication;
  2. using org.mariuszgromada.math.mxparser;
  3. using System.Diagnostics;
  4.  
  5. public class Bisection
  6. {
  7.     private static float EPSILON = (float)0.01;
  8.     private static double func(string expression, double x)
  9.     {
  10.         expression = SobstitutionClass.replaceX(expression, x);
  11.         Expression e = new Expression(expression);
  12.         double res = e.calculate();
  13.         //Console.WriteLine(e.getErrorMessage().ToString());
  14.         return res;
  15.     }
  16.     public static void bisection(string expression,
  17.                           double a,
  18.                           double b)
  19.     {
  20.         if (a*b >= 0)
  21.         {
  22.             Console.WriteLine("a e b non sono corretti");
  23.             return;
  24.         }
  25.  
  26.         double c = a;
  27.         Stopwatch stopWatch = new Stopwatch();
  28.         stopWatch.Start();
  29.         while ((b - a) >= 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001)
  30.         {
  31.             // Find middle point
  32.             c = (a + b) / 2;
  33.  
  34.             // Check if middle
  35.             // point is root
  36.             if (func(expression, c) == 0.0)
  37.                 break;
  38.  
  39.             // Decide the side
  40.             // to repeat the steps
  41.             else if (func(expression, c) * func(expression, a) < 0)
  42.                 b = c;
  43.             else
  44.                 a = c;
  45.         }
  46.         stopWatch.Stop();
  47.         TimeSpan ts = stopWatch.Elapsed;
  48.         string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
  49.             ts.Hours, ts.Minutes, ts.Seconds,
  50.             ts.Milliseconds / 10);
  51.         Console.WriteLine($"la radice equivale a {Math.Round(c, 15)} in {elapsedTime}");
  52.     }
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. using System.Globalization;
  62.  
  63. namespace MathConsoleApplication
  64. {
  65.     public class SobstitutionClass
  66.     {
  67.         public static string replaceX(string expression, double x)
  68.         {
  69.             NumberFormatInfo nfi = new NumberFormatInfo();
  70.             nfi.NumberDecimalSeparator = ".";
  71.             for (int i = 0; i < expression.Length; i++)
  72.             {
  73.                 if (expression[i] == 'x')
  74.                 {
  75.                     if (i - 1 >= 0)
  76.                     {
  77.                         if (expression[i - 1] == '(' || expression[i - 1] == '+' || expression[i - 1] == '-' || expression[i - 1] == '*' || expression[i - 1] == '/')
  78.                         {
  79.                             expression = expression.Remove(i, "x".Length);
  80.                             expression = expression.Insert(i, $"({x.ToString(nfi)})");
  81.                         }
  82.                         else
  83.                         {
  84.                             string tmpRepace = $"*{x.ToString(nfi)}";
  85.                             expression = expression.Remove(i, "x".Length);
  86.                             expression = expression.Insert(i, tmpRepace);
  87.                         }
  88.                     }
  89.                     else
  90.                     {
  91.                         expression = expression.Remove(i, "x".Length).Insert(i, x.ToString());
  92.                     }
  93.                     {
  94.                     }
  95.                 }
  96.             }
  97.             return expression;
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement