Skip to content

Commit

Permalink
Add algorithm & shared method examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubKorytko committed Nov 15, 2023
1 parent 160e7fd commit f46a821
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions AlgorithmsLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Compile Include="Components\AlgorithmsCore\BaseClass.cs" />
<Compile Include="Components\AlgorithmsCore\BaseClassTools.cs" />
<Compile Include="Components\Algorithms\Eratosthenes.cs" />
<Compile Include="Components\Algorithms\Algorithm.example.cs" />
<Compile Include="Components\Algorithms\Factorial.cs" />
<Compile Include="Components\Algorithms\Fibonacci.cs" />
<Compile Include="Components\Algorithms\Hanoi.cs" />
Expand Down
39 changes: 39 additions & 0 deletions Components/Algorithms/Algorithm.example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;

// include this
using AlgorithmsLibrary.AlgorithmsCore;

namespace AlgorithmsLibrary.Algorithms
{

// derive from the "Algorithm" class
internal class SuperAlgorithm : Algorithm
{
// private field, you can add as many as you want
private string exclamationMark;

// override description to be displayed in the menu
// (optional)
public override string Description { get { return "I have the description!"; } }

// private method, you can add as many as you want
private string GetMessage(string text)
{
return "Im the algorithm! " +
text +
exclamationMark +
" 1 + 1 = " +
// method defined in the AlgorithmsCore/BaseClassTools.cs
OnePlusOne();
}

// ovverride Display method to handle algorithm output
// (optional, but required for functionality)
public override void Display()
{
exclamationMark = "!";

Console.Write(GetMessage("Hooray"));
}
}
}
7 changes: 7 additions & 0 deletions Components/AlgorithmsCore/BaseClassTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,12 @@ protected bool IntParseTestWithOutput(string num)
return false;
}
}

// example of the method to use across algorithms
// you can use any return type or method name
protected int OnePlusOne()
{
return 1 + 1;
}
}
}

0 comments on commit f46a821

Please sign in to comment.