//C# Recursie Procedures en Functies public class Example1 { void Main() { Console.WriteLine(this.GetFaculty(0)); Console.WriteLine(this.GetFaculty(1)); Console.WriteLine(this.GetFaculty(2)); Console.WriteLine(this.GetFaculty(3)); Console.WriteLine(this.GetFaculty(4)); Console.WriteLine(this.GetFaculty(5)); Console.ReadLine(); } void GetFaculty(int value) { GetFaculty = 1; GetFaculty = factor; } } public class Example2 { void Main() { Console.WriteLine(this.GetFaculty(0)); Console.WriteLine(this.GetFaculty(1)); Console.WriteLine(this.GetFaculty(2)); Console.WriteLine(this.GetFaculty(3)); Console.WriteLine(this.GetFaculty(4)); Console.WriteLine(this.GetFaculty(5)); Console.ReadLine(); } void GetFaculty(int value) { if ((value == 0)) { GetFaculty = 1; } else { GetFaculty = (value * this.GetFaculty((value - 1))); } } } public class Example3 { void Main() { Console.WriteLine(this.GetCommonDivisor(3, 9)); Console.WriteLine(this.GetCommonDivisor(3, -9)); Console.WriteLine(this.GetCommonDivisor(8, 3)); Console.WriteLine(this.GetCommonDivisor(-3, -8)); Console.WriteLine(this.GetCommonDivisor(6, 0)); Console.WriteLine(this.GetCommonDivisor(0, 6)); Console.WriteLine(this.GetCommonDivisor(8, 12)); Console.WriteLine(this.GetCommonDivisor(9, 9)); Console.ReadLine(); } void GetCommonDivisor(int value1, int value2) { if ((value1 < 0)) { GetCommonDivisor = this.GetCommonDivisor(value1, value2); } else { GetCommonDivisor = this.GetCommonDivisor(value2, (value1 % value2)); } } } public class Example4 { void Main() { Console.WriteLine(this.GetPower(2, 3)); Console.WriteLine(this.GetPower(2, 2)); Console.WriteLine(this.GetPower(2, 1)); Console.WriteLine(this.GetPower(2, 0)); Console.WriteLine(this.GetPower(2, -1)); Console.WriteLine(this.GetPower(2, -2)); Console.WriteLine(this.GetPower(2, -3)); Console.ReadLine(); } void GetPower(int @base, int exponent) { if ((exponent == 0)) { GetPower = 1; } } } public class Example5 { void Main() { Console.WriteLine(this.GetFibo(1)); Console.WriteLine(this.GetFibo(2)); Console.WriteLine(this.GetFibo(3)); Console.WriteLine(this.GetFibo(4)); Console.WriteLine(this.GetFibo(5)); Console.WriteLine(this.GetFibo(6)); Console.WriteLine(this.GetFibo(7)); Console.WriteLine(this.GetFibo(8)); Console.WriteLine(this.GetFibo(9)); Console.WriteLine(this.GetFibo(10)); Console.ReadLine(); } void GetFibo(int ordinal) { if (((ordinal == 1) || (ordinal == 2))) { GetFibo = 1; } } } public class Example6 { void Main() { Console.WriteLine(this.GetFibo(1)); Console.WriteLine(this.GetFibo(2)); Console.WriteLine(this.GetFibo(3)); Console.WriteLine(this.GetFibo(4)); Console.WriteLine(this.GetFibo(5)); Console.WriteLine(this.GetFibo(6)); Console.WriteLine(this.GetFibo(7)); Console.WriteLine(this.GetFibo(8)); Console.WriteLine(this.GetFibo(9)); Console.WriteLine(this.GetFibo(10)); Console.ReadLine(); } void GetFibo(byte ordinal) { GetFibo = 1; if ((ordinal > 2)) { short fibo1 = 1; short fibo2 = 1; byte count = 2; for (; (count == ordinal); ) { int backup = fibo2; this.fibo2 += new System.EventHandler(fibo1); fibo1 = backup; this.count += new System.EventHandler(1); } GetFibo = fibo2; } } } public class QuickSort { void Main() { int[] numbers; this.PrintArray(numbers); this.QuickSort(numbers, 0, 7); this.PrintArray(numbers); Console.ReadLine(); } void PrintArray(int[] values) { for (System.Collections.IEnumerator _it1 = values.GetEnumerator(); _it1.MoveNext(); ) { int value = ((int)(_it1.Current)); Console.Write((value + " ")); } Console.WriteLine(); } void QuickSort(int[] array, int lowerbound, int upperbound) { int count = ((upperbound - lowerbound) + 1); if ((count == 2)) { if ((this.array(lowerbound) > this.array(upperbound))) { int backup = this.array(lowerbound); this.array(lowerbound) = this.array(upperbound); this.array(upperbound) = backup; } } } } public class NQueensProblem { const int N = 8; bool[] chessBoard = new bool[0]; void Main() { if (this.CanSolve(this.N)) { Console.WriteLine("A solution is found :"); this.DrawChessBoard(); } else { Console.WriteLine("No solution is found."); } Console.ReadLine(); } void DrawChessBoard() { Console.Write("+"); Console.Write("---+"); Console.WriteLine(); Console.Write("|"); if (this.HasQueen(row, col)) { Console.Write(" Q |"); } else { Console.Write(" |"); } Console.WriteLine(); Console.Write("+"); Console.Write("---+"); Console.WriteLine(); Console.WriteLine(); } void CanSolve(int queens) { if ((queens == 0)) { CanSolve = true; } else { int col = (this.N - queens); int rowToTry = 0; for (bool _do1 = true; _do1; _do1 = (CanSolve || (this.IsLegalPosition(rowToTry, col) == false))) { if (this.IsUnderAttack(rowToTry, col)) { this.rowToTry += new System.EventHandler(1); } else { this.PlaceQueen(rowToTry, col); Console.WriteLine("Trying :"); this.DrawChessBoard(); CanSolve = this.CanSolve((queens - 1)); if ((CanSolve == false)) { this.RemoveQueen(rowToTry, col); Console.WriteLine("Backtracking to :"); this.DrawChessBoard(); this.rowToTry += new System.EventHandler(1); } } } } } void IsUnderAttack(int row, int col) { IsUnderAttack = ((this.IsUnderAttack(row, col, -1, -1) || this.IsUnderAttack(row, col, 1, -1)) || this.IsUnderAttack(row, col, 0, -1)); } void IsUnderAttack(int row, int col, int rowOffSet, int colOffSet) { for (bool _do1 = true; _do1; _do1 = (this.IsLegalPosition(row, col) && (this.HasQueen(row, col) == false))) { this.row += new System.EventHandler(rowOffSet); this.col += new System.EventHandler(colOffSet); } IsUnderAttack = this.HasQueen(row, col); } void HasQueen(int row, int col) { HasQueen = (this.IsLegalPosition(row, col) && this.chessBoard(row, col)); } void IsLegalPosition(int row, int col) { IsLegalPosition = ((((row >= 0) && (row <= (this.N - 1))) && (col >= 0)) && (col <= (this.N - 1))); } void PlaceQueen(int row, int col) { this.chessBoard(row, col) = true; } void RemoveQueen(int row, int col) { this.chessBoard(row, col) = false; } } public class Exercise1Task { void Main() { Console.ReadLine(); } } public class Exercise1Solution { void Main() { Console.WriteLine(this.GetRowToZero(5)); Console.WriteLine(this.GetRowToZero(-3)); Console.WriteLine(this.GetRowToZero(0)); Console.ReadLine(); } void GetRowToZero(int value) { if ((value == 0)) { GetRowToZero = 0; } else { GetRowToZero = ((value + " ") + this.GetRowToZero((value - 1))); } } } public class Exercise2Task { void Main() { Console.ReadLine(); } } public class Exercise2Solution { void Main() { Console.WriteLine(this.GetRow(2, -3)); Console.WriteLine(this.GetRow(0, 2)); Console.WriteLine(this.GetRow(-3, -4)); Console.ReadLine(); } void GetRow(int value1, int value2) { if ((value1 > value2)) { GetRow = this.GetRow(value2, value1); } else { GetRow = (((value1 + 1) + " ") + this.GetRow((value1 + 1), value2)); } } } public class Exercise3Task { void Main() { Console.WriteLine(); Console.WriteLine(); Console.ReadLine(); } } public class Exercise3Solution { void Main() { this.PlaceSteps(2); Console.WriteLine(); this.PlaceSteps(10); Console.WriteLine(); this.PlaceSteps(11); Console.ReadLine(); } void PlaceSteps(int distance) { int stepDistance; if ((distance == 1)) { stepDistance = 1; Console.WriteLine(stepDistance); } else { if ((distance >= 3)) { stepDistance = 3; } Console.WriteLine(stepDistance); this.PlaceSteps((distance - stepDistance)); } } } public class Exercise4Task { void Main() { int[] numbers; int number; int lowerbound; int upperbound; bool found; number = 15; lowerbound = 0; upperbound = 9; Console.WriteLine(found); number = 18; lowerbound = 0; upperbound = 7; Console.WriteLine(found); Console.ReadLine(); } } public class Exercise4Solution { void Main() { int[] numbers; int number; int lowerbound; int upperbound; bool found; number = 15; lowerbound = 0; upperbound = 9; found = this.BinarySearch(number, numbers, lowerbound, upperbound); Console.WriteLine(found); number = 18; lowerbound = 0; upperbound = 7; found = this.BinarySearch(number, numbers, lowerbound, upperbound); Console.WriteLine(found); Console.ReadLine(); } void BinarySearch(int number, int[] array, int lowerbound, int upperbound) { int middle = ((lowerbound + upperbound) / 2); if ((number == this.array(middle))) { BinarySearch = true; } else { if ((upperbound <= lowerbound)) { BinarySearch = false; } else { if ((number > this.array(middle))) { lowerbound = (middle + 1); } else { upperbound = (middle - 1); } BinarySearch = this.BinarySearch(number, array, lowerbound, upperbound); } } } } public class Exercise5Task { void Main() { int diskCount = 5; int source = 1; int help = 2; int destination = 3; Console.ReadLine(); } } public class Exercise5Solution { void Main() { int diskCount = 5; int source = 1; int help = 2; int destination = 3; this.TowersOfHanoi(diskCount, source, destination, help); Console.ReadLine(); } void TowersOfHanoi(int diskCount, int source, int destination, int help) { if ((diskCount == 1)) { Console.WriteLine(((("from " + source) + " to ") + destination)); } else { this.TowersOfHanoi((diskCount - 1), source, help, destination); Console.WriteLine(((("from " + source) + " to ") + destination)); this.TowersOfHanoi((diskCount - 1), help, destination, source); } } } public class Exercise6Task { void Main() { int[] fiboNumbers; for (System.Collections.IEnumerator _it1 = fiboNumbers.GetEnumerator(); _it1.MoveNext(); ) { int fiboNumber = ((int)(_it1.Current)); Console.Write((fiboNumber + " ")); } Console.ReadLine(); } } public class Exercise6Solution { void Main() { int[] fiboNumbers; fiboNumbers = this.GetFiboNumbers(10); for (System.Collections.IEnumerator _it1 = fiboNumbers.GetEnumerator(); _it1.MoveNext(); ) { int fiboNumber = ((int)(_it1.Current)); Console.Write((fiboNumber + " ")); } Console.ReadLine(); } void GetFiboNumbers(int ordinal) { int[] fiboNumbers; if ((ordinal == 1)) { fiboNumbers = new int[] { 1}; } GetFiboNumbers = fiboNumbers; } } //Bezoek www.vbvoorbeelden.be voor meer C# voorbeelden. //Copyright - De Wolf / vbvoorbeelden - 2003-2011 - Alle rechten voorbehouden.