forked from markjprice/cs11dotnet7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter04.dib
245 lines (194 loc) · 3.93 KB
/
Chapter04.dib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!markdown
# Chapter 4 - Writing, Debugging, and Testing Functions
#!csharp
// allow simplified WriteLine and other console method calls
using static System.Console;
#!markdown
## Writing a times table function
#!csharp
static void TimesTable(byte number)
{
WriteLine($"This is the {number} times table:");
for (int row = 1; row <= 12; row++)
{
WriteLine($"{row} x {number} = {row * number}");
}
WriteLine();
}
#!csharp
TimesTable(6) // semicolon is optional in a .NET notebook
#!csharp
TimesTable(number: 87) // optional to specify the parameter name
#!markdown
## Writing a function that returns a value
#!csharp
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;
}
#!csharp
decimal taxToPay = CalculateTax(149, "FR");
WriteLine($"You must pay {taxToPay} in tax.");
#!markdown
## Converting numbers from cardinal to ordinal
#!csharp
/// <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}";
}
}
#!csharp
static void RunCardinalToOrdinal()
{
for (int number = 1; number <= 40; number++)
{
Write($"{CardinalToOrdinal(number)} ");
}
WriteLine();
}
#!csharp
RunCardinalToOrdinal()
#!markdown
## Calculating factorials with recursion
#!csharp
static int Factorial(int number)
{
if (number < 1)
{
return 0;
}
else if (number == 1)
{
return 1;
}
else
{
checked // for overflow
{
return number * Factorial(number - 1);
}
}
}
#!csharp
static void RunFactorial()
{
for (int i = 1; i < 15; i++)
{
try
{
WriteLine($"{i}! = {Factorial(i):N0}");
}
catch (System.OverflowException)
{
WriteLine($"{i}! is too big for a 32-bit integer.");
}
}
}
#!csharp
RunFactorial()
#!markdown
## Using lambdas in function implementations
#!csharp
static int FibImperative(int term)
{
if (term == 1)
{
return 0;
}
else if (term == 2)
{
return 1;
}
else
{
return FibImperative(term - 1) + FibImperative(term - 2);
}
}
#!csharp
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));
}
}
#!csharp
RunFibImperative()
#!csharp
static int FibFunctional(int term) =>
term switch
{
1 => 0,
2 => 1,
_ => FibFunctional(term - 1) + FibFunctional(term - 2)
};
#!csharp
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));
}
}
#!csharp
RunFibFunctional()