Cerebrus

Structs instead of Classes

Mar 29th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. //Related to Question at:
  2. //http://groups.google.com/group/dotnetdevelopment/browse_thread/thread/671641a749256799/
  3.  
  4. public struct EquationSolution
  5. {
  6.   private double[] solns;
  7.  
  8.  
  9.   public EquationSolution(double[] solutions)
  10.   {
  11.     solns = (double[])solutions.Clone();
  12.   }
  13.  
  14.  
  15.   public int NumberOfSolutions
  16.   {
  17.     get { return solns.Length; }
  18.   }
  19.  
  20.  
  21.   public double this[int n]
  22.   {
  23.     get { return solns[n]; }
  24.   }
  25.  
  26.  
  27.  
  28. }
  29.  
  30.  
  31. public struct Equation
  32. {
  33.   private double a, b, c;
  34.  
  35.   public Equation(double a, double b, double c)
  36.   {
  37.     this.a = a; this.b = b; this.c = c;
  38.   }
  39.  
  40.  
  41.   public EquationSolution Solve()
  42.   {
  43.     double[] solutions;
  44.     if (a == 0)
  45.       solutions = new double[] { (-c / b) };
  46.  
  47.  
  48.     // Get the determinant.
  49.     double d = (b * b) - 4 * a * c;
  50.  
  51.  
  52.     if (d < 0)
  53.       solutions = new double[0];  // No real solution.
  54.     else if (d == 0)
  55.       solutions = new double[1] { (-b / (2 * a)) };  // ONE real
  56. solution.
  57.     else // if (d > 0)
  58.       solutions = new double[2] { ((-b + Math.Sqrt(d)) / (2 * a)), ((-
  59. b - Math.Sqrt(d)) / (2 * a)) };  // TWO real solutions.
  60.  
  61.  
  62.     return new EquationSolution(solutions);
  63.   }
  64.  
  65.  
  66.  
  67. }
  68.  
  69.  
  70. static void Main(string[] args)
  71. {
  72.   string strEndProgram;
  73.   Console.WriteLine("This program solves equation of the form [ax²+bx
  74. +c=0]");
  75.   bool endProgram = false;
  76.   do
  77.   {
  78.     Console.Write("Please enter the value of a:");
  79.     double a = double.Parse(Console.ReadLine());
  80.     Console.Write("Please enter the value of b:");
  81.     double b = double.Parse(Console.ReadLine());
  82.     Console.Write("Please enter the value of c:");
  83.     double c = double.Parse(Console.ReadLine());
  84.  
  85.     Equation eq = new Equation(a, b, c);
  86.     EquationSolution solutions = eq.Solve();
  87.     switch(solutions.NumberOfSolutions)
  88.     {
  89.       case 0:
  90.         System.Console.WriteLine("No Solution");
  91.         break;
  92.       case 1:
  93.         Console.Out.WriteLine("The Solution is: {0:F1}", solutions
  94. [0]);
  95.         break;
  96.       case 2:
  97.         Console.Out.WriteLine("The Solutions are: {0:F1}, {1:F1}",
  98. solutions[0], solutions[1]);
  99.         break;
  100.     }
  101.     Console.Out.Write("Solve another equation? (y/n)");
  102.     strEndProgram = Console.ReadLine();
  103.  
  104.  
  105.     if(strEndProgram.StartsWith("n"))
  106.       endProgram = true;
  107.   } while(!endProgram);
  108.  
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment