mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-04 22:07:39 +00:00
Initial commit
This commit is contained in:
parent
e523533d17
commit
10cceacca6
50 changed files with 1280 additions and 0 deletions
170
vscode/Chapter04/WritingFunctions/Program.Functions.cs
Normal file
170
vscode/Chapter04/WritingFunctions/Program.Functions.cs
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
partial class Program
|
||||
{
|
||||
static void TimesTable(byte number, byte size = 12)
|
||||
{
|
||||
WriteLine($"This is the {number} times table with {size} rows:");
|
||||
for (int row = 1; row <= size; row++)
|
||||
{
|
||||
WriteLine($"{row} x {number} = {row * number}");
|
||||
}
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
static decimal CalculateTax(
|
||||
decimal amount, string twoLetterRegionCode)
|
||||
{
|
||||
decimal rate = 0.0M;
|
||||
|
||||
switch (twoLetterRegionCode)
|
||||
{
|
||||
case "CH": // Switzerland
|
||||
rate = 0.08M;
|
||||
break;
|
||||
case "DK": // Denmark
|
||||
case "NO": // Norway
|
||||
rate = 0.25M;
|
||||
break;
|
||||
case "GB": // United Kingdom
|
||||
case "FR": // France
|
||||
rate = 0.2M;
|
||||
break;
|
||||
case "HU": // Hungary
|
||||
rate = 0.27M;
|
||||
break;
|
||||
case "OR": // Oregon
|
||||
case "AK": // Alaska
|
||||
case "MT": // Montana
|
||||
rate = 0.0M;
|
||||
break;
|
||||
case "ND": // North Dakota
|
||||
case "WI": // Wisconsin
|
||||
case "ME": // Maine
|
||||
case "VA": // Virginia
|
||||
rate = 0.05M;
|
||||
break;
|
||||
case "CA": // California
|
||||
rate = 0.0825M;
|
||||
break;
|
||||
default: // most US states
|
||||
rate = 0.06M;
|
||||
break;
|
||||
}
|
||||
return amount * rate;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pass a 32-bit integer and it will be converted into its ordinal equivalent.
|
||||
/// </summary>
|
||||
/// <param name="number">Number is a cardinal value e.g. 1, 2, 3, and so on.</param>
|
||||
/// <returns>Number as an ordinal value e.g. 1st, 2nd, 3rd, and so on.</returns>
|
||||
|
||||
static string CardinalToOrdinal(int number)
|
||||
{
|
||||
switch (number)
|
||||
{
|
||||
case 11: // special cases for 11th to 13th
|
||||
case 12:
|
||||
case 13:
|
||||
return $"{number}th";
|
||||
default:
|
||||
int lastDigit = number % 10;
|
||||
string suffix = lastDigit switch
|
||||
{
|
||||
1 => "st",
|
||||
2 => "nd",
|
||||
3 => "rd",
|
||||
_ => "th"
|
||||
};
|
||||
return $"{number}{suffix}";
|
||||
}
|
||||
}
|
||||
|
||||
static void RunCardinalToOrdinal()
|
||||
{
|
||||
for (int number = 1; number <= 40; number++)
|
||||
{
|
||||
Write($"{CardinalToOrdinal(number)} ");
|
||||
}
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
static int Factorial(int number)
|
||||
{
|
||||
if (number < 0)
|
||||
{
|
||||
throw new ArgumentException(message:
|
||||
"The factorial function is defined for non-negative integers only.",
|
||||
paramName: "number");
|
||||
}
|
||||
else if (number == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
checked // for overflow
|
||||
{
|
||||
return number * Factorial(number - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void RunFactorial()
|
||||
{
|
||||
for (int i = 1; i <= 14; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
WriteLine($"{i}! = {Factorial(i):N0}");
|
||||
}
|
||||
catch (System.OverflowException)
|
||||
{
|
||||
WriteLine($"{i}! is too big for a 32-bit integer.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int FibImperative(int term)
|
||||
{
|
||||
if (term == 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (term == 2)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FibImperative(term - 1) + FibImperative(term - 2);
|
||||
}
|
||||
}
|
||||
static void RunFibImperative()
|
||||
{
|
||||
for (int i = 1; i <= 30; i++)
|
||||
{
|
||||
WriteLine("The {0} term of the Fibonacci sequence is {1:N0}.",
|
||||
arg0: CardinalToOrdinal(i),
|
||||
arg1: FibImperative(term: i));
|
||||
}
|
||||
}
|
||||
|
||||
static int FibFunctional(int term) =>
|
||||
term switch
|
||||
{
|
||||
1 => 0,
|
||||
2 => 1,
|
||||
_ => FibFunctional(term - 1) + FibFunctional(term - 2)
|
||||
};
|
||||
|
||||
static void RunFibFunctional()
|
||||
{
|
||||
for (int i = 1; i <= 30; i++)
|
||||
{
|
||||
WriteLine("The {0} term of the Fibonacci sequence is {1:N0}.",
|
||||
arg0: CardinalToOrdinal(i),
|
||||
arg1: FibFunctional(term: i));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
20
vscode/Chapter04/WritingFunctions/Program.cs
Normal file
20
vscode/Chapter04/WritingFunctions/Program.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
TimesTable(7);
|
||||
|
||||
TimesTable(7, 20);
|
||||
|
||||
TimesTable(number: 7, size: 10);
|
||||
*/
|
||||
|
||||
/*
|
||||
decimal taxToPay = CalculateTax(amount: 149, twoLetterRegionCode: "FR");
|
||||
WriteLine($"You must pay {taxToPay} in tax.");
|
||||
*/
|
||||
|
||||
//RunCardinalToOrdinal();
|
||||
|
||||
//RunFactorial();
|
||||
|
||||
//RunFibImperative();
|
||||
|
||||
RunFibFunctional();
|
||||
14
vscode/Chapter04/WritingFunctions/WritingFunctions.csproj
Normal file
14
vscode/Chapter04/WritingFunctions/WritingFunctions.csproj
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue