/* * Aresh Saharkhiz * saharkiz@gmail.com * Associate Professor / Mapua Institute of Technology / Philippines */ using System; // Introduction to Software Testing // Authors: Paul Ammann & Jeff Offutt // Chapter 2, section 2.4, page 71 // Program to compute the quadratic root for two numbers internal class Quadratic { private static double Root1, Root2; public static void Main(string[] argv) { int X, Y, Z; bool ok; if (argv.Length == 3) { try { // The book has a typo in this example, // argv[] must start at 0, not 1. // This version has been corrected. X = Convert.ToInt32(argv[0]); Y = Convert.ToInt32(argv[1]); Z = Convert.ToInt32(argv[2]); } catch (FormatException) { Console.WriteLine("Inputs not three integers, using 8, 10, -33."); X = 8; Y = 10; Z = -33; } } else { Console.WriteLine("Inputs not three integers, using 8, 10, -33."); X = 8; Y = 10; Z = -33; } ok = Root(X, Y, Z); if (ok) { Console.WriteLine("Quadratic: Root 1 = " + Root1 + ", Root 2 = " + Root2); } else { Console.WriteLine("No solution."); } } // Finds the quadratic root, A must be non-zero private static bool Root(int A, int B, int C) { double D; bool Result; D = (double)(B * B) - (double)(4.0 * A * C); if (D < 0.0) { Result = false; return (Result); } Root1 = (double)((-B + Math.Sqrt(D)) / (2.0 * A)); Root2 = (double)((-B - Math.Sqrt(D)) / (2.0 * A)); Result = true; return (Result); } // End method Root } // End class Quadratic