From dea43e1d308de98d03db94eccb8c848ec7f8c75b Mon Sep 17 00:00:00 2001 From: Joe Walter Date: Sat, 29 May 2021 15:48:10 -0400 Subject: [PATCH] Port banner to VB --- 06 Banner/csharp/banner.cs | 18 +-- 06 Banner/vbnet/banner.sln | 25 ++++ 06 Banner/vbnet/banner.vb | 226 ++++++++++++++++++++++++++++++++++ 06 Banner/vbnet/banner.vbproj | 9 ++ 4 files changed, 269 insertions(+), 9 deletions(-) create mode 100644 06 Banner/vbnet/banner.sln create mode 100644 06 Banner/vbnet/banner.vb create mode 100644 06 Banner/vbnet/banner.vbproj diff --git a/06 Banner/csharp/banner.cs b/06 Banner/csharp/banner.cs index 6b0ff142..e3e59b4b 100644 --- a/06 Banner/csharp/banner.cs +++ b/06 Banner/csharp/banner.cs @@ -153,19 +153,19 @@ namespace banner throw new KeyNotFoundException($"The provided letter {outCh} was not found in the letters list"); } - // New int array declarations default to zeros - // numSections decides how many 'sections' need to be printed - // for a given line of each character - int[] numSections = new int[7]; - // fillInSection decides whether each 'section' of the - // character gets filled in with the character or with blanks - int[] fillInSection = new int[9]; - // This iterates through each of the parts that make up // each letter. Each part represents 1 * Horizontal lines // of actual output. for (int idx = 0; idx < 7; idx++) { + // New int array declarations default to zeros + // numSections decides how many 'sections' need to be printed + // for a given line of each character + int[] numSections = new int[7]; + // fillInSection decides whether each 'section' of the + // character gets filled in with the character or with blanks + int[] fillInSection = new int[9]; + // This uses the value in each part to decide which // sections are empty spaces in the letter or filled in // spaces. For each section marked with 1 in fillInSection, @@ -228,7 +228,7 @@ namespace banner // In the original version, it would print an additional 75 blank // lines in order to feed the printer paper...don't really need this // since we're not actually printing. - Console.WriteLine(new string('\n', 75)); + // Console.WriteLine(new string('\n', 75)); } /// diff --git a/06 Banner/vbnet/banner.sln b/06 Banner/vbnet/banner.sln new file mode 100644 index 00000000..5fdc8737 --- /dev/null +++ b/06 Banner/vbnet/banner.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31321.278 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "banner", "banner.vbproj", "{1738D297-A04C-4E6E-8219-D9E72982C39D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1738D297-A04C-4E6E-8219-D9E72982C39D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1738D297-A04C-4E6E-8219-D9E72982C39D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1738D297-A04C-4E6E-8219-D9E72982C39D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1738D297-A04C-4E6E-8219-D9E72982C39D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {04E18CE1-1C55-432F-A15A-DC2FA9DDC0F1} + EndGlobalSection +EndGlobal diff --git a/06 Banner/vbnet/banner.vb b/06 Banner/vbnet/banner.vb new file mode 100644 index 00000000..90e183b8 --- /dev/null +++ b/06 Banner/vbnet/banner.vb @@ -0,0 +1,226 @@ +Imports System + +Module Banner + Private Horizontal As Integer + Private Vertical As Integer + Private Centered As Boolean + Private Character As String + Private Statement As String + + ' This provides a bit-ended representation of each symbol + ' that can be output. Each symbol Is defined by 7 parts - + ' where each part Is an integer value that, when converted to + ' the binary representation, shows which section Is filled in + ' with values And which are spaces. i.e., the 'filled in' + ' parts represent the actual symbol on the paper. + Private letters As Dictionary(Of Char, Integer()) = New Dictionary(Of Char, Integer()) From + { + {" ", {0, 0, 0, 0, 0, 0, 0}}, + {"A", {505, 37, 35, 34, 35, 37, 505}}, + {"B", {512, 274, 274, 274, 274, 274, 239}}, + {"C", {125, 131, 258, 258, 258, 131, 69}}, + {"D", {512, 258, 258, 258, 258, 131, 125}}, + {"E", {512, 274, 274, 274, 274, 258, 258}}, + {"F", {512, 18, 18, 18, 18, 2, 2}}, + {"G", {125, 131, 258, 258, 290, 163, 101}}, + {"H", {512, 17, 17, 17, 17, 17, 512}}, + {"I", {258, 258, 258, 512, 258, 258, 258}}, + {"J", {65, 129, 257, 257, 257, 129, 128}}, + {"K", {512, 17, 17, 41, 69, 131, 258}}, + {"L", {512, 257, 257, 257, 257, 257, 257}}, + {"M", {512, 7, 13, 25, 13, 7, 512}}, + {"N", {512, 7, 9, 17, 33, 193, 512}}, + {"O", {125, 131, 258, 258, 258, 131, 125}}, + {"P", {512, 18, 18, 18, 18, 18, 15}}, + {"Q", {125, 131, 258, 258, 322, 131, 381}}, + {"R", {512, 18, 18, 50, 82, 146, 271}}, + {"S", {69, 139, 274, 274, 274, 163, 69}}, + {"T", {2, 2, 2, 512, 2, 2, 2}}, + {"U", {128, 129, 257, 257, 257, 129, 128}}, + {"V", {64, 65, 129, 257, 129, 65, 64}}, + {"W", {256, 257, 129, 65, 129, 257, 256}}, + {"X", {388, 69, 41, 17, 41, 69, 388}}, + {"Y", {8, 9, 17, 481, 17, 9, 8}}, + {"Z", {386, 322, 290, 274, 266, 262, 260}}, + {"0", {57, 69, 131, 258, 131, 69, 57}}, + {"1", {0, 0, 261, 259, 512, 257, 257}}, + {"2", {261, 387, 322, 290, 274, 267, 261}}, + {"3", {66, 130, 258, 274, 266, 150, 100}}, + {"4", {33, 49, 41, 37, 35, 512, 33}}, + {"5", {160, 274, 274, 274, 274, 274, 226}}, + {"6", {194, 291, 293, 297, 305, 289, 193}}, + {"7", {258, 130, 66, 34, 18, 10, 8}}, + {"8", {69, 171, 274, 274, 274, 171, 69}}, + {"9", {263, 138, 74, 42, 26, 10, 7}}, + {"?", {5, 3, 2, 354, 18, 11, 5}}, + {"*", {69, 41, 17, 512, 17, 41, 69}}, + {"=", {41, 41, 41, 41, 41, 41, 41}}, + {"!", {1, 1, 1, 384, 1, 1, 1}}, + {".", {1, 1, 129, 449, 129, 1, 1}} + } + + ' + ' This displays the provided text on the screen And then waits for the user + ' to enter a integer value greater than 0. + ' + ' Text to display on the screen asking for the input + ' The integer value entered by the user + Private Function GetNumber(DisplayText As String) As Integer + Console.Write(DisplayText) + Dim TempStr As String = Console.ReadLine() + Dim TempInt As Integer + + Int32.TryParse(TempStr, TempInt) + + If (TempInt <= 0) Then + Throw New ArgumentException($"{DisplayText} must be greater than zero") + End If + + Return TempInt + End Function + + ' + ' This displays the provided text on the screen And then waits for the user + ' to enter a Y Or N. It cheats by just looking for a 'y' and returning that + ' as true. Anything else that the user enters Is returned as false. + ' + ' Text to display on the screen asking for the input + ' Returns true Or false + Private Function GetBool(DisplayText As String) As Boolean + Console.Write(DisplayText) + Return Console.ReadLine().StartsWith("y", StringComparison.InvariantCultureIgnoreCase) + End Function + + ' + ' This displays the provided text on the screen And then waits for the user + ' to enter an arbitrary string. That string Is then returned 'as-is'. + ' + ' Text to display on the screen asking for the input + ' The string entered by the user. + Private Function GetString(DisplayText As String) As String + Console.Write(DisplayText) + Return (Console.ReadLine().ToUpper()) + End Function + + ' + ' This queries the user for the various inputs needed by the program. + ' + Private Sub GetInput() + Horizontal = GetNumber("Horizontal ") + Vertical = GetNumber("Vertical ") + Centered = GetBool("Centered ") + Character = GetString("Character (type 'ALL' if you want character being printed) ") + Statement = GetString("Statement ") + ' We don't care about what the user enters here. This is just telling them + ' to set the page in the printer. + GetString("Set page ") + End Sub + + ' + ' This prints out a single character of the banner - adding + ' a few blanks lines as a spacer between characters. + ' + Private Sub PrintChar(ch As Char) + ' In the trivial case (a space character), just print out the spaces + If ch.Equals(" ") Then + Console.WriteLine(New String("\n", 7 * Horizontal)) + Return + End If + + ' If a specific character to be printed was provided by the user, + ' then user that as our ouput character - otherwise take the + ' current character + Dim outCh As Char = IIf(Character.Equals("ALL"), ch, Character.Substring(0, 1)) + Dim letter(7) As Integer + Try + letters(outCh).CopyTo(letter, 0) + Catch ex As KeyNotFoundException + Throw New KeyNotFoundException($"The provided letter {outCh} was not found in the letters list") + End Try + + ' This iterates through each of the parts that make up + ' each letter. Each part represents 1 * Horizontal lines + ' of actual output. + For idx As Integer = 0 To 7 + ' New int array declarations default to zeros + ' numSections decides how many 'sections' need to be printed + ' for a given line of each character + Dim numSections(7) As Integer + ' fillInSection decides whether each 'section' of the + ' character gets filled in with the character Or with blanks + Dim fillInSection(9) As Integer + + ' This uses the value in each part to decide which + ' sections are empty spaces in the letter Or filled in + ' spaces. For each section marked with 1 in fillInSection, + ' that will correspond to 1 * Vertical characters actually + ' being output. + For exp As Integer = 8 To 0 Step -1 + If (Math.Pow(2, exp) < letter(idx)) Then + fillInSection(8 - exp) = 1 + letter(idx) -= Math.Pow(2, exp) + If (letter(idx) = 1) Then + ' Once we've exhausted all of the sections + ' defined in this part of the letter, then + ' we marked that number And break out of this + ' for loop. + numSections(idx) = 8 - exp + Exit For + End If + End If + Next exp + + ' Now that we know which sections of this part of the letter + ' are filled in Or spaces, we can actually create the string + ' to print out. + Dim lineStr As String = "" + + If (Centered) Then + lineStr += New String(" ", (63 - 4.5 * Vertical) * 1 / 1 + 1) + End If + + For idx2 As Integer = 0 To numSections(idx) + lineStr = lineStr + New String(IIf(fillInSection(idx2) = 0, " ", outCh), Vertical) + Next idx2 + + ' Then we print that string out 1 * Horizontal number of times + For lineIdx As Integer = 1 To Horizontal + Console.WriteLine(lineStr) + Next lineIdx + Next idx + + + ' Finally, add a little spacer after each character for readability. + Console.WriteLine(New String(Environment.NewLine, 2 * Horizontal - 1)) + End Sub + + ' + ' This prints the entire banner based in the parameters + ' the user provided. + ' + Private Sub PrintBanner() + ' Iterate through each character in the statement + For Each ch As Char In Statement + PrintChar(ch) + Next ch + + ' In the original version, it would print an additional 75 blank + ' lines in order to feed the printer paper...don't really need this + ' since we're not actually printing. + Console.WriteLine(New String(Environment.NewLine, 75)) + End Sub + + ' + ' Main entry point into the banner class And handles the main loop. + ' + Public Sub Play() + GetInput() + PrintBanner() + End Sub +End Module + +Module Program + Sub Main(args As String()) + Banner.Play() + End Sub +End Module diff --git a/06 Banner/vbnet/banner.vbproj b/06 Banner/vbnet/banner.vbproj new file mode 100644 index 00000000..659fea7b --- /dev/null +++ b/06 Banner/vbnet/banner.vbproj @@ -0,0 +1,9 @@ + + + + Exe + banner + netcoreapp3.1 + + +