Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace OkePreparing
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Hello World!");
- //Console.Write(typeof(string).Assembly.ImageRuntimeVersion);
- var fibbMethod = GetFibbonaciMethod(FibboObject.Normal);
- var result = fibbMethod.CountFibbonaci(10);
- var recursiveMethod = GetFibbonaciMethod(FibboObject.Recursive);
- var recursiveResult = recursiveMethod.CountFibbonaci(10);
- var tailResusive = GetFibbonaciMethod(FibboObject.TailRecursive);
- var tailRecursiveResult = tailResusive.CountFibbonaci(10);
- Console.WriteLine($"{result} for 10 - normal");
- Console.WriteLine($"{recursiveResult} for 10 - recursive");
- Console.WriteLine($"{tailRecursiveResult} for 10 - tail recursive");
- Console.Read();
- }
- public static IFibbonaciFactory GetFibbonaciMethod(FibboObject obj)
- {
- IFibbonaciFactory fibb = null;
- switch (obj)
- {
- case FibboObject.Normal:
- fibb = new NormalFibbonaci();
- break;
- case FibboObject.Recursive:
- fibb = new RecursiveFibbonaci();
- break;
- case FibboObject.TailRecursive:
- fibb = new TailRecursiveFibbonaci();
- break;
- }
- return fibb;
- }
- }
- public interface IFibbonaciFactory
- {
- int CountFibbonaci(int n);
- }
- public class NormalFibbonaci : IFibbonaciFactory
- {
- public int CountFibbonaci(int n)
- {
- if (n <= 1)
- {
- return 1;
- }
- var fibbArray = new int[n];
- fibbArray[0] = 1;
- fibbArray[1] = 1;
- for (int i = 2; i < n; i++)
- {
- fibbArray[i] = fibbArray[i - 1] + fibbArray[i - 2];
- }
- return fibbArray[n - 1];
- }
- }
- public class RecursiveFibbonaci : IFibbonaciFactory
- {
- public int CountFibbonaci(int n)
- {
- if (n == 0)
- {
- return 0;
- }
- if (n == 1)
- {
- return 1;
- }
- return CountFibbonaci(n - 1) + CountFibbonaci(n - 2);
- }
- }
- public class TailRecursiveFibbonaci : IFibbonaciFactory
- {
- public int CountFibbonaci(int n)
- {
- return CountTailRecursiveFibb(n, 0, 1);
- }
- private int CountTailRecursiveFibb(int n, int e1, int e2)
- {
- if (n == 0)
- {
- return e1;
- }
- if (n == 1)
- {
- return e2;
- }
- return CountTailRecursiveFibb(n - 1, e2, e1 + e2);
- }
- }
- public enum FibboObject
- {
- Normal,
- Recursive,
- TailRecursive
- }
- //public interface INameFactory
- //{
- // void ShowName()
- // {
- // Console.WriteLine("name");
- // }
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement