Refactored into more modern coding style.
authorPat Thoyts <patthoyts@users.sourceforge.net>
Tue, 5 Aug 2014 14:54:11 +0000 (15:54 +0100)
committerPat Thoyts <patthoyts@users.sourceforge.net>
Wed, 6 Aug 2014 20:39:31 +0000 (21:39 +0100)
Eliminated all the goto statements while maintaining passing
the regression test.

Signed-off-by: Pat Thoyts <patthoyts@users.sourceforge.net>
MervsSolver/Program.cs
MervsSolver/Solver.cs

index 09156eb9caa834d66372ea3d0bd347829cd54cb2..d7ba69fbc3991afe09a536c378f2079c532cf6a4 100644 (file)
@@ -17,12 +17,9 @@ namespace MervsSolver
             {
                 Solver solver = new Solver(x0, v0, mu, steps);
                 Process gnuplot = Gnuplot(mu);
-                //Console.WriteLine("{0} results", solver.Result.Count);
-                int i = 0;
                 foreach (Coord coord in solver.Result)
                 {
                     gnuplot.StandardInput.WriteLine("{0:0.000}\t{1:0.000}", coord.X, coord.Y);
-                    ++i;
                 }
                 gnuplot.StandardInput.Close();
                 gnuplot.WaitForExit();
index 9f5db2873908665b4a009d5e8ba4a08d538b27aa..73a70cc0663e8cec3fa21968ad20f0a66108700a 100644 (file)
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 
 namespace MervsSolver
 {
@@ -15,63 +16,60 @@ namespace MervsSolver
             double mu = continuousParameter;
             double d = stepSize;
 
-            double[] A = new double[300];
-            double[] E = new double[300];
-            double b = 0, c = 0, z = 0, s = 0, h = 0, x1 = 0, v1 = 0;
-            int i = 0;
+            double[] A = new double[50+1];
+            double[] E = new double[50+1];
+            double z = 0;
 
             if (v0 < 0)
-                goto Line320;
-            if (v0 > 0)
-                goto Line300;
-            if (x0 > 0)
-                goto Line320;
-        Line300:
-            z = d;
-            goto Line330;
-        Line320:
-            z = -d;
-        Line330:
-            x1 = x0 + z;
-            b = equation(v0, z, mu, x1);
-            c = z * x1;
-            s = (b * b) - 4 * c;
-            if (s < 0)
-                goto Line540;
-            double k = z / Math.Abs(z);
-            v1 = (-b + k * Math.Sqrt((b * b) - 4 * c)) / 2;
-        Line430:
-            i = i + 1;
-            A[i] = x1;
-            E[i] = v1;
-            h = h + 1;
-            if (h == 50)
-                goto Line590;
-        Line480:
-            v0 = v1;
-            x0 = x1;
-            if (v0 == 0)
-                goto Line520;
-            goto Line330;
-        Line520:
-            if (i == 0 || i > 500)
-                return;
-            if (E[i - 1] < 0)
-                goto Line300;
-            goto Line320;
-        Line540:
-            z = z / 10;
-            if (Math.Abs(z) < 1.0e-3)
-                goto Line570;
-            goto Line330;
-        Line570:
-            v1 = 0;
-            goto Line430;
-        Line590:
-            for (i = 1; i != h; ++i)
-                mResult.Add(new Coord(0 + (A[i] * -75), (E[i] * -75)));
-            h = 0; i = 0;
-            goto Line480;
+                z = -d;
+            else if (v0 > 0)
+                z = d;
+            else if (x0 > 0)
+                z = -d;
+            else
+                z = d;
+
+            int i = 0;
+            while (true)
+            {
+                double v1;
+                double x1 = x0 + z;
+                double b = equation(v0, z, mu, x1);
+                double c = z * x1;
+                double s = (b * b) - 4 * c;
+                if (s < 0)
+                {
+                    z = z / 10;
+                    if (Math.Abs(z) >= 1.0e-3)
+                        continue;
+                    v1 = 0;
+                }
+                else
+                {
+                    double k = z / Math.Abs(z);
+                    v1 = (-b + k * Math.Sqrt((b * b) - 4 * c)) / 2;
+                }
+                i = i + 1;
+                A[i] = x1;
+                E[i] = v1;
+                if (i == 50)
+                {
+                    for (int n = 1; n != i; ++n)
+                        mResult.Add(new Coord(0 + (A[n] * -75), (E[n] * -75)));
+                    i = 0;
+                }
+                v0 = v1;
+                x0 = x1;
+                if (v0 == 0)
+                {
+                    if (i == 0 || mResult.Count > 1000)
+                        break;
+                    if (E[i - 1] < 0)
+                        z = d;
+                    else
+                        z = -d;
+                }
+            }
         }
 
         static double equation(double v0, double z, double mu, double x1)