/* * Aresh Saharkhiz * saharkiz@gmail.com * Associate Professor / Mapua Institute of Technology / Philippines */ using System; // Introduction to Software Testing // Authors: Paul Ammann & Jeff Offutt // Chapter 5, section 5.2, page 191 internal class power_class { public static int power(int left, int right) { //************************************** // Raises Left to the power of Right // precondition : Right >= 0 // postcondition: Returns Left**Right //************************************** int rslt; rslt = left; if (right == 0) { rslt = 1; } else { for (int i = 2; i <= right; i++) { rslt = rslt * left; } } return (rslt); } public static void Main(string[] argv) { // Driver method for power // Read two integers from standard input, call power() int[] inArr = new int[argv.Length]; if (argv.Length != 2) { Console.WriteLine("Usage: java power v1 v2"); return; } for (int i = 0; i < argv.Length; i++) { try { inArr[i] = Convert.ToInt32(argv[i]); } catch (FormatException) { Console.WriteLine("Entry must be a integer, using 1."); inArr[i] = 1; } } if (inArr[1] < 0) { Console.WriteLine("Right must be large or equal to 0, using 1."); inArr[1] = 1; } Console.WriteLine("The result is: " + power(inArr[0], inArr[1])); } }