Daily Archives: November 2, 2020
VB.NET || How To Get First/Last Day Of The Week And The First/Last Day Of The Month
The following is a module with functions which demonstrates how to get the first and last day of the week, as well as how to get the first and last day of the month using VB.NET.
1. First & Last Day Of Week
The example below demonstrates the use of ‘Utils.DateRange.GetWeekStartAndEnd‘ to get the first and last day of the week.
By default, the start of the week is set on Monday. This can be changed by setting the second parameter to any valid property contained in the Microsoft.VisualBasic.FirstDayOfWeek enum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
' First & Last Day Of Week Dim weekResult = Utils.DateRange.GetWeekStartAndEnd(Date.Today) Debug.Print($" Today = {Date.Today.ToShortDateString} Week Start = {weekResult.StartDate.ToShortDateString} Week End = {weekResult.EndDate.ToShortDateString} ") ' expected output: ' Today = 11/2/2020 ' Week Start = 11/2/2020 ' Week End = 11/8/2020 |
2. First & Last Day Of Month
The example below demonstrates the use of ‘Utils.DateRange.GetMonthStartAndEnd‘ to get the first and last day of the week.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
' First & Last Day Of Month Dim monthResult = Utils.DateRange.GetMonthStartAndEnd(Date.Today) Debug.Print($" Today = {Date.Today.ToShortDateString} Month Start = {monthResult.StartDate.ToShortDateString} Month End = {monthResult.EndDate.ToShortDateString} ") ' expected output: ' Today = 11/2/2020 ' Month Start = 11/1/2020 ' Month End = 11/30/2020 |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 2, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils Public Class DateRange Public Property StartDate As Date Public Property EndDate As Date ''' <summary> ''' Takes in a date and returns the first and last day of the week ''' </summary> ''' <param name="date">The reference date in question</param> ''' <param name="firstDayOfWeek">The day the week starts on</param> ''' <returns>An object that contains the date range result</returns> Public Shared Function GetWeekStartAndEnd([date] As Date, Optional firstDayOfWeek As Microsoft.VisualBasic.FirstDayOfWeek = Microsoft.VisualBasic.FirstDayOfWeek.Monday) As DateRange Dim result = New DateRange result.StartDate = [date].AddDays(1 - Weekday([date], firstDayOfWeek)).Date result.EndDate = result.StartDate.AddDays(6).Date Return result End Function ''' <summary> ''' Takes in a date and returns the first and last day of the month ''' </summary> ''' <param name="date">The reference date in question</param> ''' <returns>An object that contains the date range result</returns> Public Shared Function GetMonthStartAndEnd([date] As Date) As DateRange Dim result = New DateRange result.StartDate = New Date([date].Year, [date].Month, 1).Date result.EndDate = result.StartDate.AddMonths(1).AddDays(-1).Date Return result End Function End Class End Module End Namespace ' http://programmingnotes.org/ |
4. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 2, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Module Program Sub Main(args As String()) Try Dim weekResult = Utils.DateRange.GetWeekStartAndEnd(Date.Today) Display($" Today = {Date.Today.ToShortDateString} Week Start = {weekResult.StartDate.ToShortDateString} Week End = {weekResult.EndDate.ToShortDateString} ") Dim monthResult = Utils.DateRange.GetMonthStartAndEnd(Date.Today) Display($" Today = {Date.Today.ToShortDateString} Month Start = {monthResult.StartDate.ToShortDateString} Month End = {monthResult.EndDate.ToShortDateString} ") Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
VB.NET || Roman Numeral Conversion – How To Convert Roman Numeral To Integer & Integer To Roman Numeral
The following is a program with functions which demonstrates how to convert roman numerals to integer, and integers to roman numerals.
The sample program implemented on this page was presented in a Data Structures course. This program was assigned in order to practice the use of the class data structure.
1. Roman Numeral Conversion
The example below demonstrates how to convert integers to roman numerals and roman numerals to integers.
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 2, 2020 ' Taken From: http://programmingnotes.org/ ' File: romanNumeralConversion.vb ' Description: The following demonstrates how to convert roman numerals. ' ============================================================================ Option Strict On Option Explicit On Imports System Module Program Sub Main(args As String()) Try Dim decimalNumber = 1987 DecimalTest(decimalNumber) Display("") Dim roman = "MCMXCI" RomanTest(roman) Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub RomanTest(roman As String) Dim decimalNumber = RomanNumeral.ConvertToDecimal(roman) Dim convertedBack = RomanNumeral.ConvertToRoman(decimalNumber) Display("======= Roman Test Start =======") Display($"Original = {roman} Converted = {decimalNumber} Converted Back To Original = {convertedBack}") Display("======= Roman Test End =======") End Sub Public Sub DecimalTest(decimalNumber As Decimal) Dim roman = RomanNumeral.ConvertToRoman(decimalNumber) Dim convertedBack = RomanNumeral.ConvertToDecimal(roman) Display("======= Decimal Test Start =======") Display($"Original = {decimalNumber} Converted = {roman} Converted Back To Original = {convertedBack}") Display("======= Decimal Test End =======") End Sub ''' <summary> ''' Class to hold Roman Numeral Conversion Values ''' </summary> Public Class RomanNumeral Public Property [decimal] As Decimal Public Property roman As String ''' <summary> ''' Converts a Decimal number to a Roman Numeral ''' </summary> ''' <param name="decimal">Decimal number to be converted</param> Public Shared Function ConvertToRoman(decimalNumber As Decimal) As String Dim stbRoman = New Text.StringBuilder If decimalNumber > 0 Then Dim conversionValues = GetValues() For Each conversionValue In conversionValues While decimalNumber > 0 _ AndAlso decimalNumber >= conversionValue.decimal stbRoman.Append(conversionValue.roman) decimalNumber -= conversionValue.decimal End While If decimalNumber <= 0 Then Exit For End If Next End If Dim roman = stbRoman.ToString Return roman End Function ''' <summary> ''' Converts a Roman Numeral to a Decimal value ''' </summary> ''' <param name="roman">Roman Numeral value to be converted</param> Public Shared Function ConvertToDecimal(roman As String) As Decimal Dim decimalNumber As Decimal = 0 If Not String.IsNullOrWhiteSpace(roman) Then Dim previousNumber As Decimal = 0 Dim conversionValues = GetValues() ' Iterate the string starting from the end For index = roman.Length - 1 To 0 Step -1 Dim currentLetter = roman(index).ToString ' Skip whitepsace characters If String.IsNullOrWhiteSpace(currentLetter) Then Continue For End If ' Find a conversion value that matches the current letter Dim conversionValue = (From x In conversionValues Where x.roman.ToLower = currentLetter.ToLower Select x Take 1).FirstOrDefault ' If a valid conversion was found, get the value If conversionValue Is Nothing Then Continue For End If Dim currentNumber = conversionValue.decimal ' Calculate the result If previousNumber > currentNumber Then decimalNumber -= currentNumber Else decimalNumber += currentNumber End If ' Save the current number in order to process the next letter previousNumber = currentNumber Next End If Return decimalNumber End Function ''' <summary> ''' Returns Roman Numeral Conversion Values ''' </summary> Public Shared Function GetValues() As List(Of RomanNumeral) Dim conversionValues = New List(Of RomanNumeral) From { New RomanNumeral With {.decimal = 1000, .roman = "M"}, New RomanNumeral With {.decimal = 900, .roman = "CM"}, New RomanNumeral With {.decimal = 500, .roman = "D"}, New RomanNumeral With {.decimal = 400, .roman = "CD"}, New RomanNumeral With {.decimal = 100, .roman = "C"}, New RomanNumeral With {.decimal = 90, .roman = "XC"}, New RomanNumeral With {.decimal = 50, .roman = "L"}, New RomanNumeral With {.decimal = 40, .roman = "XL"}, New RomanNumeral With {.decimal = 10, .roman = "X"}, New RomanNumeral With {.decimal = 9, .roman = "IX"}, New RomanNumeral With {.decimal = 5, .roman = "V"}, New RomanNumeral With {.decimal = 4, .roman = "IV"}, New RomanNumeral With {.decimal = 1, .roman = "I"} } Return conversionValues.OrderByDescending(Function(x) x.decimal).ToList() End Function End Class Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output
======= Decimal Test Start =======
Original = 1987
Converted = MCMLXXXVII
Converted Back To Original = 1987
======= Decimal Test End ============== Roman Test Start =======
Original = MCMXCI
Converted = 1991
Converted Back To Original = MCMXCI
======= Roman Test End =======
C++ || Roman Numeral Conversion – How To Convert Roman Numeral To Integer & Integer To Roman Numeral Using C++
The following is a program with functions which demonstrates how to convert roman numerals to integer, and integers to roman numerals.
The sample program implemented on this page is an updated version of a homework assignment which was presented in a C++ Data Structures course. This program was assigned in order to practice the use of the class data structure, which is very similar to the struct data structure.
1. Roman Numeral Conversion
The example below demonstrates how to convert integers to roman numerals and roman numerals to integers.
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 |
// ============================================================================ // Author: Kenneth Perkins // Taken From: http://programmingnotes.org/ // Date: Nov 2, 2020 // File: romanNumeralConversion.cpp // Description: The following demonstrates how to convert roman numerals. // ============================================================================ #include <iostream> #include <vector> #include <string> #include <cctype> #include <algorithm> /** * USE: Converts values from decimal to roman numeral and back */ class RomanNumeral { private: double m_decimal; std::string m_roman; bool m_isEmpty; public: RomanNumeral() { this->m_isEmpty = true; } RomanNumeral(double decimal, std::string roman) { this->m_decimal = decimal; this->m_roman = roman; this->m_isEmpty = false; } double decimal() const { return this->m_decimal; } std::string roman() const { return this->m_roman; } bool isEmpty() const { return this->m_isEmpty; } /** * FUNCTION: convertToRoman * USE: Converts a decimal number to a roman numeral * @param decimal: The number to be converted to a roman numeral. * @return: The converted roman numeral value. */ static std::string convertToRoman(double decimal) { std::string roman = ""; if (decimal > 0) { auto conversionValues = getValues(); for (const auto& conversionValue : conversionValues) { while (decimal > 0 && decimal >= conversionValue.decimal()) { roman += conversionValue.roman(); decimal -= conversionValue.decimal(); } if (decimal <= 0) { break; } } } return roman; } /** * FUNCTION: convertToDecimal * USE: Converts a roman numeral to a decimal value * @param roman: The number to be converted to a decimal number. * @return: The converted decimal value. */ static double convertToDecimal(std::string roman) { double decimal = 0; if (!isEmpty(roman)) { double previousNumber = 0; auto conversionValues = getValues(); // Iterate the std::string starting from the end for (int index = roman.length() - 1; index >= 0; --index) { // Get the current letter std::string currentLetter = std::string(1, roman[index]); // Skip whitepsace characters if (isEmpty(currentLetter)) { continue; } // Find a conversion value that matches the current letter auto conversionValue = find(conversionValues, currentLetter); // If a valid conversion was found, get the value if (conversionValue.isEmpty()) { continue; } auto currentNumber = conversionValue.decimal(); // Calculate the result if (previousNumber > currentNumber) { decimal -= currentNumber; } else { decimal += currentNumber; } // Save the current number in order to process the next letter previousNumber = currentNumber; } } return decimal; } // Returns roman Numeral Conversion Values static std::vector<RomanNumeral> getValues() { std::vector<RomanNumeral> conversionValues; conversionValues.push_back(RomanNumeral(1000, "M")); conversionValues.push_back(RomanNumeral(900, "CM")); conversionValues.push_back(RomanNumeral(500, "D")); conversionValues.push_back(RomanNumeral(400, "CD")); conversionValues.push_back(RomanNumeral(100, "C")); conversionValues.push_back(RomanNumeral(90, "XC")); conversionValues.push_back(RomanNumeral(50, "L")); conversionValues.push_back(RomanNumeral(40, "XL")); conversionValues.push_back(RomanNumeral(10, "X")); conversionValues.push_back(RomanNumeral(9, "IX")); conversionValues.push_back(RomanNumeral(5, "V")); conversionValues.push_back(RomanNumeral(4, "IV")); conversionValues.push_back(RomanNumeral(1, "I")); // Sort the list in descending roman numeral order std::sort(conversionValues.begin(), conversionValues.end(), [](const auto& lhs, const auto& rhs) { return lhs.decimal() > rhs.decimal(); }); return conversionValues; } // Finds a conversion that matches the given roman numeral search value static RomanNumeral find(std::vector<RomanNumeral> conversionValues, std::string searchValue) { RomanNumeral result; searchValue = toLower(searchValue); auto predicate = [searchValue](auto x) { return (toLower(x.roman()) == searchValue); }; auto iter = std::find_if(conversionValues.begin(), conversionValues.end(), predicate); if (iter != conversionValues.end()) { result = *iter; } return result; } // Checks if a string is empty or only contains whitespace static bool isEmpty(std::string str) { return str.empty() || std::all_of(str.begin(), str.end(), [](char c) { return std::isspace(static_cast<unsigned char>(c)); }); } // Converts a string to lowercase static std::string toLower(std::string str) { std::transform(str.begin(), str.end(), str.begin(), [](char c) { return std::tolower(static_cast<unsigned char>(c)); }); return str; } }; void display(std::string message) { message += "\n"; std::cout << message; } void romanTest(std::string roman) { auto decimal = RomanNumeral::convertToDecimal(roman); auto convertedBack = RomanNumeral::convertToRoman(decimal); display("======= Roman Test Start ======="); display("Original = " + roman + "\n Converted = " + std::to_string(decimal) + "\n Converted Back To Original = " + convertedBack); display("======= Roman Test End ======="); } void decimalTest(double decimal) { auto roman = RomanNumeral::convertToRoman(decimal); auto convertedBack = RomanNumeral::convertToDecimal(roman); display("======= Decimal Test Start ======="); display("Original = " + std::to_string(decimal) + "\n Converted = " + roman + "\n Converted Back To Original = " + std::to_string(convertedBack)); display("======= Decimal Test End ======="); } int main() { auto decimal = 1987; decimalTest(decimal); display(""); auto roman = "McMxcI"; romanTest(roman); std::cin.get(); return 0; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output
======= Decimal Test Start =======
Original = 1987.000000
Converted = MCMLXXXVII
Converted Back To Original = 1987.000000
======= Decimal Test End ============== Roman Test Start =======
Original = McMxcI
Converted = 1991.000000
Converted Back To Original = MCMXCI
======= Roman Test End =======