From: Pat Thoyts Date: Tue, 5 Aug 2014 14:54:11 +0000 (+0100) Subject: Refactored into more modern coding style. X-Git-Url: http://privyetmir.co.uk/gitweb?a=commitdiff_plain;h=753dd079c2d0b7b55ef959ceb47177bab54d9c6b;p=MervsSolver Refactored into more modern coding style. Eliminated all the goto statements while maintaining passing the regression test. Signed-off-by: Pat Thoyts --- diff --git a/MervsSolver/Program.cs b/MervsSolver/Program.cs index 09156eb..d7ba69f 100644 --- a/MervsSolver/Program.cs +++ b/MervsSolver/Program.cs @@ -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(); diff --git a/MervsSolver/Solver.cs b/MervsSolver/Solver.cs index 9f5db28..73a70cc 100644 --- a/MervsSolver/Solver.cs +++ b/MervsSolver/Solver.cs @@ -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)